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] 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;