From bca8c3af6be955c6d4d210e864508c949dd65d3a Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Fri, 1 Jul 2022 19:47:23 +0800 Subject: [PATCH] 1.working on node serialization --- EasyRandomGenerator.meta | 8 ++++ Sample/MathGraph.meta | 8 ++++ Sample/MathGraph/Editor.meta | 8 ++++ Sample/MathGraph/Editor/MathEditor.asset | 27 ++++++++++++++ Sample/MathGraph/Editor/MathEditor.asset.meta | 8 ++++ Sample/MathGraph/Editor/MathEditor.cs | 22 +++++++++++ Sample/MathGraph/Editor/MathEditor.cs.meta | 14 +++++++ Sample/MathGraph/Editor/MathGraphView.cs | 9 +++++ Sample/MathGraph/Editor/MathGraphView.cs.meta | 11 ++++++ Sample/MathGraph/MathGraph.cs | 9 +++++ Sample/MathGraph/MathGraph.cs.meta | 11 ++++++ Sample/MathGraph/New MathGraph.asset | 23 ++++++++++++ Sample/MathGraph/New MathGraph.asset.meta | 8 ++++ TNode/Attribute/DisableOnInspector.cs | 11 ++++++ TNode/Attribute/DisableOnInspector.cs.meta | 3 ++ TNode/Editor/BaseViews/DataGraphView.cs | 37 +++++++++++++++++++ TNode/Editor/GraphEditor.cs | 2 +- TNode/Editor/Inspector/NodeInspector.cs | 7 +++- TNode/Editor/Model/NodeEditorData.cs | 5 ++- .../GraphEditorCreator/GraphEditorCreator.cs | 16 ++++---- TNode/Models/NodeData.cs | 3 +- 21 files changed, 237 insertions(+), 13 deletions(-) create mode 100644 EasyRandomGenerator.meta create mode 100644 Sample/MathGraph.meta create mode 100644 Sample/MathGraph/Editor.meta create mode 100644 Sample/MathGraph/Editor/MathEditor.asset create mode 100644 Sample/MathGraph/Editor/MathEditor.asset.meta create mode 100644 Sample/MathGraph/Editor/MathEditor.cs create mode 100644 Sample/MathGraph/Editor/MathEditor.cs.meta create mode 100644 Sample/MathGraph/Editor/MathGraphView.cs create mode 100644 Sample/MathGraph/Editor/MathGraphView.cs.meta create mode 100644 Sample/MathGraph/MathGraph.cs create mode 100644 Sample/MathGraph/MathGraph.cs.meta create mode 100644 Sample/MathGraph/New MathGraph.asset create mode 100644 Sample/MathGraph/New MathGraph.asset.meta create mode 100644 TNode/Attribute/DisableOnInspector.cs create mode 100644 TNode/Attribute/DisableOnInspector.cs.meta diff --git a/EasyRandomGenerator.meta b/EasyRandomGenerator.meta new file mode 100644 index 0000000..fb32c7a --- /dev/null +++ b/EasyRandomGenerator.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cf0168c8ec1f9304c9872577b1e6abdf +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph.meta b/Sample/MathGraph.meta new file mode 100644 index 0000000..911ec5f --- /dev/null +++ b/Sample/MathGraph.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 23ebabfc8f40d2c4689dc4ec9a5786d5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph/Editor.meta b/Sample/MathGraph/Editor.meta new file mode 100644 index 0000000..1a654ad --- /dev/null +++ b/Sample/MathGraph/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 73ee98eea19fa9b42b9c7990a8161d56 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph/Editor/MathEditor.asset b/Sample/MathGraph/Editor/MathEditor.asset new file mode 100644 index 0000000..bfe8dac --- /dev/null +++ b/Sample/MathGraph/Editor/MathEditor.asset @@ -0,0 +1,27 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cde084f079a7426daa86ed86cb80ed1b, type: 3} + m_Name: MathEditor + m_EditorClassIdentifier: + nodeData: + rid: -2 + nodePos: + serializedVersion: 2 + x: 0 + y: 0 + width: 0 + height: 0 + references: + version: 2 + RefIds: + - rid: -2 + type: {class: , ns: , asm: } diff --git a/Sample/MathGraph/Editor/MathEditor.asset.meta b/Sample/MathGraph/Editor/MathEditor.asset.meta new file mode 100644 index 0000000..58b227a --- /dev/null +++ b/Sample/MathGraph/Editor/MathEditor.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: acb7b8ebcdf5f4f40bccf1e405c94da3 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph/Editor/MathEditor.cs b/Sample/MathGraph/Editor/MathEditor.cs new file mode 100644 index 0000000..75ae61d --- /dev/null +++ b/Sample/MathGraph/Editor/MathEditor.cs @@ -0,0 +1,22 @@ +using TNode.Editor; +using UnityEditor; +using UnityEditor.Callbacks; +using UnityEditor.Experimental.GraphView; +using UnityEngine; +using UnityEngine.UIElements; +using System; +public class MathEditor : GraphEditor{ + [OnOpenAsset] + public static bool OnOpenAsset(int instanceID, int line){ + var graph = EditorUtility.InstanceIDToObject(instanceID) as MathGraph; + if (graph != null) + { + var wnd = GetWindow(); + wnd.titleContent = new GUIContent("MathGraph Editor"); + wnd.CreateGUI(); + wnd._graphView.Data = graph; + return true; + } + return false; + } +} \ No newline at end of file diff --git a/Sample/MathGraph/Editor/MathEditor.cs.meta b/Sample/MathGraph/Editor/MathEditor.cs.meta new file mode 100644 index 0000000..49b41e3 --- /dev/null +++ b/Sample/MathGraph/Editor/MathEditor.cs.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: c9041cb574597424fa4124edc3f99af1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: + - m_ViewDataDictionary: {instanceID: 0} + - mVisualTreeAsset: {fileID: 9197481963319205126, guid: b67f6dcbe2361b649ad2b7845207321b, type: 3} + - nodeEditorData: {fileID: 11400000, guid: acb7b8ebcdf5f4f40bccf1e405c94da3, type: 2} + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph/Editor/MathGraphView.cs b/Sample/MathGraph/Editor/MathGraphView.cs new file mode 100644 index 0000000..730dfab --- /dev/null +++ b/Sample/MathGraph/Editor/MathGraphView.cs @@ -0,0 +1,9 @@ +using TNode.Models; +using TNode.Attribute; +using TNode.Editor.BaseViews; +[NodeComponent] +public class MathGraphView : DataGraphView{ + + + +} \ No newline at end of file diff --git a/Sample/MathGraph/Editor/MathGraphView.cs.meta b/Sample/MathGraph/Editor/MathGraphView.cs.meta new file mode 100644 index 0000000..c7e283a --- /dev/null +++ b/Sample/MathGraph/Editor/MathGraphView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7f4d84b648626d24eb29bfeb81c85e3f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph/MathGraph.cs b/Sample/MathGraph/MathGraph.cs new file mode 100644 index 0000000..31e37ee --- /dev/null +++ b/Sample/MathGraph/MathGraph.cs @@ -0,0 +1,9 @@ +using TNode.Models; +using UnityEngine; +using UnityEditor; +using System; +[CreateAssetMenu(fileName = "New MathGraph", menuName = "TNode/MathGraph")] +[Serializable] +public class MathGraph : GraphData{ + +} \ No newline at end of file diff --git a/Sample/MathGraph/MathGraph.cs.meta b/Sample/MathGraph/MathGraph.cs.meta new file mode 100644 index 0000000..1420f5b --- /dev/null +++ b/Sample/MathGraph/MathGraph.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 73baeb2c71a23da4ca06e3e3e52d5a78 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Sample/MathGraph/New MathGraph.asset b/Sample/MathGraph/New MathGraph.asset new file mode 100644 index 0000000..154660b --- /dev/null +++ b/Sample/MathGraph/New MathGraph.asset @@ -0,0 +1,23 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 73baeb2c71a23da4ca06e3e3e52d5a78, type: 3} + m_Name: New MathGraph + m_EditorClassIdentifier: + nodes: [] + nodeLinks: [] + entryNode: + rid: -2 + references: + version: 2 + RefIds: + - rid: -2 + type: {class: , ns: , asm: } diff --git a/Sample/MathGraph/New MathGraph.asset.meta b/Sample/MathGraph/New MathGraph.asset.meta new file mode 100644 index 0000000..041e2b1 --- /dev/null +++ b/Sample/MathGraph/New MathGraph.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4b23c513fb78ea44b8a11a0bf7c8479e +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/TNode/Attribute/DisableOnInspector.cs b/TNode/Attribute/DisableOnInspector.cs new file mode 100644 index 0000000..841963c --- /dev/null +++ b/TNode/Attribute/DisableOnInspector.cs @@ -0,0 +1,11 @@ +using System; + +namespace TNode.Attribute{ + /// + /// Use this attribute to mark a field as disabled.An disable field will not be edit by the inspector. + /// + [AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] + public class DisableOnInspectorAttribute:System.Attribute{ + + } +} \ No newline at end of file diff --git a/TNode/Attribute/DisableOnInspector.cs.meta b/TNode/Attribute/DisableOnInspector.cs.meta new file mode 100644 index 0000000..433c224 --- /dev/null +++ b/TNode/Attribute/DisableOnInspector.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e3d9cad2647b42dc8db0ce63f1291343 +timeCreated: 1656669029 \ No newline at end of file diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index f6c94c0..23e78fa 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.cs @@ -4,6 +4,7 @@ using System.Linq; using TNode.BaseViews; using TNode.Cache; using TNode.Editor.Inspector; +using TNode.Editor.Model; using TNode.Models; using UnityEditor; using UnityEditor.Experimental.GraphView; @@ -189,6 +190,14 @@ namespace TNode.Editor.BaseViews{ this.Add(miniMap); miniMap.SetPosition(rect); } + + public void CreateBlackBoard(){ + var blackboard = new Blackboard(); + //Set black board to left side of the view + blackboard.SetPosition(new Rect(0,0,200,600)); + this.Add(blackboard); + + } public virtual void DestroyInspector(){ if(_nodeInspector!=null){ this.Remove(_nodeInspector); @@ -204,6 +213,34 @@ namespace TNode.Editor.BaseViews{ } } + public void SaveEditorData(GraphEditorData graphEditorData){ + graphEditorData.nodesData.Clear(); + //iterator nodes + + foreach (var node in this.nodes){ + var nodeEditorData = new NodeEditorData{ + nodePos = node.GetPosition(), + }; + if (node is INodeView nodeView){ + nodeEditorData.nodeGuid = nodeView.GetNodeData().id; + } + graphEditorData.nodesData.Add(nodeEditorData); + } + } + public void LoadEditorData(GraphEditorData graphEditorData){ + //Load node position + foreach (var nodeEditorData in graphEditorData.nodesData){ + + var node = this.nodes.Select(x => x as INodeView).First(x=>x?.GetNodeData().id==nodeEditorData.nodeGuid); + + if (node != null){ + ((GraphElement)node).SetPosition(nodeEditorData.nodePos); + } + } + } + public void SaveWithEditorData(GraphEditorData graphEditorData){ + SaveEditorData(graphEditorData); + } public override List GetCompatiblePorts(Port startPort, NodeAdapter nodeAdapter){ return ports.Where(x => x.portType == startPort.portType).ToList(); } diff --git a/TNode/Editor/GraphEditor.cs b/TNode/Editor/GraphEditor.cs index 6cb646b..be8fb71 100644 --- a/TNode/Editor/GraphEditor.cs +++ b/TNode/Editor/GraphEditor.cs @@ -19,7 +19,7 @@ namespace TNode.Editor{ [SerializeField] private VisualTreeAsset mVisualTreeAsset = default; //Persist editor data ,such as node position,node size ,etc ,in this script object - public NodeEditorData nodeEditorData; + public GraphEditorData nodeEditorData; public void CreateGUI(){ diff --git a/TNode/Editor/Inspector/NodeInspector.cs b/TNode/Editor/Inspector/NodeInspector.cs index ca101db..e4d2ad4 100644 --- a/TNode/Editor/Inspector/NodeInspector.cs +++ b/TNode/Editor/Inspector/NodeInspector.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using TNode.Attribute; using TNode.BaseViews; using TNode.Editor.BaseViews; using TNode.Models; @@ -54,12 +55,16 @@ namespace TNode.Editor.Inspector{ if (methodInfo != null){ var genericMethod = methodInfo.MakeGenericMethod(type); var createdItem = genericMethod.Invoke(inspectorItemFactory,null) as VisualElement; - body.Add(createdItem); if (createdItem is INodeDataBindingBase castedItem){ castedItem.BindingNodeData = _data; castedItem.BindingPath = bindingPath; } + + //Check if field has DisableOnInspector attribute and if so,disable it + if (field.GetCustomAttribute() != null){ + createdItem?.SetEnabled(false); + } } } } diff --git a/TNode/Editor/Model/NodeEditorData.cs b/TNode/Editor/Model/NodeEditorData.cs index 2b56e64..9f38640 100644 --- a/TNode/Editor/Model/NodeEditorData.cs +++ b/TNode/Editor/Model/NodeEditorData.cs @@ -1,12 +1,13 @@ using System; using TNode.Models; using UnityEngine; +using UnityEngine.Serialization; namespace TNode.Editor.Model{ [Serializable] - public class NodeEditorData:ScriptableObject{ - [SerializeReference] public NodeData nodeData; + public class NodeEditorData{ + public string nodeGuid; public Rect nodePos; } } \ No newline at end of file diff --git a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs index ebbfe41..1248939 100644 --- a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs +++ b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs @@ -143,17 +143,17 @@ namespace TNode.Editor.Tools.GraphEditorCreator{ } //Create an NodeAttribute Editor Data Instance for the new graph editor - NodeEditorData nodeEditorData = ScriptableObject.CreateInstance(); - nodeEditorData.name = editorName; + var graphEditorData = ScriptableObject.CreateInstance(); + graphEditorData.name = editorName; VisualTreeAsset defaultEditorTree = Resources.Load("GraphEditor"); - EditorUtility.SetDirty(nodeEditorData); + EditorUtility.SetDirty(graphEditorData); //Save it at the same folder as the new graph editor - string nodeEditorDataPath = Path.Combine(path, editorName + ".asset"); - AssetDatabase.CreateAsset(nodeEditorData, nodeEditorDataPath); + string grapEditorPath = Path.Combine(path, editorName + ".asset"); + AssetDatabase.CreateAsset(graphEditorData, grapEditorPath); //Wait for the new file to be imported - while (!AssetDatabase.LoadAssetAtPath(nodeEditorDataPath)){ + while (!AssetDatabase.LoadAssetAtPath(grapEditorPath)){ EditorUtility.DisplayProgressBar("Generating Graph Editor", "Please wait while the new graph editor is being imported", 0.5f); EditorApplication.update(); @@ -164,8 +164,8 @@ namespace TNode.Editor.Tools.GraphEditorCreator{ MonoImporter monoImporter = AssetImporter.GetAtPath(editorPath) as MonoImporter; if (monoImporter != null) - monoImporter.SetDefaultReferences(new string[]{"nodeEditorData", "mVisualTreeAsset"}, - new Object[]{nodeEditorData, defaultEditorTree}); + monoImporter.SetDefaultReferences(new string[]{"graphEditorData", "mVisualTreeAsset"}, + new Object[]{graphEditorData, defaultEditorTree}); //Refresh the asset ann close it diff --git a/TNode/Models/NodeData.cs b/TNode/Models/NodeData.cs index 55faf3f..14b6449 100644 --- a/TNode/Models/NodeData.cs +++ b/TNode/Models/NodeData.cs @@ -16,8 +16,9 @@ namespace TNode.Models{ public NodeData() : base(){ //Object Registration + id = Guid.NewGuid().ToString(); } - + [DisableOnInspector] public string id; public string nodeName; public bool entryPoint;