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);
}
}
3 Answers
3
Just use the return value of DrawDefaultInspector, like:
if (DrawDefaultInspector()) {
Debug.Log("Changed");
}
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.
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.
Debug.Log("changed") is never called, so I'm certain that GUI.changed is never becoming true. It sounds like this may be a Unity bug.
– anon53920522It's not a bug, it's working correctly. I've updated my answer to provide more insight.
– anon92224933Thanks, 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.
– anon53920522In 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.
– anon53920522I understand that GUI.changed remains true only for a single frame, but since if(GUI.changed) was not ever evaluating to true I had to resort to manually checking the raw output of GUI.changed on every frame. After sifting through the console output, there was not a single instance of "true". Given that we've both tested the same code, and that GUI.changed is never true for me but is true during at least one frame for you, it seems to strongly suggest that this is a Unity bug. I appreciate the help.
– anon53920522