I'm using a custom Editor Script to display and adjust parameters of elements in a Reorderable List. But whenever I restart Unity and click the GameObject holding the script, these parameters are reseted (like shown in the picture at the end of the post). When I deactivate the Custom Editor Script, everything works as it should, so the problem might be in this script.
This is the Story class using the custom Editor script:
[System.Serializable]
public class Story : MonoBehaviour
{
public List<Chapter> ChapterList;
}
and the Editor Script, using a Reorderable List:
[CustomEditor(typeof(Story))]
public class StoryEditor : Editor
{
private Story story;
private ReorderableList reorderableList;
void OnEnable()
{
story = (Story) target;
if (story.ChapterList == null)
story.ChapterList = new System.Collections.Generic.List<Chapter>();
if (reorderableList == null)
reorderableList =
new ReorderableList(
story.ChapterList,
typeof(Chapter),
true, true, true, true);
reorderableList.drawElementCallback += DrawElement;
}
private void DrawElement(Rect rect, int index, bool active, bool focused)
{
Chapter c = story.ChapterList[index];
EditorGUI.BeginChangeCheck();
c.ID = index+1;
EditorGUI.IntField(new Rect(rect.x, rect.y, 20, rect.height-1), c.ID);
c.Title = EditorGUI.TextField(new Rect(rect.x + 21, rect.y, rect.width - 79, rect.height-1), c.Title);
c.Hide = EditorGUI.Toggle(new Rect(rect.x + rect.width - 56, rect.y, 17, rect.height-1), c.Hide);
if (GUI.Button(
new Rect(
rect.x + rect.width - 40, rect.y, 40, rect.height-1), "Edit"))
Select(index);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(target);
}
}
I googled the problem, but the solutions I found mentioned to use [System.Serializable] and EditorUtility.SetDirty(target);, which I'm using both. I assume I'm missusing something in the "DrawElement" method, but I can't figure out what it is.
Upper part: Before Restarting, Lower Part: After Restarting Unity.
Story
has[Serializable]
rather thanChapter
(which I don't know if it also has[Serializable]
because I don't have its code). You also don't appear to callSetDirty
when items are added/removed, although maybe you've determined that's not necessary in your case. I guess what I'm suggesting is that you use this debug approach: use code identical to something that works for people and change it to what you think you need one step at a time to isolate what small change causes problems. – 31eee384Add/Remove
also callSetDirty
andChapter
is[Serializable]
too. – Jonas Zimmer