- Home /
Unity Custom Editor - save value
This question has been asked, but the answers I have come across do not work for me, so I will ask it again. I have a class similar to this:
public class MyClass : MonoBehavior
{
public int foo;
}
I also have an editor for this class, like so:
[CustomEditor(typeof(MyClass))]
public class MyEditor : Editor
{
private MyClass myClass;
public int editInfo;
public override void OnInspectorGUI ()
{
DrawDefaultInspector ();
myClass = (MyClass)target;
editInfo = EditorGUILayout.IntField("Edit Info: ", editInfo);
if(GUILayout.Button ("edit class"))
{
editClass();
}
}
public void editClass()
{
myClass.foo += editInfo;
}
}
My problem is twofold : Firstly, Upon hitting the play button in the editor, Unity resets the editInfo field in my custom Editor. Secondly, although the changes are kept in my Dummy class (MyClass), if I implement this on a real class (too large to show here, but the functionality is the same), the value reverts.
I have tried Undo.RecordObject() and I have added the SerializeField property to all possible places where it could have any meaning. What am i doing wrong?
Ok, I figured out why the variable in my real life class was reverting. The problem was that it was a prefab. On play, it reverted to the prefab value (I still don't know why). Applying the changes to the prefab fixed this. It would still be nice to not have to do this. Any suggestions?
Answer by FortisVenaliter · Jul 27, 2017 at 07:24 PM
Assume that nothing in an Editor class is permanent. It can change from frame to frame. If you assume anything will be persistent, you're setting yourself up for failure.
Also, don't store the myClass as a class-scope variable. It should remain method-scope or use an automated getter property.
Finally, keep the data in the data class. "editInfo" is indeterminate simply because it's in an editor class. As soon as you move it to the source data class, it will be more persistent.
I'm not sure what you mean by "nothing in the Editor class is permanent". Do you mean all variables, fields, etc.?
Why should myClass be method-scope as opposed to class-scope? It doesn't seem to make a difference.
The third paragraph avoids the problem, but is inconvenient for other reasons. I don't really want my editor specific variables in the class itself. It seems like a hack. It also clutters the editor - ins$$anonymous$$d of begin able to place my editInfo variable near its associated button, it is wedged in between all my other stuff. Not ideal.
For your first question: yes. Every bit of data not built-in to an editor class is suspect. They may stay between objects, or may get cleared out between updates on the same object, so never assume they are deter$$anonymous$$ate or persistent.
For the second... your code there is technically safe as written. But if you didn't set it every single call (line 11), you may end up with an outdated instance after switching between two instances in the hierarchy selector. And it could cause problems moving forward when you up the complexity, so it's best to keep it local. The reason it should be method-scope is to prevent the 'target' variable from changing without updating the 'myClass' variable. If you kept it method-scope, that wouldn't be possible.
Unfortunately, if it needs to be persistent, it can't be in the editor. If you want a clean structure to work with, add an inner class to your data class called "EditorData" that's serializable. That way the editor-only data is at least grouped together in it's own place and you can set it to null in Start() to prevent it from taking up RA$$anonymous$$ outside of the editor.
Thanks, this makes sense. the internal struct is basically what i did to organize. I thought about scriptable objects (I forget exactly what they are, but I remember they are used for storing persistent data), but it seemed like overkill, even if it would work.
Your answer
Follow this Question
Related Questions
Editor slows down by ForLoop(I guess). 1 Answer
How to Hide/Show List or Array in the inspector based on a variable? 0 Answers
How to add custom ui on top control panel 1 Answer
Generic CustomEditor 0 Answers
Automatically increment an integer field in a bunch of prefab gameobjects in the unity editor. 0 Answers