Reputation: 9586
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?
Upvotes: 1
Views: 4326
Reputation: 1736
Use this on your override:
public override void OnInspectorGUI ()
{
if (GUI.changed)
{
value = mynewvalue;
}
}
Upvotes: 1