- Home /
Detecting GUI.changed using DrawDefaultInspector
I've created a custom editor that displays the default inspector. I'd like to detect when inspector values have changed, but GUI.changed always returns false.
Does GUI.changed work with DrawDefaultInspector? If not, is there an alternative solution?
[CustomEditor(typeof(PlayerWalk))] public class PlayerWalkEditor : Editor { public override void OnInspectorGUI () { PlayerWalk t = (PlayerWalk)target;
DrawDefaultInspector ();
Debug.Log (GUI.changed);
}
}
Answer by Byterunner · Apr 04, 2011 at 03:58 PM
Yes, it does, but GUI.changed is set to true only for the frame that the GUI changes. The following will print to the console whenever it detects a change in your script:
I don't know what your PlayerWalk script is doing, so I made a quick one of my own:
using UnityEngine;
public class PlayerWalk : MonoBehaviour { public float speed = 1.0f;
void Start()
{
}
}
And here is the full PlayerWalkEditor.cs:
using UnityEngine; using UnityEditor;
[CustomEditor(typeof(PlayerWalk))]
public class PlayerWalkEditor : Editor { public override void OnInspectorGUI () { PlayerWalk t = (PlayerWalk)target;
DrawDefaultInspector ();
if(GUI.changed)
{
Debug.Log ("changed");
}
}
}
Make sure you put PlayerWalkEditor.cs into a folder called 'Editor'. Now when you add the PlayerWalk script to an object and change the speed variable, "changed" is printed to the console.
Debug.Log("changed") is never called, so I'm certain that GUI.changed is never beco$$anonymous$$g true. It sounds like this may be a Unity bug.
It's not a bug, it's working correctly. I've updated my answer to provide more insight.
Thanks, but I'm afraid I should have clarified in my original post. GUI.changed had been working before I began using DrawDefaultInspector(), and indeed continues to work in a few other editors within the same project which implement their own custom inspector GUI. I recently replaced all of PlayerWalkEditor's GUIlayout methods with a single DrawDefaultInspector call which immediately broke the if(GUI.changed) code block.
In addition, OnInspectorGUI is called successfully, but the Debug.Log(GUI.changed) line in my original post will always log "false" when a default GUI control for one of PlayerWalk's public fields is manipulated within the inspector.
If you're not putting your Debug.Log(GUI.Changed) command within some kind of if() block, it's just going to print "false" every single frame. I had originally took your posted code and found that it did print "true" for the one frame that it detected the GUI change, but then immediately went back to "false" on the very next frame. I had to scroll way back in the console to find the true, but it was there. GUI.changed does not persist from frame to frame; it's always false unless you're in the middle of changing something. If you want the value to persist, you'll need to make a new variable.
Answer by ciela_spike · Sep 17, 2014 at 11:19 AM
Just use the return value of DrawDefaultInspector, like:
if (DrawDefaultInspector()) {
Debug.Log("Changed");
}
Answer by adamonline45 · Feb 07, 2012 at 06:13 AM
This seems to be about a year old, but I've observed that using the code above, it only says 'changed' when I change the last inspector-visible property. I suspect that if you want all the inspector values to emit this 'changed' text, you would need to override the InspectorGUI implementation for each property, with the check for GUI.Changed() after each. At least, that's my speculation at this point.
I'm still researching this, and will update if appropriate.
Your answer
Follow this Question
Related Questions
How to show an array of custom objects in Inspector? 2 Answers
How to create a unique looking inspector, 1 Answer
Prefix label greyed out when following component disabled 1 Answer
How to view variables from a script stored inside another script in Inspector 1 Answer
Questions Regarding Images in Custom Inspector/Editor 1 Answer