暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GraphObject.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using UnityEditor.Graphs;
  3. using UnityEditor.ShaderGraph;
  4. using UnityEditor.ShaderGraph.Serialization;
  5. using UnityEngine;
  6. namespace UnityEditor.Graphing
  7. {
  8. class HandleUndoRedoAction : IGraphDataAction
  9. {
  10. void HandleGraphUndoRedo(GraphData graphData)
  11. {
  12. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out HandleUndoRedoAction");
  13. AssertHelpers.IsNotNull(newGraphData, "NewGraphData is null while carrying out HandleUndoRedoAction");
  14. try
  15. {
  16. graphData?.ReplaceWith(newGraphData);
  17. }
  18. catch (Exception e)
  19. {
  20. if (graphData != null)
  21. graphData.replaceInProgress = false;
  22. throw e;
  23. }
  24. }
  25. public Action<GraphData> modifyGraphDataAction => HandleGraphUndoRedo;
  26. public GraphData newGraphData { get; set; }
  27. }
  28. class GraphObject : ScriptableObject, ISerializationCallbackReceiver
  29. {
  30. [SerializeField]
  31. SerializationHelper.JSONSerializedElement m_SerializedGraph;
  32. [SerializeField]
  33. int m_SerializedVersion;
  34. [SerializeField]
  35. bool m_IsDirty;
  36. [SerializeField]
  37. bool m_IsSubGraph;
  38. [SerializeField]
  39. string m_AssetGuid;
  40. internal string AssetGuid
  41. {
  42. get => m_AssetGuid;
  43. }
  44. [NonSerialized]
  45. GraphData m_Graph;
  46. [NonSerialized]
  47. int m_DeserializedVersion;
  48. public DataStore<GraphData> graphDataStore
  49. {
  50. get => m_DataStore;
  51. private set
  52. {
  53. if (m_DataStore != value && value != null)
  54. m_DataStore = value;
  55. }
  56. }
  57. DataStore<GraphData> m_DataStore;
  58. public GraphData graph
  59. {
  60. get { return m_Graph; }
  61. set
  62. {
  63. if (m_Graph != null)
  64. m_Graph.owner = null;
  65. m_Graph = value;
  66. graphDataStore = new DataStore<GraphData>(ReduceGraphDataAction, m_Graph);
  67. if (m_Graph != null)
  68. m_Graph.owner = this;
  69. }
  70. }
  71. Material m_MaterialArtifact;
  72. /// <summary>
  73. /// The Material artifact generated by the import process.
  74. /// Every modification to this material will be lost when the graph is saved.
  75. /// for.
  76. /// </summary>
  77. public Material materialArtifact
  78. {
  79. get
  80. {
  81. if (m_MaterialArtifact == null)
  82. m_MaterialArtifact = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(AssetGuid));
  83. return m_MaterialArtifact;
  84. }
  85. }
  86. // this value stores whether an undo operation has been registered (which indicates a change has been made to the graph)
  87. // and is used to trigger the MaterialGraphEditWindow to update it's title
  88. public bool isDirty
  89. {
  90. get { return m_IsDirty; }
  91. set { m_IsDirty = value; }
  92. }
  93. public virtual void RegisterCompleteObjectUndo(string actionName)
  94. {
  95. Undo.RegisterCompleteObjectUndo(this, actionName);
  96. if (materialArtifact)
  97. Undo.RecordObject(m_MaterialArtifact, actionName);
  98. m_SerializedVersion++;
  99. m_DeserializedVersion++;
  100. m_IsDirty = true;
  101. }
  102. public void OnBeforeSerialize()
  103. {
  104. if (graph != null)
  105. {
  106. var json = MultiJson.Serialize(graph);
  107. m_SerializedGraph = new SerializationHelper.JSONSerializedElement { JSONnodeData = json };
  108. m_IsSubGraph = graph.isSubGraph;
  109. m_AssetGuid = graph.assetGuid;
  110. }
  111. }
  112. public void OnAfterDeserialize()
  113. {
  114. }
  115. public bool wasUndoRedoPerformed => m_DeserializedVersion != m_SerializedVersion;
  116. public void HandleUndoRedo()
  117. {
  118. Debug.Assert(wasUndoRedoPerformed);
  119. var deserializedGraph = DeserializeGraph();
  120. var handleUndoRedoAction = new HandleUndoRedoAction();
  121. handleUndoRedoAction.newGraphData = deserializedGraph;
  122. graphDataStore.Dispatch(handleUndoRedoAction);
  123. }
  124. GraphData DeserializeGraph()
  125. {
  126. var json = m_SerializedGraph.JSONnodeData;
  127. var deserializedGraph = new GraphData { isSubGraph = m_IsSubGraph, assetGuid = m_AssetGuid };
  128. MultiJson.Deserialize(deserializedGraph, json);
  129. m_DeserializedVersion = m_SerializedVersion;
  130. m_SerializedGraph = default;
  131. return deserializedGraph;
  132. }
  133. public void Validate()
  134. {
  135. if (graph != null)
  136. {
  137. graph.OnEnable();
  138. graph.ValidateGraph();
  139. }
  140. }
  141. // This is a very simple reducer, all it does is take the action and apply it to the graph data, which causes some mutation in state
  142. // This isn't strictly redux anymore but its needed given that our state tree is quite large and we don't want to be creating copies of it everywhere by unboxing
  143. void ReduceGraphDataAction(GraphData initialState, IGraphDataAction graphDataAction)
  144. {
  145. graphDataAction.modifyGraphDataAction(initialState);
  146. }
  147. void OnEnable()
  148. {
  149. if (graph == null && m_SerializedGraph.JSONnodeData != null)
  150. {
  151. graph = DeserializeGraph();
  152. }
  153. Validate();
  154. }
  155. void OnDestroy()
  156. {
  157. graph?.OnDisable();
  158. }
  159. }
  160. }