1

The headline already says it ...

I have a custom editor script for a C# class in Unity with that a user can set several parameters via UI. But there's one case where some serialized variables in the class belonging to the CustomEditor class are changed programmatically and the change is not updated in the inspector.

How do I tell the CustomEditor class that it should update the changed variable?

Example code:

public class Foo
{
    [SerializeField] private float value;

    public void ChangeValue()
    {
        value = 1.0f;
    }
}


[CustomEditor(typeof (Foo))]
internal class FooEditor : Editor
{
    private Foo self;
    private SerializedProperty value;

    internal void OnEnable()
    {
        self = (target as Foo);
        value = serializedObject.FindProperty("value");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

    }
}

In Foo I want to change value vis script but the value update is not reflected in the editor UI when set via script (only when a user changes it via UI). How can I make the change reflect also when it's updated via script?

1 Answer 1

1

Use this on your override:

public override void OnInspectorGUI ()
{   
    if (GUI.changed)
    {
        value = mynewvalue;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for providing this answer. It was surprisingly hard to find out the easy way to cause a Custom Editor to auto-update with a change. I created a level editor and I didn't like it creating/destroying objects every frame even when there were no changes. This did exactly what I needed. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.