Undo add/delete ScriptableObject

I’m trying to do pathEditor, each path node is ScriptableObject

[System.Serializable]
public class TGPathNode: ScriptableObject {
        public TGPathBezierMode    BezierMode;
        public TGPathNodeMode    Mode;
        public List<TGPathNode> Links = new List<TGPathNode>();
        public Vector3[]         Points = new Vector3[3];
}

Nodes stores in MonoBehaviour script like this

    public List<TGPathNode> PathNodes = new List<TGPathNode>();

In Editor i need Undo support for add delete pathnode, please help me to do it
This code no effect after Undo command:

Undo.RecordObject(_target.PathNodes[0],"DeleteNode");
                DestroyImmediate(_target.PathNodes[0]);
                EditorUtility.SetDirty(_target);

Please help me i’m new in C# and Unity

Try this:

// Make sure that the list is serialized
[SerializeField]
public List<TGPathNode> PathNodes = new List<TGPathNode>();

Undo.RecordObject(_target,"DeleteNode");
Undo.DestroyImmediate(_target.PathNodes[0]);
EditorUtility.SetDirty(_target);

No effect
Other things like Move, Select and Add works fine with

Undo.RecordObject(_target,"DeleteNode");
// Some stuff except Delete PathNode works fine
EditorUtility.SetDirty(_target);

I put

Debug.Log(_target.PathNodes.Count);

and after Undo command log show previous count, but PathNode[0]=null
Why?

I found a solution that worked for me here

Undo.RecordObjects( new UnityEngine.Object [] { itemToRemove, holder }, "Removed");
Undo.RecordObjects( new UnityEngine.Object [] {  holder }, "Removed");
holder.Remove(itemToRemove);
Undo.DestroyObjectImmediate(itemToRemove);
Undo.RecordObjects( new UnityEngine.Object [] {  holder }, "Removed");
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
1 Like

Thanks! Some another way but it works fine!

TGPathNode node;
// loop until all Nodes to delete
{
  Undo.RecordObject(_target,"DeleteNode");
  node=PathNodes[nodeToDeleteindex];
  _target.PathNodes.Remove(node);
  Undo.DestroyObjectImmediate(node);
}
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
EditorUtility.SetDirty(_target);

I did’nt now about Undo.DestroyObjectImmediate and Undo.CollapseUndoOperations

1 Like