Changes in Custom Inspector not reflected in Scene
Hi,
I'm experimenting a bit with custom editor, and so far, everything is almost fine.
The "almost" part is some problem with my latest script, a component supposed to update a couple GUIText (in other GameObjects), in fact, I want this script to be a centralized way of editing several texts at a time.
The thing is that whenever I update the string field of my CustomEditor, it does update the string field of the GUIText, but the text itself, as I see it in my scene, doesn't get refreshed. To be fair, sometimes it does refresh the GUIText... But in a messed up way... It will refresh afterward, whenever I move the GUIText, or enable/disable it, but it's kinda... Messy, and I wish I could solve this issue.
I guess it might be a bit hard to explain / understand, but images are worth a thousand words, so here it is :
Sometimes, instead of a messed up text, it just doesn't update at all.
Notes :
I do call Undo.RecordObject to save my object
I don't call EditorUtility.SetDirty, it apparently shouldn't be used anymore (since Unity 5.3, I'm using 5.3.6p5 by the way), and although it does update the scene view / game view properly, it doesn't save my serialized attributes properly (they are reseted when I reload the scene / reopen the project)
Although I don't think it's related to the issue, the string exposed in the editor is actually part of a non-serialized Dictionary. The key / value are serialized by being stored in 2 Lists, "key" and "value" as suggested by this page of the documentation : https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.OnBeforeSerialize.html
If I add "base.InspectorGUI()" at the beginning of my custom InspectorGUI code, those lists become available in the editor (instead of the Dictionary values, wich I do not want). Updating the "value" list does what I want, meaning that the original InspectorGUI has lines of code doing what I want to, I just need to figure wich lines... But I can't see the code of the original Editor class, can I?
So, basically, what I need is probably some generic (not GUIText related) line of code that updates the scene I guess.
This is a simplified version of my code :
serializedObject.Update();
MyCustomComponent myScript = target as MyCustomComponent;
SerializedProperty toa = serializedObject.FindProperty("textObjectArray");
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(toa, true);
EditorGUILayout.Space();
EditorGUILayout.LabelField("ROTATION Instruction Text");
//Saves the value to a serialized attribute of MyCustomComponent and changes the text attribute of all Texts stored within the textobjectArray
myScript.setInstructionText(EditorGUILayout.TextArea(myScript.getInstructionText, GUILayout.MaxHeight(30)));
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
}
if (GUI.changed)
{
Undo.RecordObject(myScript, "Saving Instruction Dispatcher of "+ myScript.gameObject.name);
}
I believe I'm missing an instruction to refresh the scene based on the changes I've made. If anyone has a clue on what I'm missing, I'd follow any lead, thanks :)
Answer by DorianF · Oct 19, 2016 at 12:30 PM
Ok, I guess I found what I was missing, or at least a part of it, though I feel like my answer will be incomplete.
I finally found something alike my problem here http://answers.unity3d.com/questions/1042052/change-text-in-text-component-from-editor-script.html so I just update my code :
if (GUI.changed)
{
//NEW
foreach(Text text in myScript.textObjectArray)
{
EditorUtility.SetDirty(text);
}
//END NEW
Undo.RecordObject(myScript, "Saving Instruction Dispatcher of "+ myScript.gameObject.name);
}
What I was doing before was setting "myScript" itself as dirty, wich was somehow refreshing the view with my latest changes, but not saving them within my script.
I'm not exactly sure as to why this works better than what I was doing before, but it does. However, when I read the docs on SetDirty I understand it as "One should probably not use SetDirty anymore, or at least, not in 95% of the cases." and I don't feel like I'm in the remaining 5% cases, so I just feel like there should be a better solution...
For now, I'll just use this one
Add 1 point for you! Thanks this works well for me 2017.1.b10. This issue is fixed since years in 2017.3.