From 6efb4d796883b9d0cfdea000ca2ad0012a77299e Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Wed, 13 Jul 2022 12:23:29 +0800 Subject: [PATCH 1/4] refactor: again refactor this --- TNode/Editor/Blackboard.meta | 3 +++ TNode/Editor/Blackboard/IBlackboardView.cs | 7 +++++++ TNode/Editor/Blackboard/IBlackboardView.cs.meta | 3 +++ TNode/Editor/NodeGraphView/IBaseDataGraphView.cs | 4 +++- TNode/Editor/NodeGraphView/IDataGraphView.cs | 2 +- .../Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs | 1 - .../GraphEditorCreator/SourceGeneratorForGraphEditor.cs | 7 +------ {TNode => TNodeGraphViewImpl}/Editor/Cache.meta | 0 .../Editor/Cache/NodeEditorExtensions.cs | 4 +--- .../Editor/Cache/NodeEditorExtensions.cs.meta | 0 {TNode => TNodeGraphViewImpl}/Editor/GraphEditor.cs | 2 +- {TNode => TNodeGraphViewImpl}/Editor/GraphEditor.cs.meta | 0 TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs | 4 ++-- .../Editor/Search/NodeSearchWindowProvider.cs | 2 +- 14 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 TNode/Editor/Blackboard.meta create mode 100644 TNode/Editor/Blackboard/IBlackboardView.cs create mode 100644 TNode/Editor/Blackboard/IBlackboardView.cs.meta rename {TNode => TNodeGraphViewImpl}/Editor/Cache.meta (100%) rename {TNode => TNodeGraphViewImpl}/Editor/Cache/NodeEditorExtensions.cs (99%) rename {TNode => TNodeGraphViewImpl}/Editor/Cache/NodeEditorExtensions.cs.meta (100%) rename {TNode => TNodeGraphViewImpl}/Editor/GraphEditor.cs (98%) rename {TNode => TNodeGraphViewImpl}/Editor/GraphEditor.cs.meta (100%) diff --git a/TNode/Editor/Blackboard.meta b/TNode/Editor/Blackboard.meta new file mode 100644 index 0000000..ab8049d --- /dev/null +++ b/TNode/Editor/Blackboard.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bf3b6f6e73b647e2a5421f482a0b0e8f +timeCreated: 1657686050 \ No newline at end of file diff --git a/TNode/Editor/Blackboard/IBlackboardView.cs b/TNode/Editor/Blackboard/IBlackboardView.cs new file mode 100644 index 0000000..6a7e0f0 --- /dev/null +++ b/TNode/Editor/Blackboard/IBlackboardView.cs @@ -0,0 +1,7 @@ +namespace TNode.Editor.Blackboard{ + public interface IBlackboardView{ + public void AddData(){ + + } + } +} \ No newline at end of file diff --git a/TNode/Editor/Blackboard/IBlackboardView.cs.meta b/TNode/Editor/Blackboard/IBlackboardView.cs.meta new file mode 100644 index 0000000..d57a0dd --- /dev/null +++ b/TNode/Editor/Blackboard/IBlackboardView.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9c618ca8a5b0444284489b8a1942af91 +timeCreated: 1657686059 \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs index e851505..4800d8b 100644 --- a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs +++ b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs @@ -5,7 +5,9 @@ namespace TNode.Editor.NodeGraphView{ public interface IBaseDataGraphView{ public void AddTNode(NodeData nodeData, Rect rect); public void RemoveTNode(NodeData nodeData); - + public BlackboardData GetBlackboardData(); + + } } \ No newline at end of file diff --git a/TNode/Editor/NodeGraphView/IDataGraphView.cs b/TNode/Editor/NodeGraphView/IDataGraphView.cs index db2d69b..e0ed67b 100644 --- a/TNode/Editor/NodeGraphView/IDataGraphView.cs +++ b/TNode/Editor/NodeGraphView/IDataGraphView.cs @@ -2,6 +2,6 @@ namespace TNode.Editor.NodeGraphView{ public interface IDataGraphView : IBaseDataGraphView where T:GraphData{ - + public T Data{ get; set; } } } \ No newline at end of file diff --git a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs index 1248939..145792a 100644 --- a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs +++ b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs @@ -1,6 +1,5 @@ using System.IO; using System.Text.RegularExpressions; -using TNode.Editor.Model; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; diff --git a/TNode/Editor/Tools/GraphEditorCreator/SourceGeneratorForGraphEditor.cs b/TNode/Editor/Tools/GraphEditorCreator/SourceGeneratorForGraphEditor.cs index 8e4eeae..7529026 100644 --- a/TNode/Editor/Tools/GraphEditorCreator/SourceGeneratorForGraphEditor.cs +++ b/TNode/Editor/Tools/GraphEditorCreator/SourceGeneratorForGraphEditor.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Text.RegularExpressions; -using Microsoft.CodeAnalysis; +using System.Text.RegularExpressions; using UnityEngine; namespace TNode.Editor.Tools.GraphEditorCreator{ diff --git a/TNode/Editor/Cache.meta b/TNodeGraphViewImpl/Editor/Cache.meta similarity index 100% rename from TNode/Editor/Cache.meta rename to TNodeGraphViewImpl/Editor/Cache.meta diff --git a/TNode/Editor/Cache/NodeEditorExtensions.cs b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs similarity index 99% rename from TNode/Editor/Cache/NodeEditorExtensions.cs rename to TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs index 4f2de04..856f0c1 100644 --- a/TNode/Editor/Cache/NodeEditorExtensions.cs +++ b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs @@ -3,16 +3,14 @@ using System.Collections.Generic; using System.Linq; using TNode.Attribute; using TNode.Editor; -using TNode.Editor.Inspector; using TNode.Editor.NodeViews; using TNode.Models; using TNodeGraphViewImpl.Editor.GraphBlackboard; using TNodeGraphViewImpl.Editor.NodeGraphView; using UnityEditor.Experimental.GraphView; using UnityEngine; -using UnityEngine.TestTools.Utils; -namespace TNode.Cache{ +namespace TNodeGraphViewImpl.Editor.Cache{ /// /// Internal singleton class for caching TNode reflection Data. /// diff --git a/TNode/Editor/Cache/NodeEditorExtensions.cs.meta b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs.meta similarity index 100% rename from TNode/Editor/Cache/NodeEditorExtensions.cs.meta rename to TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs.meta diff --git a/TNode/Editor/GraphEditor.cs b/TNodeGraphViewImpl/Editor/GraphEditor.cs similarity index 98% rename from TNode/Editor/GraphEditor.cs rename to TNodeGraphViewImpl/Editor/GraphEditor.cs index 93a272e..59c4b11 100644 --- a/TNode/Editor/GraphEditor.cs +++ b/TNodeGraphViewImpl/Editor/GraphEditor.cs @@ -1,8 +1,8 @@ using Codice.CM.Common; -using TNode.Cache; using TNode.Editor.Inspector; using TNode.Editor.Model; using TNode.Models; +using TNodeGraphViewImpl.Editor.Cache; using TNodeGraphViewImpl.Editor.NodeGraphView; using UnityEditor; using UnityEditor.Experimental.GraphView; diff --git a/TNode/Editor/GraphEditor.cs.meta b/TNodeGraphViewImpl/Editor/GraphEditor.cs.meta similarity index 100% rename from TNode/Editor/GraphEditor.cs.meta rename to TNodeGraphViewImpl/Editor/GraphEditor.cs.meta diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index bcc280c..0a46a2b 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; -using TNode.Cache; using TNode.Editor; using TNode.Editor.Inspector; using TNode.Editor.Model; @@ -12,6 +11,7 @@ using TNode.Editor.NodeViews; using TNode.Editor.Search; using TNode.Editor.Tools.NodeCreator; using TNode.Models; +using TNodeGraphViewImpl.Editor.Cache; using TNodeGraphViewImpl.Editor.GraphBlackboard; using TNodeGraphViewImpl.Editor.GraphBlackboard.BlackboardProperty; using UnityEditor; @@ -21,7 +21,7 @@ using UnityEngine.UIElements; using Edge = UnityEditor.Experimental.GraphView.Edge; namespace TNodeGraphViewImpl.Editor.NodeGraphView{ - public abstract class BaseDataGraphView:GraphView,IBaseDataGraphView where T:GraphData{ + public abstract class BaseDataGraphView:GraphView,IDataGraphView where T:GraphData{ #region variables and properties private T _data; private bool _isInspectorOn; diff --git a/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs b/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs index 058d07b..5ef4ced 100644 --- a/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs +++ b/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -using TNode.Cache; using TNode.Editor.NodeGraphView; using TNode.Editor.Tools.NodeCreator; using TNode.Models; +using TNodeGraphViewImpl.Editor.Cache; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; From 02467dc0731a5207afd81ccdcaeab75d16f6eae9 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Wed, 13 Jul 2022 13:21:41 +0800 Subject: [PATCH 2/4] feat:create search window at right position --- TNode/Editor/Blackboard/IBlackboardView.cs | 20 ++++-- .../EditorPersistence/GraphEditorData.cs | 5 +- .../GraphElementEditorData.cs | 4 +- .../NodeGraphView/IBaseDataGraphView.cs | 2 + .../GraphEditorCreator/GraphEditorCreator.cs | 1 + .../Editor/Cache/NodeEditorExtensions.cs | 9 +-- .../DefaultGraphBlackboardView.cs | 38 ++++++++--- .../GraphBlackboard/GraphBlackboardView.cs | 45 +++++++++++-- TNodeGraphViewImpl/Editor/GraphEditor.cs | 2 +- .../Editor/NodeGraphView/DataGraphView.cs | 66 ++++++++----------- 10 files changed, 124 insertions(+), 68 deletions(-) diff --git a/TNode/Editor/Blackboard/IBlackboardView.cs b/TNode/Editor/Blackboard/IBlackboardView.cs index 6a7e0f0..eca1cf9 100644 --- a/TNode/Editor/Blackboard/IBlackboardView.cs +++ b/TNode/Editor/Blackboard/IBlackboardView.cs @@ -1,7 +1,19 @@ -namespace TNode.Editor.Blackboard{ +using TNode.Editor.NodeGraphView; +using TNode.Models; +using UnityEditor; +using UnityEngine; + +namespace TNode.Editor.Blackboard{ public interface IBlackboardView{ - public void AddData(){ - - } + public BlackboardData GetBlackboardData(); + public void SetBlackboardData(BlackboardData data); + + public void AddItem(); + + void Setup(IBaseDataGraphView graphView,EditorWindow ownerWindow); + } + public interface IBlackboardView : IBlackboardView where T : BlackboardData{ + + public T Data{ get; set; } } } \ No newline at end of file diff --git a/TNode/Editor/EditorPersistence/GraphEditorData.cs b/TNode/Editor/EditorPersistence/GraphEditorData.cs index 02fc71c..9c64722 100644 --- a/TNode/Editor/EditorPersistence/GraphEditorData.cs +++ b/TNode/Editor/EditorPersistence/GraphEditorData.cs @@ -1,10 +1,7 @@ using System.Collections.Generic; -using TNode.Editor.EditorPersistence; -using TNode.Editor.Model; using UnityEngine; -using UnityEngine.Serialization; -namespace TNode.Editor{ +namespace TNode.Editor.EditorPersistence{ [CreateAssetMenu(fileName = "Graph Editor Data", menuName = "TNode/Graph Editor Data")] public class GraphEditorData:ScriptableObject{ diff --git a/TNode/Editor/EditorPersistence/GraphElementEditorData.cs b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs index f21170d..6c8809a 100644 --- a/TNode/Editor/EditorPersistence/GraphElementEditorData.cs +++ b/TNode/Editor/EditorPersistence/GraphElementEditorData.cs @@ -1,9 +1,7 @@ using System; -using TNode.Models; using UnityEngine; -using UnityEngine.Serialization; -namespace TNode.Editor.Model{ +namespace TNode.Editor.EditorPersistence{ [Serializable] public class GraphElementEditorData{ diff --git a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs index 4800d8b..16289d0 100644 --- a/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs +++ b/TNode/Editor/NodeGraphView/IBaseDataGraphView.cs @@ -6,6 +6,8 @@ namespace TNode.Editor.NodeGraphView{ public void AddTNode(NodeData nodeData, Rect rect); public void RemoveTNode(NodeData nodeData); + public void CreateBlackboard(); + public GraphData GetGraphData(); public BlackboardData GetBlackboardData(); diff --git a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs index 145792a..13c2ed3 100644 --- a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs +++ b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs @@ -1,5 +1,6 @@ using System.IO; using System.Text.RegularExpressions; +using TNode.Editor.EditorPersistence; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; diff --git a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs index 856f0c1..7624080 100644 --- a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs +++ b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using TNode.Attribute; using TNode.Editor; +using TNode.Editor.Blackboard; using TNode.Editor.NodeViews; using TNode.Models; using TNodeGraphViewImpl.Editor.GraphBlackboard; @@ -151,14 +152,14 @@ namespace TNodeGraphViewImpl.Editor.Cache{ return null; } - public static Blackboard CreateBlackboardDataFromBlackboardDataType(Type t){ + public static IBlackboardView CreateBlackboardDataFromBlackboardDataType(Type t){ var type = typeof(GraphBlackboardView<>).MakeGenericType(t); - var res = CreateViewComponentFromBaseType(type) as Blackboard; + var res = CreateViewComponentFromBaseType(type) as IBlackboardView; return res ?? new DefaultGraphBlackboardView(); } - public static Blackboard CreateBlackboardWithGraphData(GraphData graphData){ + public static IBlackboardView CreateBlackboardWithGraphData(GraphData graphData){ var graphType = graphData.GetType(); if (NodeEditorSingleton.Instance.GraphBlackboard.ContainsKey(graphType)){ var type = NodeEditorSingleton.Instance.GraphBlackboard[graphType]; @@ -167,7 +168,7 @@ namespace TNodeGraphViewImpl.Editor.Cache{ } return null; } - public static Blackboard CreateBlackboardWithGraphData(Type graphType){ + public static IBlackboardView CreateBlackboardWithGraphData(Type graphType){ if (NodeEditorSingleton.Instance.GraphBlackboard.ContainsKey(graphType)){ var type = NodeEditorSingleton.Instance.GraphBlackboard[graphType]; return CreateBlackboardDataFromBlackboardDataType(type); diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 2ed561f..4c0d01d 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -1,17 +1,39 @@ -using TNode.Attribute; +using System.Collections; +using System.Reflection; +using TNode.Attribute; +using TNode.Editor.NodeGraphView; +using TNode.Editor.Search; using TNode.Models; +using UnityEditor; +using UnityEditor.Experimental.GraphView; +using UnityEngine; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ [ViewComponent] public class DefaultGraphBlackboardView:GraphBlackboardView{ - public DefaultGraphBlackboardView(){ - - } - public void ConstructView(){ - - } - public void AddParameter(){ + protected override void UpdateBlackboard(BlackboardData data){ + + foreach (var field in data.GetType() + .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ + //if the field is MonoBehaviour,add a property field for blackboard + //skip if the field is a list or Ilist + if (!typeof(IList).IsAssignableFrom(field.FieldType)){ + var propertyField = new BlackboardPropertyField(new BlackboardProperty.BlackboardProperty(field.Name,field.FieldType)); + this.Add(propertyField); + } + } + this.addItemRequested = (sender) => { + var res = ScriptableObject.CreateInstance(); + + //Get right top corner of the blackboard + var blackboardPos = GetPosition().position+OwnerWindow.position.position; + var searchWindowContext = new SearchWindowContext(blackboardPos,200,200); + //Call search window + res.Setup(Owner.GetGraphData().GetType(),Owner,OwnerWindow); + SearchWindow.Open(searchWindowContext, res); + }; } + } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs index 05fe3f5..8c2644d 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardView.cs @@ -1,15 +1,52 @@ -using TNode.Models; +using TNode.Editor.Blackboard; +using TNode.Editor.NodeGraphView; +using TNode.Editor.Search; +using TNode.Models; +using UnityEditor; using UnityEditor.Experimental.GraphView; +using UnityEngine; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ /// /// Implement this class to create graph black board for specified graph /// - public class GraphBlackboardView:Blackboard where T:BlackboardData{ - public T BlackboardData; + public class GraphBlackboardView:Blackboard,IBlackboardView where T:BlackboardData{ + protected IBaseDataGraphView Owner; + protected EditorWindow OwnerWindow; + private T _data; - public GraphBlackboardView() : base(){ + public void Setup(IBaseDataGraphView graphView,EditorWindow ownerWindow){ + Owner = graphView; + OwnerWindow = ownerWindow; + } + + + + public new void SetPosition(Rect rect){ } + + protected virtual void UpdateBlackboard(BlackboardData data){ + + } + public T Data{ + get => (T) _data; + + set{ + _data = value; + UpdateBlackboard(value); + } + } + public BlackboardData GetBlackboardData(){ + return _data; + } + + public void SetBlackboardData(BlackboardData data){ + Data = (T) data; + } + + public void AddItem(){ + + } } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/GraphEditor.cs b/TNodeGraphViewImpl/Editor/GraphEditor.cs index 59c4b11..ade213b 100644 --- a/TNodeGraphViewImpl/Editor/GraphEditor.cs +++ b/TNodeGraphViewImpl/Editor/GraphEditor.cs @@ -1,6 +1,6 @@ using Codice.CM.Common; +using TNode.Editor.EditorPersistence; using TNode.Editor.Inspector; -using TNode.Editor.Model; using TNode.Models; using TNodeGraphViewImpl.Editor.Cache; using TNodeGraphViewImpl.Editor.NodeGraphView; diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index 0a46a2b..ec9a3ac 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -4,8 +4,9 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using TNode.Editor; +using TNode.Editor.Blackboard; +using TNode.Editor.EditorPersistence; using TNode.Editor.Inspector; -using TNode.Editor.Model; using TNode.Editor.NodeGraphView; using TNode.Editor.NodeViews; using TNode.Editor.Search; @@ -29,7 +30,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ private NodeInspector _nodeInspector; public GraphEditor Owner; private Dictionary _nodeDict = new(); - private Blackboard _blackboard; + private IBlackboardView _blackboard; public T Data{ get{ return _data; } set{ @@ -191,47 +192,14 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ miniMap.SetPosition(rect); } - public virtual void CreateBlackboard(){ - - _blackboard = NodeEditorExtensions.CreateBlackboardWithGraphData(typeof(T)); - - _blackboard.SetPosition(new Rect(0,0,200,600)); - Add(_blackboard); - - OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; - - } private void BlackboardUpdate(){ if (_data.blackboardData == null || _data.blackboardData.GetType() == typeof(BlackboardData)){ _data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType()); if (_data.blackboardData == null) return; + _blackboard.SetBlackboardData(_data.blackboardData); } - - //Iterate field of the blackboard and add a button for each field - foreach (var field in _data.blackboardData.GetType() - .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ - //if the field is MonoBehaviour,add a property field for blackboard - //skip if the field is a list or Ilist - if (!typeof(IList).IsAssignableFrom(field.FieldType)){ - var propertyField = new BlackboardPropertyField(new BlackboardProperty(field.Name,field.FieldType)); - _blackboard.Add(propertyField); - } - - } - _blackboard.addItemRequested = (sender) => { - var res = ScriptableObject.CreateInstance(); - - //Get right top corner of the blackboard - var blackboardPos = _blackboard.GetPosition().position; - var searchWindowContext = new SearchWindowContext(blackboardPos,200,200); - //Call search window - res.Setup(typeof(T),this,Owner); - - SearchWindow.Open(searchWindowContext, res); - }; - } public virtual void DestroyInspector(){ @@ -330,10 +298,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ OnGraphViewDestroy(); } - public bool IsDroppable(){ - return true; - } - + #region implement interfaces public void AddTNode(NodeData nodeData, Rect rect){ if (NodeEditorExtensions.CreateNodeViewFromNodeType(nodeData.GetType()) is Node nodeView){ nodeView.SetPosition(rect); @@ -381,9 +346,30 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData.id); } + public void CreateBlackboard(){ + _blackboard = NodeEditorExtensions.CreateBlackboardWithGraphData(typeof(T)); + _blackboard.Setup(this,Owner); + + var castedBlackboard = _blackboard as Blackboard; + + Add(castedBlackboard); + + Rect blackboardPos = new Rect(0,0,200,700); + castedBlackboard?.SetPosition(blackboardPos); + + + OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; + } + + public GraphData GetGraphData(){ + return _data; + } + + public BlackboardData GetBlackboardData(){ return this._data.blackboardData; } + #endregion } From 69346b388bdb83459726bd835033dacb03eeb91f Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Wed, 13 Jul 2022 16:44:41 +0800 Subject: [PATCH 3/4] fix: better serialization --- TNode/Attribute/BlackboardSection.cs | 16 --- TNode/Attribute/BlackboardSection.cs.meta | 3 - TNode/Editor/Serialization.meta | 3 + .../Serialization/BlackboardDataWrapper.cs | 10 ++ .../BlackboardDataWrapper.cs.meta | 3 + TNode/Editor/Serialization/DataWrapper.cs | 53 +++++++++ .../Editor/Serialization/DataWrapper.cs.meta | 3 + TNode/Editor/Serialization/NodeDataWrapper.cs | 110 ++++++++++++++++++ .../Serialization}/NodeDataWrapper.cs.meta | 0 TNode/JsonSerialize/JsonSerializeTool.cs | 2 +- TNode/JsonSerialize/UnityObjectConverter.cs | 24 ++++ .../UnityObjectConverter.cs.meta | 3 + TNode/Models/BlackboardData.cs | 4 +- TNode/Models/GraphData.cs | 51 ++++++-- TNode/Tools.meta | 3 - TNode/Tools/NodeDataWrapper.cs | 90 -------------- .../Editor/Cache/NodeEditorExtensions.cs | 4 +- .../DefaultGraphBlackboardView.cs | 26 ++++- .../Editor/Inspector/NodeInspector.cs | 1 + .../Editor/Inspector/NodeInspectorInNode.cs | 5 +- .../Editor/NodeGraphView/DataGraphView.cs | 22 ++-- .../Editor/NodeViews/DefaultNodeView.cs | 1 + .../Editor/NodeViews/DragNodeView.cs | 1 + .../Editor/NodeViews/NodeView.cs | 7 +- 24 files changed, 299 insertions(+), 146 deletions(-) delete mode 100644 TNode/Attribute/BlackboardSection.cs delete mode 100644 TNode/Attribute/BlackboardSection.cs.meta create mode 100644 TNode/Editor/Serialization.meta create mode 100644 TNode/Editor/Serialization/BlackboardDataWrapper.cs create mode 100644 TNode/Editor/Serialization/BlackboardDataWrapper.cs.meta create mode 100644 TNode/Editor/Serialization/DataWrapper.cs create mode 100644 TNode/Editor/Serialization/DataWrapper.cs.meta create mode 100644 TNode/Editor/Serialization/NodeDataWrapper.cs rename TNode/{Tools => Editor/Serialization}/NodeDataWrapper.cs.meta (100%) create mode 100644 TNode/JsonSerialize/UnityObjectConverter.cs create mode 100644 TNode/JsonSerialize/UnityObjectConverter.cs.meta delete mode 100644 TNode/Tools.meta delete mode 100644 TNode/Tools/NodeDataWrapper.cs diff --git a/TNode/Attribute/BlackboardSection.cs b/TNode/Attribute/BlackboardSection.cs deleted file mode 100644 index bbefd7a..0000000 --- a/TNode/Attribute/BlackboardSection.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using JetBrains.Annotations; - -namespace TNode.Attribute{ - - /// - /// Use this attribute to declare a blackboard section ,a blackboard section is a group of variables with same types - /// - [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] - [BaseTypeRequired(typeof(List<>))] - - public class BlackboardSection:System.Attribute{ - - } -} \ No newline at end of file diff --git a/TNode/Attribute/BlackboardSection.cs.meta b/TNode/Attribute/BlackboardSection.cs.meta deleted file mode 100644 index 1794987..0000000 --- a/TNode/Attribute/BlackboardSection.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 773d073006dc4dd488e18b38165efd5a -timeCreated: 1656942977 \ No newline at end of file diff --git a/TNode/Editor/Serialization.meta b/TNode/Editor/Serialization.meta new file mode 100644 index 0000000..03f9a0e --- /dev/null +++ b/TNode/Editor/Serialization.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4c1e0017367a4d448a68ed34b7540782 +timeCreated: 1657690936 \ No newline at end of file diff --git a/TNode/Editor/Serialization/BlackboardDataWrapper.cs b/TNode/Editor/Serialization/BlackboardDataWrapper.cs new file mode 100644 index 0000000..5d05035 --- /dev/null +++ b/TNode/Editor/Serialization/BlackboardDataWrapper.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using TNode.Models; +using UnityEngine; + +namespace TNode.Editor.Serialization{ + public class BlackboardDataWrapper:DataWrapper{ + + } +} \ No newline at end of file diff --git a/TNode/Editor/Serialization/BlackboardDataWrapper.cs.meta b/TNode/Editor/Serialization/BlackboardDataWrapper.cs.meta new file mode 100644 index 0000000..774afc6 --- /dev/null +++ b/TNode/Editor/Serialization/BlackboardDataWrapper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ac3fada821244e69b6b9a27a7b94eeee +timeCreated: 1657691334 \ No newline at end of file diff --git a/TNode/Editor/Serialization/DataWrapper.cs b/TNode/Editor/Serialization/DataWrapper.cs new file mode 100644 index 0000000..7fed251 --- /dev/null +++ b/TNode/Editor/Serialization/DataWrapper.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using TNode.Models; +using UnityEngine; + +namespace TNode.Editor.Serialization{ + [Serializable] + public class DataWrapper:ScriptableObject where TWrapper:DataWrapper where TData:IModel,new(){ + [SerializeReference] + public TData data; + private static readonly Dictionary Cache = new (); + public static TWrapper Get(TData data){ + if (data.GetType().IsGenericType){ + return CreateInstance(); + } + if(Cache.ContainsKey(data)){ + return Cache[data]; + } + var wrapper = CreateInstance(); + wrapper.data = data; + Cache.Add(data,wrapper); + return wrapper; + } + public event Action> OnValueChanged; + + public void SetValue(string path, object value){ + var fieldInfo = data.GetType().GetField(path); + fieldInfo.SetValue(data,value); + OnValueChanged?.Invoke(this); + } + + public object GetValue(string path){ + var fieldInfo = data.GetType().GetField(path); + return fieldInfo.GetValue(data); + } + public static implicit operator TData(DataWrapper wrapper){ + if (wrapper == null) + return default(TData); + return wrapper.data; + + } + /// + /// Use this to get the wrapped data directly. + /// + /// + /// + public static implicit operator DataWrapper(TData unWrapper){ + if (unWrapper == null) + return null; + return Get(unWrapper); + } + } +} \ No newline at end of file diff --git a/TNode/Editor/Serialization/DataWrapper.cs.meta b/TNode/Editor/Serialization/DataWrapper.cs.meta new file mode 100644 index 0000000..ec3dcc7 --- /dev/null +++ b/TNode/Editor/Serialization/DataWrapper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3b4407f1670d4359b807377900c83583 +timeCreated: 1657693507 \ No newline at end of file diff --git a/TNode/Editor/Serialization/NodeDataWrapper.cs b/TNode/Editor/Serialization/NodeDataWrapper.cs new file mode 100644 index 0000000..cff0ea0 --- /dev/null +++ b/TNode/Editor/Serialization/NodeDataWrapper.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using TNode.Models; +using UnityEngine; +using UnityEngine.Serialization; + + +namespace TNode.Editor.Serialization{ + [Obsolete] + + public class NodeDataWrapper : ScriptableObject where T : NodeData{ + public T Data; + private static readonly Dictionary> Cache = new (); + public event Action> OnValueChanged; + public static NodeDataWrapper Get(T data){ + if(Cache.ContainsKey(data)){ + return Cache[data]; + } + var wrapper = ScriptableObject.CreateInstance>(); + Cache.Add(data,wrapper); + return wrapper; + } + public NodeDataWrapper(T data){ + this.Data = data; + } + + public void SetValue(string path, object value){ + var fieldInfo = Data.GetType().GetField(path); + fieldInfo.SetValue(Data,value); + OnValueChanged?.Invoke(this); + } + + public object GetValue(string path){ + var fieldInfo = Data.GetType().GetField(path); + return fieldInfo.GetValue(Data); + } + public static implicit operator T(NodeDataWrapper wrapper){ + if (wrapper == null) + return null; + return wrapper.Data; + + } + public static implicit operator NodeDataWrapper(T unWrapper){ + if (unWrapper == null) + return null; + return Get(unWrapper); + } + } + public class NodeDataWrapper:DataWrapper{ + + } + /// + /// Scriptable object wrapper enable property drawer for t-node + /// instance create automatically when using get function,generic node data is not support yet because of unity serialization system. + /// TODO : support generic node data + /// + // public class NodeDataWrapper:ScriptableObject{ + // [SerializeReference] + // public NodeData data; + // private static readonly Dictionary Cache = new (); + // public event Action OnValueChanged; + // /// + // /// Create a new wrapper or get a cached wrapper for the given data + // /// + // /// node data,an implemented type is acceptable + // /// + // public static NodeDataWrapper Get(NodeData data){ + // if (data.GetType().IsGenericType){ + // return CreateInstance(); + // } + // if(Cache.ContainsKey(data)){ + // return Cache[data]; + // } + // var wrapper = CreateInstance(); + // wrapper.data = data; + // Cache.Add(data,wrapper); + // return wrapper; + // } + // + // + // public void SetValue(string path, object value){ + // var fieldInfo = data.GetType().GetField(path); + // fieldInfo.SetValue(data,value); + // OnValueChanged?.Invoke(this); + // } + // + // public object GetValue(string path){ + // var fieldInfo = data.GetType().GetField(path); + // return fieldInfo.GetValue(data); + // } + // public static implicit operator NodeData(NodeDataWrapper wrapper){ + // if (wrapper == null) + // return null; + // return wrapper.data; + // + // } + // /// + // /// Use this to get the wrapped data directly. + // /// + // /// + // /// + // public static implicit operator NodeDataWrapper(NodeData unWrapper){ + // if (unWrapper == null) + // return null; + // return Get(unWrapper); + // } + // } + + +} \ No newline at end of file diff --git a/TNode/Tools/NodeDataWrapper.cs.meta b/TNode/Editor/Serialization/NodeDataWrapper.cs.meta similarity index 100% rename from TNode/Tools/NodeDataWrapper.cs.meta rename to TNode/Editor/Serialization/NodeDataWrapper.cs.meta diff --git a/TNode/JsonSerialize/JsonSerializeTool.cs b/TNode/JsonSerialize/JsonSerializeTool.cs index 58888ba..77194ac 100644 --- a/TNode/JsonSerialize/JsonSerializeTool.cs +++ b/TNode/JsonSerialize/JsonSerializeTool.cs @@ -19,7 +19,7 @@ namespace TNode.JsonSerialize{ ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore, DateFormatString = "yyyy-MM-dd HH:mm:ss", - Converters = new List { new Vector3Converter() }, + Converters = new List { new Vector3Converter(),new UnityObjectConverter() }, TypeNameHandling = TypeNameHandling.Auto, ContractResolver = new WritablePropertiesOnlyResolver(), Formatting = Formatting.Indented diff --git a/TNode/JsonSerialize/UnityObjectConverter.cs b/TNode/JsonSerialize/UnityObjectConverter.cs new file mode 100644 index 0000000..0d7555d --- /dev/null +++ b/TNode/JsonSerialize/UnityObjectConverter.cs @@ -0,0 +1,24 @@ +using System; +using Newtonsoft.Json; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace TNode.JsonSerialize{ + public class UnityObjectConverter:JsonConverter{ + public override void WriteJson(JsonWriter writer, Object value, JsonSerializer serializer){ + //Holding the object reference in a string + var go = value; + var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(go)); + writer.WriteValue(value.GetInstanceID().ToString()); + } + + public override Object ReadJson(JsonReader reader, Type objectType, Object existingValue, bool hasExistingValue, + JsonSerializer serializer){ + //Reading the object reference from the string + var guid = reader.Value.ToString(); + var go = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); + return go; + } + } +} \ No newline at end of file diff --git a/TNode/JsonSerialize/UnityObjectConverter.cs.meta b/TNode/JsonSerialize/UnityObjectConverter.cs.meta new file mode 100644 index 0000000..346f409 --- /dev/null +++ b/TNode/JsonSerialize/UnityObjectConverter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1c430c5760df439690d22ab18daa9d72 +timeCreated: 1657700744 \ No newline at end of file diff --git a/TNode/Models/BlackboardData.cs b/TNode/Models/BlackboardData.cs index fc0b66a..14cff62 100644 --- a/TNode/Models/BlackboardData.cs +++ b/TNode/Models/BlackboardData.cs @@ -1,5 +1,7 @@ -namespace TNode.Models{ +using System; +namespace TNode.Models{ + [Serializable] public class BlackboardData:IModel{ } diff --git a/TNode/Models/GraphData.cs b/TNode/Models/GraphData.cs index 3937714..8f7031b 100644 --- a/TNode/Models/GraphData.cs +++ b/TNode/Models/GraphData.cs @@ -2,37 +2,64 @@ using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; -using TNode.Editor; using TNode.JsonSerialize; -using UnityEditor.Experimental.GraphView; using UnityEngine.Serialization; namespace TNode.Models{ [Serializable] public class GraphData:ScriptableObject,ISerializationCallbackReceiver{ - [SerializeField] public Dictionary NodeDictionary = new Dictionary(); - public List nodeLinks = new(); - public BlackboardData blackboardData = new(); + + + [SerializeField] + protected List nodeLinks; [TextArea(1,10)] [SerializeField] //[HideInInspector] - private string jsonObject; + private string jsonNodeData; [TextArea(1,10)] [SerializeField] private string jsonBlackboard; + + + public BlackboardData blackboardData; + + public List NodeLinks{ + get{ + return nodeLinks ??= new List(); + + } + set => nodeLinks = value; + } + + public void OnBeforeSerialize(){ - jsonObject = JsonConvert.SerializeObject(NodeDictionary,JsonSerializeTool.JsonSerializerSettings); - jsonBlackboard = JsonConvert.SerializeObject(blackboardData,JsonSerializeTool.JsonSerializerSettings); + if (nodeLinks != null){ + jsonNodeData = JsonConvert.SerializeObject(NodeDictionary,JsonSerializeTool.JsonSerializerSettings); + + } + + if (jsonBlackboard != null){ + jsonBlackboard = JsonConvert.SerializeObject(blackboardData,typeof(object),JsonSerializeTool.JsonSerializerSettings); + + } } public void OnAfterDeserialize(){ //Deserialize node dictionary - var deserializedData = JsonConvert.DeserializeObject>(jsonObject,JsonSerializeTool.JsonSerializerSettings); + var deserializedData = JsonConvert.DeserializeObject>(jsonNodeData,JsonSerializeTool.JsonSerializerSettings); NodeDictionary = deserializedData; //Deserialize blackboard data - var deserializedBlackboard = JsonConvert.DeserializeObject(jsonBlackboard,JsonSerializeTool.JsonSerializerSettings); - blackboardData = deserializedBlackboard; - + // var deserializedBlackboard = + // JsonConvert.DeserializeObject(jsonBlackboard,JsonSerializeTool.JsonSerializerSettings); + // blackboardData = deserializedBlackboard as BlackboardData; + // Debug.Log(deserializedBlackboard); + } + + public void OnEnable(){ + var deserializedBlackboard = + JsonConvert.DeserializeObject(jsonBlackboard,JsonSerializeTool.JsonSerializerSettings); + blackboardData = deserializedBlackboard as BlackboardData; + Debug.Log(deserializedBlackboard); } } } \ No newline at end of file diff --git a/TNode/Tools.meta b/TNode/Tools.meta deleted file mode 100644 index 15e65f1..0000000 --- a/TNode/Tools.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 206b9a7ba6b54706b02c6aa2cb9a18b0 -timeCreated: 1656762017 \ No newline at end of file diff --git a/TNode/Tools/NodeDataWrapper.cs b/TNode/Tools/NodeDataWrapper.cs deleted file mode 100644 index 1170590..0000000 --- a/TNode/Tools/NodeDataWrapper.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using TNode.Models; -using UnityEngine; - -namespace TNode.Editor{ - /// - /// Scriptable object wrapper enable property drawer for t-node - /// - public class NodeDataWrapper : ScriptableObject where T : NodeData{ - public T Data; - private static readonly Dictionary> Cache = new (); - public event Action> OnValueChanged; - public static NodeDataWrapper Get(T data){ - if(Cache.ContainsKey(data)){ - return Cache[data]; - } - var wrapper = ScriptableObject.CreateInstance>(); - Cache.Add(data,wrapper); - return wrapper; - } - public NodeDataWrapper(T data){ - this.Data = data; - } - - public void SetValue(string path, object value){ - var fieldInfo = Data.GetType().GetField(path); - fieldInfo.SetValue(Data,value); - OnValueChanged?.Invoke(this); - } - - public object GetValue(string path){ - var fieldInfo = Data.GetType().GetField(path); - return fieldInfo.GetValue(Data); - } - public static implicit operator T(NodeDataWrapper wrapper){ - if (wrapper == null) - return null; - return wrapper.Data; - - } - public static implicit operator NodeDataWrapper(T unWrapper){ - if (unWrapper == null) - return null; - return Get(unWrapper); - } - } - - public class NodeDataWrapper:ScriptableObject{ - [SerializeReference] - public NodeData Data; - private static readonly Dictionary Cache = new (); - public event Action OnValueChanged; - public static NodeDataWrapper Get(NodeData data){ - if (data.GetType().IsGenericType){ - return ScriptableObject.CreateInstance(); - } - if(Cache.ContainsKey(data)){ - return Cache[data]; - } - var wrapper = ScriptableObject.CreateInstance(); - wrapper.Data = data; - Cache.Add(data,wrapper); - return wrapper; - } - - - public void SetValue(string path, object value){ - var fieldInfo = Data.GetType().GetField(path); - fieldInfo.SetValue(Data,value); - OnValueChanged?.Invoke(this); - } - - public object GetValue(string path){ - var fieldInfo = Data.GetType().GetField(path); - return fieldInfo.GetValue(Data); - } - public static implicit operator NodeData(NodeDataWrapper wrapper){ - if (wrapper == null) - return null; - return wrapper.Data; - - } - public static implicit operator NodeDataWrapper(NodeData unWrapper){ - if (unWrapper == null) - return null; - return Get(unWrapper); - } - } -} \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs index 7624080..f8fe8b6 100644 --- a/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs +++ b/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs @@ -8,6 +8,7 @@ using TNode.Editor.NodeViews; using TNode.Models; using TNodeGraphViewImpl.Editor.GraphBlackboard; using TNodeGraphViewImpl.Editor.NodeGraphView; +using TNodeGraphViewImpl.Editor.NodeViews; using UnityEditor.Experimental.GraphView; using UnityEngine; @@ -200,7 +201,6 @@ namespace TNodeGraphViewImpl.Editor.Cache{ //Check the generic type of BaseNodeView by t if (t.IsGenericType){ - Debug.Log($"A generic type {t} is detected"); //AKA if BlackboardDragNodeData is pulled //Get BlackboardDragNodeData as generic type @@ -209,14 +209,12 @@ namespace TNodeGraphViewImpl.Editor.Cache{ //What you want is a BaseNodeView> to be created var genericViewType = typeof(BaseNodeView<>).MakeGenericType(genericTypeDefinition); - Debug.Log($"The generic view type is {genericViewType}"); //search for the specific type of genericViewType in the dictionary if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(genericViewType)){ var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[genericViewType]; //The implementedType is still a generic type ,so we make it a specific type by using MakeGenericType - Debug.Log($"{implementedType}"); //Get argument type of t var argumentType = t.GetGenericArguments()[0]; var instance = Activator.CreateInstance(implementedType.MakeGenericType(argumentType)); diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 4c0d01d..80eaeb2 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -3,27 +3,45 @@ using System.Reflection; using TNode.Attribute; using TNode.Editor.NodeGraphView; using TNode.Editor.Search; +using TNode.Editor.Serialization; using TNode.Models; using UnityEditor; using UnityEditor.Experimental.GraphView; +using UnityEditor.UIElements; using UnityEngine; +using UnityEngine.UIElements; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ [ViewComponent] public class DefaultGraphBlackboardView:GraphBlackboardView{ protected override void UpdateBlackboard(BlackboardData data){ - - + var serializedObject = new SerializedObject((BlackboardDataWrapper)data); foreach (var field in data.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ //if the field is MonoBehaviour,add a property field for blackboard //skip if the field is a list or Ilist if (!typeof(IList).IsAssignableFrom(field.FieldType)){ + VisualElement visualElement = new VisualElement(); var propertyField = new BlackboardPropertyField(new BlackboardProperty.BlackboardProperty(field.Name,field.FieldType)); - this.Add(propertyField); + var foldoutData = new Foldout{ + text = field.Name + }; + var drawer = new PropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name); + drawer.Bind(serializedObject); + foldoutData.Add(drawer); + visualElement.Add(propertyField); + visualElement.Add(foldoutData); + this.Add(visualElement); + + } + else{ + var blackboardList = new BlackboardSection{ + title = field.Name + }; + this.Add(blackboardList); } } - this.addItemRequested = (sender) => { + addItemRequested = (sender) => { var res = ScriptableObject.CreateInstance(); //Get right top corner of the blackboard diff --git a/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs b/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs index 0367625..96c833e 100644 --- a/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs +++ b/TNodeGraphViewImpl/Editor/Inspector/NodeInspector.cs @@ -5,6 +5,7 @@ using TNode.Attribute; using TNode.Editor.NodeViews; using TNode.Models; using TNodeGraphViewImpl.Editor.NodeGraphView; +using TNodeGraphViewImpl.Editor.NodeViews; using Unity.VisualScripting; using UnityEditor; using UnityEditor.Experimental.GraphView; diff --git a/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs index de375a1..b0c573c 100644 --- a/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs +++ b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs @@ -1,5 +1,6 @@ using System.Reflection; using TNode.Attribute; +using TNode.Editor.Serialization; using TNode.Models; using UnityEditor; using UnityEditor.UIElements; @@ -47,11 +48,11 @@ namespace TNode.Editor.Inspector{ var showInNodeViewAttribute = field.GetCustomAttribute() != null; if (!showInNodeViewAttribute) continue; - var drawer = new PropertyField(serializedObject.FindProperty("Data").FindPropertyRelative(field.Name),field.Name); - Debug.Log(serializedObject.FindProperty("Data")); + var drawer = new PropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name); drawer.Bind(serializedObject); Add(drawer); } } + } } \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index ec9a3ac..7ef4c68 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -15,6 +15,7 @@ using TNode.Models; using TNodeGraphViewImpl.Editor.Cache; using TNodeGraphViewImpl.Editor.GraphBlackboard; using TNodeGraphViewImpl.Editor.GraphBlackboard.BlackboardProperty; +using TNodeGraphViewImpl.Editor.NodeViews; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; @@ -156,7 +157,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ AddTNode(dataNode,nodePos); } - foreach (var edge in _data.nodeLinks){ + foreach (var edge in _data.NodeLinks){ var inputNode = _data.NodeDictionary[edge.inPort.nodeDataId]; var outputNode = _data.NodeDictionary[edge.outPort.nodeDataId]; var inputNodeView = _nodeDict[inputNode.id]; @@ -194,12 +195,13 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ private void BlackboardUpdate(){ - if (_data.blackboardData == null || _data.blackboardData.GetType() == typeof(BlackboardData)){ + if (_data.blackboardData == null || _data.blackboardData.GetType()==(typeof(BlackboardData))){ _data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType()); if (_data.blackboardData == null) return; - _blackboard.SetBlackboardData(_data.blackboardData); + } + _blackboard.SetBlackboardData(_data.blackboardData); } public virtual void DestroyInspector(){ @@ -272,17 +274,23 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ } - _data.nodeLinks = links; + _data.NodeLinks = links; } private void SaveGraphData(){ _data.NodeDictionary.Clear(); - _data.nodeLinks.Clear(); + _data.NodeLinks.Clear(); SaveNode(); SaveEdge(); + SaveBlackboard(); EditorUtility.SetDirty(_data); } - + private void SaveBlackboard(){ + if (_data.blackboardData == null){ + _data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType()); + } + } + public override List GetCompatiblePorts(Port startPort, NodeAdapter nodeAdapter){ return ports.Where(x => x.portType == startPort.portType).ToList(); @@ -354,7 +362,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ Add(castedBlackboard); - Rect blackboardPos = new Rect(0,0,200,700); + Rect blackboardPos = new Rect(0,0,300,700); castedBlackboard?.SetPosition(blackboardPos); diff --git a/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs index caec25e..285ce31 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/DefaultNodeView.cs @@ -1,5 +1,6 @@ using TNode.Editor.NodeViews; using TNode.Models; +using TNodeGraphViewImpl.Editor.NodeViews; namespace TNode.Editor{ diff --git a/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs index dbcd21c..a425150 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs @@ -1,5 +1,6 @@ using TNode.Attribute; using TNode.Models; +using TNodeGraphViewImpl.Editor.NodeViews; namespace TNode.Editor.NodeViews{ [ViewComponent] diff --git a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs index 5cdc716..9a6bd90 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs @@ -1,15 +1,15 @@ using System; using System.Linq; using System.Reflection; -using TNode.Attribute; using TNode.Attribute.Ports; using TNode.Editor.Inspector; +using TNode.Editor.Serialization; using TNode.Models; using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.UIElements; -namespace TNode.Editor.NodeViews{ +namespace TNodeGraphViewImpl.Editor.NodeViews{ public abstract class BaseNodeView : Node,INodeView where T:NodeData,new(){ protected T _data; @@ -28,10 +28,9 @@ namespace TNode.Editor.NodeViews{ } } - private void OnDataValueChanged(NodeDataWrapper obj){ + private void OnDataValueChanged(DataWrapper obj){ Refresh(); } - public sealed override string title{ get => base.title; set => base.title = value; From 5c851d5cca8b2241ae9c78515154cf37c7c6cfdb Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Wed, 13 Jul 2022 19:42:30 +0800 Subject: [PATCH 4/4] fix: fix serialization bugs --- TNode/Attribute/Ports/InputAttribute.cs | 2 +- TNode/Attribute/Ports/OutputAttribute.cs | 4 +- TNode/Attribute/Ports/PortAttribute.cs | 13 ++++- TNode/JsonSerialize.meta | 3 -- TNode/JsonSerialize/JsonSerializeTool.cs | 36 -------------- TNode/JsonSerialize/JsonSerializeTool.cs.meta | 3 -- TNode/JsonSerialize/NodeDataConverter.cs | 42 ---------------- TNode/JsonSerialize/NodeDataConverter.cs.meta | 3 -- TNode/JsonSerialize/UnityObjectConverter.cs | 24 ---------- .../UnityObjectConverter.cs.meta | 3 -- TNode/JsonSerialize/Vector3Converter.cs | 28 ----------- TNode/JsonSerialize/Vector3Converter.cs.meta | 3 -- TNode/Models/BlackboardDragNodeData.cs | 15 +++--- TNode/Models/GraphData.cs | 48 ++++++------------- TNode/RuntimeCache/RuntimeCache.cs | 5 ++ .../DefaultGraphBlackboardView.cs | 5 ++ .../Editor/Inspector/NodeInspectorInNode.cs | 2 +- .../Editor/NodeGraphView/DataGraphView.cs | 9 ++-- .../Editor/NodeViews/DragNodeView.cs | 6 +-- .../Editor/NodeViews/NodeView.cs | 16 ++++++- TNodeGraphViewImpl/Editor/Resources.meta | 3 ++ .../Resources/GraphViewPropertyField.uss | 5 ++ .../Resources/GraphViewPropertyField.uss.meta | 3 ++ 23 files changed, 80 insertions(+), 201 deletions(-) delete mode 100644 TNode/JsonSerialize.meta delete mode 100644 TNode/JsonSerialize/JsonSerializeTool.cs delete mode 100644 TNode/JsonSerialize/JsonSerializeTool.cs.meta delete mode 100644 TNode/JsonSerialize/NodeDataConverter.cs delete mode 100644 TNode/JsonSerialize/NodeDataConverter.cs.meta delete mode 100644 TNode/JsonSerialize/UnityObjectConverter.cs delete mode 100644 TNode/JsonSerialize/UnityObjectConverter.cs.meta delete mode 100644 TNode/JsonSerialize/Vector3Converter.cs delete mode 100644 TNode/JsonSerialize/Vector3Converter.cs.meta create mode 100644 TNodeGraphViewImpl/Editor/Resources.meta create mode 100644 TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss create mode 100644 TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss.meta diff --git a/TNode/Attribute/Ports/InputAttribute.cs b/TNode/Attribute/Ports/InputAttribute.cs index e0d4b96..3755188 100644 --- a/TNode/Attribute/Ports/InputAttribute.cs +++ b/TNode/Attribute/Ports/InputAttribute.cs @@ -5,7 +5,7 @@ namespace TNode.Attribute.Ports{ [MeansImplicitUse] [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class InputAttribute : PortAttribute{ - public InputAttribute(string name="", PortNameHandling nameHandling = PortNameHandling.Auto) : base(name, nameHandling){ + public InputAttribute(string name="", PortNameHandling nameHandling = PortNameHandling.Auto,TypeHandling typeHandling=TypeHandling.Declared) : base(name, nameHandling,typeHandling){ } } } \ No newline at end of file diff --git a/TNode/Attribute/Ports/OutputAttribute.cs b/TNode/Attribute/Ports/OutputAttribute.cs index a0c3908..b9c6888 100644 --- a/TNode/Attribute/Ports/OutputAttribute.cs +++ b/TNode/Attribute/Ports/OutputAttribute.cs @@ -1,6 +1,8 @@ namespace TNode.Attribute.Ports{ public class OutputAttribute:PortAttribute{ - public OutputAttribute(string name="", PortNameHandling nameHandling = PortNameHandling.Auto) : base(name, nameHandling){ + + + public OutputAttribute(string name="", PortNameHandling nameHandling = PortNameHandling.Auto,TypeHandling typeHandling = TypeHandling.Declared) : base(name, nameHandling,typeHandling){ } } } \ No newline at end of file diff --git a/TNode/Attribute/Ports/PortAttribute.cs b/TNode/Attribute/Ports/PortAttribute.cs index cc3be06..1ade1cc 100644 --- a/TNode/Attribute/Ports/PortAttribute.cs +++ b/TNode/Attribute/Ports/PortAttribute.cs @@ -9,17 +9,26 @@ namespace TNode.Attribute.Ports{ Manual, Format, MemberType + } + public enum TypeHandling{ + Declared, + Implemented, + Specified } [MeansImplicitUse] [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class PortAttribute:System.Attribute{ public readonly string Name; public readonly PortNameHandling NameHandling; - - public PortAttribute(string name,PortNameHandling nameHandling=PortNameHandling.Auto){ + public Type HandledType; + public TypeHandling TypeHandling{ get; set; } + public PortAttribute(string name,PortNameHandling nameHandling=PortNameHandling.Auto,TypeHandling typeHandling=TypeHandling.Declared){ this.Name = name; this.NameHandling = nameHandling; + this.TypeHandling = typeHandling; } + + } } \ No newline at end of file diff --git a/TNode/JsonSerialize.meta b/TNode/JsonSerialize.meta deleted file mode 100644 index 68eeed3..0000000 --- a/TNode/JsonSerialize.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: ec6119d082a947f58ed6402290b65596 -timeCreated: 1656817965 \ No newline at end of file diff --git a/TNode/JsonSerialize/JsonSerializeTool.cs b/TNode/JsonSerialize/JsonSerializeTool.cs deleted file mode 100644 index 77194ac..0000000 --- a/TNode/JsonSerialize/JsonSerializeTool.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; - - -namespace TNode.JsonSerialize{ - public static class JsonSerializeTool{ - class WritablePropertiesOnlyResolver : DefaultContractResolver - { - protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) - { - IList props = base.CreateProperties(type, memberSerialization); - return props.Where(p => p.Writable).ToList(); - } - } - public static JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings(){ - ReferenceLoopHandling = ReferenceLoopHandling.Ignore, - NullValueHandling = NullValueHandling.Ignore, - DateFormatString = "yyyy-MM-dd HH:mm:ss", - Converters = new List { new Vector3Converter(),new UnityObjectConverter() }, - TypeNameHandling = TypeNameHandling.Auto, - ContractResolver = new WritablePropertiesOnlyResolver(), - Formatting = Formatting.Indented - - - }; - - public static JsonSerializerSettings InternalJsonSerializerSettings = new JsonSerializerSettings(){ - ReferenceLoopHandling = ReferenceLoopHandling.Ignore, - NullValueHandling = NullValueHandling.Ignore, - Formatting = Formatting.Indented - }; - } -} \ No newline at end of file diff --git a/TNode/JsonSerialize/JsonSerializeTool.cs.meta b/TNode/JsonSerialize/JsonSerializeTool.cs.meta deleted file mode 100644 index 4480c5b..0000000 --- a/TNode/JsonSerialize/JsonSerializeTool.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 14067671fe28434e9ae2e67a34074c52 -timeCreated: 1656819161 \ No newline at end of file diff --git a/TNode/JsonSerialize/NodeDataConverter.cs b/TNode/JsonSerialize/NodeDataConverter.cs deleted file mode 100644 index a3c5f26..0000000 --- a/TNode/JsonSerialize/NodeDataConverter.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using Newtonsoft.Json; -using TNode.Models; -using UnityEngine; - -namespace TNode.JsonSerialize{ - public class NodeDataConverter:JsonConverter{ - public override void WriteJson(JsonWriter writer, NodeData value, JsonSerializer serializer){ - //Write node data with type information - writer.WriteStartObject(); - writer.WritePropertyName("type"); - Debug.Log(value.GetType().ToString()); - writer.WriteValue(value.GetType().Name); - writer.WritePropertyName("data"); - serializer.Serialize(writer, value, value.GetType()); - - - writer.WriteEndObject(); - - - } - - public override NodeData ReadJson(JsonReader reader, Type objectType, NodeData existingValue, bool hasExistingValue, - JsonSerializer serializer){ - //Load type info - reader.Read(); - - if (reader.Value != null){ - var type = reader.Value.ToString(); - if (type.Trim().Length==0){ - Debug.LogError(type); - throw new JsonSerializationException("Type name is empty"); - } - reader.Read(); - //Load data - var data = serializer.Deserialize(reader, Type.GetType(type)); - return (NodeData) data; - } - return null; - } - } -} \ No newline at end of file diff --git a/TNode/JsonSerialize/NodeDataConverter.cs.meta b/TNode/JsonSerialize/NodeDataConverter.cs.meta deleted file mode 100644 index e29d3c5..0000000 --- a/TNode/JsonSerialize/NodeDataConverter.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 6f960539f2744729b35ff3011677d8ba -timeCreated: 1656857829 \ No newline at end of file diff --git a/TNode/JsonSerialize/UnityObjectConverter.cs b/TNode/JsonSerialize/UnityObjectConverter.cs deleted file mode 100644 index 0d7555d..0000000 --- a/TNode/JsonSerialize/UnityObjectConverter.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using Newtonsoft.Json; -using UnityEditor; -using UnityEngine; -using Object = UnityEngine.Object; - -namespace TNode.JsonSerialize{ - public class UnityObjectConverter:JsonConverter{ - public override void WriteJson(JsonWriter writer, Object value, JsonSerializer serializer){ - //Holding the object reference in a string - var go = value; - var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(go)); - writer.WriteValue(value.GetInstanceID().ToString()); - } - - public override Object ReadJson(JsonReader reader, Type objectType, Object existingValue, bool hasExistingValue, - JsonSerializer serializer){ - //Reading the object reference from the string - var guid = reader.Value.ToString(); - var go = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); - return go; - } - } -} \ No newline at end of file diff --git a/TNode/JsonSerialize/UnityObjectConverter.cs.meta b/TNode/JsonSerialize/UnityObjectConverter.cs.meta deleted file mode 100644 index 346f409..0000000 --- a/TNode/JsonSerialize/UnityObjectConverter.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 1c430c5760df439690d22ab18daa9d72 -timeCreated: 1657700744 \ No newline at end of file diff --git a/TNode/JsonSerialize/Vector3Converter.cs b/TNode/JsonSerialize/Vector3Converter.cs deleted file mode 100644 index 00d4f93..0000000 --- a/TNode/JsonSerialize/Vector3Converter.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Numerics; -using Newtonsoft.Json; -namespace TNode.JsonSerialize{ - - public class Vector3Converter:JsonConverter{ - public override void WriteJson(JsonWriter writer, Vector3 value, JsonSerializer serializer){ - writer.WriteStartArray(); - writer.WriteValue(value.X); - writer.WriteValue(value.Y); - writer.WriteValue(value.Z); - writer.WriteEndArray(); - } - - public override Vector3 ReadJson(JsonReader reader, Type objectType, Vector3 existingValue, bool hasExistingValue, JsonSerializer serializer){ - if (reader.TokenType == JsonToken.Null){ - return default(Vector3); - } - else{ - var array = serializer.Deserialize(reader); - - if (array != null) return new Vector3(array[0], array[1], array[2]); - } - return default(Vector3); - - } - } -} \ No newline at end of file diff --git a/TNode/JsonSerialize/Vector3Converter.cs.meta b/TNode/JsonSerialize/Vector3Converter.cs.meta deleted file mode 100644 index dbbbc50..0000000 --- a/TNode/JsonSerialize/Vector3Converter.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: fecb77054ad348239341a58c50549879 -timeCreated: 1656817975 \ No newline at end of file diff --git a/TNode/Models/BlackboardDragNodeData.cs b/TNode/Models/BlackboardDragNodeData.cs index 6b1680b..3fb0891 100644 --- a/TNode/Models/BlackboardDragNodeData.cs +++ b/TNode/Models/BlackboardDragNodeData.cs @@ -4,15 +4,18 @@ using Newtonsoft.Json; using TNode.Attribute; using TNode.Attribute.Ports; using TNode.RuntimeCache; +using UnityEngine; +using UnityEngine.Serialization; namespace TNode.Models{ - public class BlackboardDragNodeData:NodeData{ - private string _blackDragData; - [JsonIgnore] - private BlackboardData _blackboardData; + [Serializable] + public class BlackboardDragNodeData:NodeData{ + public string blackDragData; + [SerializeReference] + public BlackboardData blackboardData; - [Output("",PortNameHandling.MemberType)] - public T Value => _blackboardData.GetValue(_blackDragData); + [Output("",PortNameHandling.MemberType,TypeHandling.Implemented)] + public object Value => blackboardData.GetValue(blackDragData); public BlackboardDragNodeData(){ diff --git a/TNode/Models/GraphData.cs b/TNode/Models/GraphData.cs index 8f7031b..d5c01e4 100644 --- a/TNode/Models/GraphData.cs +++ b/TNode/Models/GraphData.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using UnityEngine; using Newtonsoft.Json; -using TNode.JsonSerialize; +using UnityEngine; using UnityEngine.Serialization; namespace TNode.Models{ @@ -10,18 +9,12 @@ namespace TNode.Models{ public class GraphData:ScriptableObject,ISerializationCallbackReceiver{ public Dictionary NodeDictionary = new Dictionary(); + [SerializeReference] + public List nodeList = new List(); [SerializeField] protected List nodeLinks; - [TextArea(1,10)] - [SerializeField] - //[HideInInspector] - private string jsonNodeData; - [TextArea(1,10)] - [SerializeField] - private string jsonBlackboard; - - + [SerializeReference] public BlackboardData blackboardData; public List NodeLinks{ @@ -34,32 +27,19 @@ namespace TNode.Models{ public void OnBeforeSerialize(){ - if (nodeLinks != null){ - jsonNodeData = JsonConvert.SerializeObject(NodeDictionary,JsonSerializeTool.JsonSerializerSettings); - - } - - if (jsonBlackboard != null){ - jsonBlackboard = JsonConvert.SerializeObject(blackboardData,typeof(object),JsonSerializeTool.JsonSerializerSettings); - + + + nodeList.Clear(); + foreach(var node in NodeDictionary.Values){ + nodeList.Add(node); } } public void OnAfterDeserialize(){ - //Deserialize node dictionary - var deserializedData = JsonConvert.DeserializeObject>(jsonNodeData,JsonSerializeTool.JsonSerializerSettings); - NodeDictionary = deserializedData; - //Deserialize blackboard data - // var deserializedBlackboard = - // JsonConvert.DeserializeObject(jsonBlackboard,JsonSerializeTool.JsonSerializerSettings); - // blackboardData = deserializedBlackboard as BlackboardData; - // Debug.Log(deserializedBlackboard); - } - - public void OnEnable(){ - var deserializedBlackboard = - JsonConvert.DeserializeObject(jsonBlackboard,JsonSerializeTool.JsonSerializerSettings); - blackboardData = deserializedBlackboard as BlackboardData; - Debug.Log(deserializedBlackboard); + NodeDictionary.Clear(); + foreach(var node in nodeList){ + NodeDictionary.Add(node.id,node); + } } + } } \ No newline at end of file diff --git a/TNode/RuntimeCache/RuntimeCache.cs b/TNode/RuntimeCache/RuntimeCache.cs index 4310a3e..b46593a 100644 --- a/TNode/RuntimeCache/RuntimeCache.cs +++ b/TNode/RuntimeCache/RuntimeCache.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using EasyRandomGenerator.Blackboard; using JetBrains.Annotations; using TNode.Models; using Unity.VisualScripting; @@ -21,6 +22,10 @@ namespace TNode.RuntimeCache{ new (); private static readonly string[] ExcludedAssemblies = new string[]{"Microsoft", "UnityEngine","UnityEditor","mscorlib","System"}; + + public RuntimeCache(){ + RegisterRuntimeBlackboard(typeof(EasyBlackboardData)); + } public void RegisterRuntimeBlackboard(Type type){ if(!CachedDelegatesForGettingValue.ContainsKey(type)){ CachedDelegatesForGettingValue.Add(type, new Dictionary()); diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 80eaeb2..69ffebc 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -14,6 +14,11 @@ using UnityEngine.UIElements; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ [ViewComponent] public class DefaultGraphBlackboardView:GraphBlackboardView{ + public DefaultGraphBlackboardView():base(){ + //the label and the field gap smaller + styleSheets.Add( Resources.Load("GraphViewPropertyField")); + + } protected override void UpdateBlackboard(BlackboardData data){ var serializedObject = new SerializedObject((BlackboardDataWrapper)data); foreach (var field in data.GetType() diff --git a/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs index b0c573c..e016bfe 100644 --- a/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs +++ b/TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs @@ -38,7 +38,7 @@ namespace TNode.Editor.Inspector{ private void RefreshPropertyDrawer(){ //Check if the data's type is a generic type of BlackboardDragNodeData<> - if (_data.GetType().IsSubclassOf(typeof(BlackboardDragNodeData<>))){ + if (_data.GetType().IsSubclassOf(typeof(BlackboardDragNodeData))){ return; } var serializedObject = new SerializedObject((NodeDataWrapper)_data); diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index 7ef4c68..ae5d75a 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -105,11 +105,10 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ foreach (var selectable in blackboardFields){ if(selectable is { } field) { //Make a constructor of BlackboardDragNodeData by reflection - var specifiedType = - typeof(BlackboardDragNodeData<>).MakeGenericType(field.BlackboardProperty.PropertyType); - //Create a new instance of specified type - var dragNodeData = NodeCreator.InstantiateNodeData(specifiedType); - this.AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200))); + var dragNodeData = NodeCreator.InstantiateNodeData(); + dragNodeData.blackboardData = _data.blackboardData; + dragNodeData.blackDragData = field.BlackboardProperty.PropertyName; + AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200))); } } diff --git a/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs index a425150..ad80fae 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs @@ -4,11 +4,9 @@ using TNodeGraphViewImpl.Editor.NodeViews; namespace TNode.Editor.NodeViews{ [ViewComponent] - public class DragBaseNodeView:BaseNodeView>{ + public class DragBaseNodeView:BaseNodeView{ public DragBaseNodeView() : base(){ - //Make capsule like style - - this.titleContainer.visible = false; + this.titleContainer.visible = false; this.titleContainer.RemoveFromHierarchy(); } } diff --git a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs index 9a6bd90..cdbf1ba 100644 --- a/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs +++ b/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs @@ -76,6 +76,18 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{ throw new ArgumentOutOfRangeException(); } } + protected virtual Type BuildPortType(PortAttribute portAttribute,PropertyInfo propertyInfo){ + switch (portAttribute.TypeHandling){ + case TypeHandling.Declared : + return propertyInfo.PropertyType; + case TypeHandling.Implemented: + return propertyInfo.GetValue(_data)?.GetType(); + case TypeHandling.Specified: + return portAttribute.HandledType??typeof(object); + default: + throw new ArgumentOutOfRangeException(); + } + } /// /// of course you can override this method to build your own port builder /// @@ -84,7 +96,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{ foreach (var propertyInfo in propertyInfos){ if (propertyInfo.GetCustomAttributes(typeof(OutputAttribute),true).FirstOrDefault() is OutputAttribute attribute){ - Port port = InstantiatePort(Orientation.Horizontal, Direction.Output,Port.Capacity.Multi,propertyInfo.PropertyType); + Port port = InstantiatePort(Orientation.Horizontal, Direction.Output,Port.Capacity.Multi,BuildPortType(attribute,propertyInfo)); this.outputContainer.Add(port); var portName = BuildPortName(attribute,propertyInfo); port.portName = portName; @@ -93,7 +105,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{ } foreach (var propertyInfo in propertyInfos){ if(propertyInfo.GetCustomAttributes(typeof(InputAttribute),true).FirstOrDefault() is InputAttribute attribute){ - Port port = InstantiatePort(Orientation.Horizontal, Direction.Input,Port.Capacity.Single,propertyInfo.PropertyType); + Port port = InstantiatePort(Orientation.Horizontal, Direction.Input,Port.Capacity.Single,BuildPortType(attribute,propertyInfo)); this.inputContainer.Add(port); var portName = BuildPortName(attribute,propertyInfo); port.portName = portName; diff --git a/TNodeGraphViewImpl/Editor/Resources.meta b/TNodeGraphViewImpl/Editor/Resources.meta new file mode 100644 index 0000000..f5dfec6 --- /dev/null +++ b/TNodeGraphViewImpl/Editor/Resources.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7d628735dd6d477c88ed608b684c50b4 +timeCreated: 1657702172 \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss b/TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss new file mode 100644 index 0000000..69a2c57 --- /dev/null +++ b/TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss @@ -0,0 +1,5 @@ +.unity-property-field__label { + width: 75px; + min-width: 100px; + max-width: 150px; +} \ No newline at end of file diff --git a/TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss.meta b/TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss.meta new file mode 100644 index 0000000..74ae713 --- /dev/null +++ b/TNodeGraphViewImpl/Editor/Resources/GraphViewPropertyField.uss.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2b84790a3a0445f894b9c496ad1e716b +timeCreated: 1657702197 \ No newline at end of file