using System; using UnityEditor; using UnityEngine; namespace UnityEditor.Graphing { public class SerializableGraphObject : ScriptableObject, IGraphObject, ISerializationCallbackReceiver { [SerializeField] SerializationHelper.JSONSerializedElement m_SerializedGraph; IGraph m_Graph; IGraph m_DeserializedGraph; public IGraph graph { get { return m_Graph; } set { if (m_Graph != null) m_Graph.owner = null; m_Graph = value; if (m_Graph != null) m_Graph.owner = this; } } public void RegisterCompleteObjectUndo(string name) { Undo.RegisterCompleteObjectUndo(this, name); } public void OnBeforeSerialize() { if (graph != null) m_SerializedGraph = SerializationHelper.Serialize(graph); } public void OnAfterDeserialize() { var deserializedGraph = SerializationHelper.Deserialize(m_SerializedGraph, null); if (graph == null) graph = deserializedGraph; else m_DeserializedGraph = deserializedGraph; // graph.ReplaceWith(m_DeserializedGraph); } void Validate() { if (graph != null) { graph.OnEnable(); graph.ValidateGraph(); } } void OnEnable() { Validate(); Undo.undoRedoPerformed += UndoRedoPerformed; UndoRedoPerformed(); } void OnDisable() { Undo.undoRedoPerformed -= UndoRedoPerformed; } void UndoRedoPerformed() { if (m_DeserializedGraph != null) { graph.ReplaceWith(m_DeserializedGraph); m_DeserializedGraph = null; } } } }