Tracking changes of a Component on the selected GameObject in custom EditorWindow

I am trying to create my own EditorWindow for one of my components called Interactable (see code later).

The edit window will eventually become much more complex, but for now all I have in it is a few boxes and text fields. I want this custom EditorWindow to work much like the Animation window, meaning: it only displays stuff when the selected gameObject has an Interactable component and I mainly want to edit the properties of the Interactable.

THE PROBLEM:
So far everything’s peachy, except that I can’t make the editor register any changes of the Interactable (i.e. the little asterisk does not appear in the main window title and therefore my changes cannot be saved). (My Unity version is 5.3.5 btw)

I must have missed something. How do I make this work?
Any help is appreciated!

Here are the relevant classes:

[Serializable]
public class InteractionNode
{
    public string Name { get; set; }
    public Vector2 Position { get; set; }
    public Vector2 Size { get; set; }
    
    public InteractionNode(string name)
    {
        Name = name;
    }
}

[Serializable]
public class InteractionGraph
{
    public List<InteractionNode> Nodes { get; set; }

    public InteractionGraph()
    {
        Nodes = new List<InteractionNode>();
    }
}

public class Interactable : MonoBehaviour
{

    [HideInInspector]
    public InteractionGraph interactionGraph;

    public Texture2D cursorTexture;

    [SerializeField]
    private Vector2 _DialogPosition;
    public Vector2 DialogPosition
    {
        get
        {
            return transform.TransformPoint(new Vector3(_DialogPosition.x, _DialogPosition.y, 0f));
        }
        set
        {
            _DialogPosition = transform.InverseTransformPoint(new Vector3(value.x, value.y, 0f));
        }
    }

}

I have a custom editor for Interactables, which works just fine:

[CustomEditor(typeof(Interactable), true)]
public class InteractableEditor : Editor
{
    void OnSceneGUI()
    {
        var stuff = target as Interactable;

        //talk pos
        Undo.RecordObject(target, "dialog position changed");
        stuff.DialogPosition = Handles.DoPositionHandle(stuff.DialogPosition, Quaternion.identity);
        Handles.Label(stuff.DialogPosition, "dialog");
    }
}

And finally here’s the EditorWindow (I have commented out everything now I’m only trying to trigger the change tracking):

public class ScriptNodesEditor : EditorWindow
{
    private static ScriptNodesEditor nodeScriptEditorWindow;

[MenuItem("Window/MyEditor")]
    static void ShowEditor()
    {
        nodeScriptEditorWindow = GetWindow<ScriptNodesEditor>();
    }

    void OnSelectionChange()
    {
        Repaint();
    }

    void OnGUI()
    {
        if (Selection.activeGameObject != null)
        {
            var interactable = Selection.activeGameObject.GetComponent<Interactable>();
            Undo.RecordObject(interactable, "change");
        }
    }
}

Now this should trigger the change on every OnGUI call, right? Nothing is happening.

Instead of Undo.RecordObject(interactable) I tried calling

  • Undo.RecordObject(Selection.activeGameObject, “”)
  • EditorUtility.SetDirty(interactable)
  • EditorUtility.SetDirty(Selection.activeGameObject)

None worked so far.

Found the answer.

In short: properties are not serialized. I changed the properties to public fields and everything is fine.

In more detail…
When you use Undo.RecordObject() a snapshot will be generated (through serialization) of the model you are passing to this method, and when Unity checks if anything is changed on your model it will compare the snapshot taken earlier with the current model. Since the c# properties are not serialized, they won’t show up on the snapshot, therefore changes on those will not be noticed.