GraphObject.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using UnityEditor.ShaderGraph;
  3. using UnityEngine;
  4. namespace UnityEditor.Graphing
  5. {
  6. class GraphObject : ScriptableObject, ISerializationCallbackReceiver
  7. {
  8. [SerializeField]
  9. SerializationHelper.JSONSerializedElement m_SerializedGraph;
  10. [SerializeField]
  11. int m_SerializedVersion;
  12. [SerializeField]
  13. bool m_IsDirty;
  14. [SerializeField]
  15. bool m_IsSubGraph;
  16. [SerializeField]
  17. string m_AssetGuid;
  18. [NonSerialized]
  19. GraphData m_Graph;
  20. [NonSerialized]
  21. int m_DeserializedVersion;
  22. public GraphData graph
  23. {
  24. get { return m_Graph; }
  25. set
  26. {
  27. if (m_Graph != null)
  28. m_Graph.owner = null;
  29. m_Graph = value;
  30. if (m_Graph != null)
  31. m_Graph.owner = this;
  32. }
  33. }
  34. public bool isDirty
  35. {
  36. get { return m_IsDirty; }
  37. set { m_IsDirty = value; }
  38. }
  39. public void RegisterCompleteObjectUndo(string actionName)
  40. {
  41. Undo.RegisterCompleteObjectUndo(this, actionName);
  42. m_SerializedVersion++;
  43. m_DeserializedVersion++;
  44. m_IsDirty = true;
  45. }
  46. public void OnBeforeSerialize()
  47. {
  48. if (graph != null)
  49. {
  50. m_SerializedGraph = SerializationHelper.Serialize(graph);
  51. m_IsSubGraph = graph.isSubGraph;
  52. m_AssetGuid = graph.assetGuid;
  53. }
  54. }
  55. public void OnAfterDeserialize()
  56. {
  57. if (graph == null)
  58. {
  59. graph = DeserializeGraph();
  60. }
  61. }
  62. public bool wasUndoRedoPerformed => m_DeserializedVersion != m_SerializedVersion;
  63. public void HandleUndoRedo()
  64. {
  65. Debug.Assert(wasUndoRedoPerformed);
  66. var deserializedGraph = DeserializeGraph();
  67. m_Graph.ReplaceWith(deserializedGraph);
  68. }
  69. GraphData DeserializeGraph()
  70. {
  71. var deserializedGraph = SerializationHelper.Deserialize<GraphData>(m_SerializedGraph, GraphUtil.GetLegacyTypeRemapping());
  72. deserializedGraph.isSubGraph = m_IsSubGraph;
  73. deserializedGraph.assetGuid = m_AssetGuid;
  74. m_DeserializedVersion = m_SerializedVersion;
  75. m_SerializedGraph = default(SerializationHelper.JSONSerializedElement);
  76. return deserializedGraph;
  77. }
  78. public void Validate()
  79. {
  80. if (graph != null)
  81. {
  82. graph.OnEnable();
  83. graph.ValidateGraph();
  84. }
  85. }
  86. void OnEnable()
  87. {
  88. Validate();
  89. }
  90. void OnDestroy()
  91. {
  92. graph?.OnDisable();
  93. }
  94. }
  95. }