1.working on node serialization

main
taoria 3 years ago
parent aca6495a07
commit bca8c3af6b
  1. 8
      EasyRandomGenerator.meta
  2. 8
      Sample/MathGraph.meta
  3. 8
      Sample/MathGraph/Editor.meta
  4. 27
      Sample/MathGraph/Editor/MathEditor.asset
  5. 8
      Sample/MathGraph/Editor/MathEditor.asset.meta
  6. 22
      Sample/MathGraph/Editor/MathEditor.cs
  7. 14
      Sample/MathGraph/Editor/MathEditor.cs.meta
  8. 9
      Sample/MathGraph/Editor/MathGraphView.cs
  9. 11
      Sample/MathGraph/Editor/MathGraphView.cs.meta
  10. 9
      Sample/MathGraph/MathGraph.cs
  11. 11
      Sample/MathGraph/MathGraph.cs.meta
  12. 23
      Sample/MathGraph/New MathGraph.asset
  13. 8
      Sample/MathGraph/New MathGraph.asset.meta
  14. 11
      TNode/Attribute/DisableOnInspector.cs
  15. 3
      TNode/Attribute/DisableOnInspector.cs.meta
  16. 37
      TNode/Editor/BaseViews/DataGraphView.cs
  17. 2
      TNode/Editor/GraphEditor.cs
  18. 7
      TNode/Editor/Inspector/NodeInspector.cs
  19. 5
      TNode/Editor/Model/NodeEditorData.cs
  20. 16
      TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs
  21. 3
      TNode/Models/NodeData.cs

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cf0168c8ec1f9304c9872577b1e6abdf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 23ebabfc8f40d2c4689dc4ec9a5786d5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 73ee98eea19fa9b42b9c7990a8161d56
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,27 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cde084f079a7426daa86ed86cb80ed1b, type: 3}
m_Name: MathEditor
m_EditorClassIdentifier:
nodeData:
rid: -2
nodePos:
serializedVersion: 2
x: 0
y: 0
width: 0
height: 0
references:
version: 2
RefIds:
- rid: -2
type: {class: , ns: , asm: }

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: acb7b8ebcdf5f4f40bccf1e405c94da3
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,22 @@
using TNode.Editor;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
using System;
public class MathEditor : GraphEditor<MathGraph>{
[OnOpenAsset]
public static bool OnOpenAsset(int instanceID, int line){
var graph = EditorUtility.InstanceIDToObject(instanceID) as MathGraph;
if (graph != null)
{
var wnd = GetWindow<MathEditor>();
wnd.titleContent = new GUIContent("MathGraph Editor");
wnd.CreateGUI();
wnd._graphView.Data = graph;
return true;
}
return false;
}
}

@ -0,0 +1,14 @@
fileFormatVersion: 2
guid: c9041cb574597424fa4124edc3f99af1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences:
- m_ViewDataDictionary: {instanceID: 0}
- mVisualTreeAsset: {fileID: 9197481963319205126, guid: b67f6dcbe2361b649ad2b7845207321b, type: 3}
- nodeEditorData: {fileID: 11400000, guid: acb7b8ebcdf5f4f40bccf1e405c94da3, type: 2}
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,9 @@
using TNode.Models;
using TNode.Attribute;
using TNode.Editor.BaseViews;
[NodeComponent]
public class MathGraphView : DataGraphView<MathGraph>{
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7f4d84b648626d24eb29bfeb81c85e3f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,9 @@
using TNode.Models;
using UnityEngine;
using UnityEditor;
using System;
[CreateAssetMenu(fileName = "New MathGraph", menuName = "TNode/MathGraph")]
[Serializable]
public class MathGraph : GraphData{
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 73baeb2c71a23da4ca06e3e3e52d5a78
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,23 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 73baeb2c71a23da4ca06e3e3e52d5a78, type: 3}
m_Name: New MathGraph
m_EditorClassIdentifier:
nodes: []
nodeLinks: []
entryNode:
rid: -2
references:
version: 2
RefIds:
- rid: -2
type: {class: , ns: , asm: }

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4b23c513fb78ea44b8a11a0bf7c8479e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,11 @@
using System;
namespace TNode.Attribute{
/// <summary>
/// Use this attribute to mark a field as disabled.An disable field will not be edit by the inspector.
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class DisableOnInspectorAttribute:System.Attribute{
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e3d9cad2647b42dc8db0ce63f1291343
timeCreated: 1656669029

@ -4,6 +4,7 @@ using System.Linq;
using TNode.BaseViews;
using TNode.Cache;
using TNode.Editor.Inspector;
using TNode.Editor.Model;
using TNode.Models;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
@ -189,6 +190,14 @@ namespace TNode.Editor.BaseViews{
this.Add(miniMap);
miniMap.SetPosition(rect);
}
public void CreateBlackBoard(){
var blackboard = new Blackboard();
//Set black board to left side of the view
blackboard.SetPosition(new Rect(0,0,200,600));
this.Add(blackboard);
}
public virtual void DestroyInspector(){
if(_nodeInspector!=null){
this.Remove(_nodeInspector);
@ -204,6 +213,34 @@ namespace TNode.Editor.BaseViews{
}
}
public void SaveEditorData(GraphEditorData graphEditorData){
graphEditorData.nodesData.Clear();
//iterator nodes
foreach (var node in this.nodes){
var nodeEditorData = new NodeEditorData{
nodePos = node.GetPosition(),
};
if (node is INodeView nodeView){
nodeEditorData.nodeGuid = nodeView.GetNodeData().id;
}
graphEditorData.nodesData.Add(nodeEditorData);
}
}
public void LoadEditorData(GraphEditorData graphEditorData){
//Load node position
foreach (var nodeEditorData in graphEditorData.nodesData){
var node = this.nodes.Select(x => x as INodeView).First(x=>x?.GetNodeData().id==nodeEditorData.nodeGuid);
if (node != null){
((GraphElement)node).SetPosition(nodeEditorData.nodePos);
}
}
}
public void SaveWithEditorData(GraphEditorData graphEditorData){
SaveEditorData(graphEditorData);
}
public override List<Port> GetCompatiblePorts(Port startPort, NodeAdapter nodeAdapter){
return ports.Where(x => x.portType == startPort.portType).ToList();
}

@ -19,7 +19,7 @@ namespace TNode.Editor{
[SerializeField]
private VisualTreeAsset mVisualTreeAsset = default;
//Persist editor data ,such as node position,node size ,etc ,in this script object
public NodeEditorData nodeEditorData;
public GraphEditorData nodeEditorData;
public void CreateGUI(){

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using TNode.Attribute;
using TNode.BaseViews;
using TNode.Editor.BaseViews;
using TNode.Models;
@ -54,12 +55,16 @@ namespace TNode.Editor.Inspector{
if (methodInfo != null){
var genericMethod = methodInfo.MakeGenericMethod(type);
var createdItem = genericMethod.Invoke(inspectorItemFactory,null) as VisualElement;
body.Add(createdItem);
if (createdItem is INodeDataBindingBase castedItem){
castedItem.BindingNodeData = _data;
castedItem.BindingPath = bindingPath;
}
//Check if field has DisableOnInspector attribute and if so,disable it
if (field.GetCustomAttribute<DisableOnInspectorAttribute>() != null){
createdItem?.SetEnabled(false);
}
}
}
}

@ -1,12 +1,13 @@
using System;
using TNode.Models;
using UnityEngine;
using UnityEngine.Serialization;
namespace TNode.Editor.Model{
[Serializable]
public class NodeEditorData:ScriptableObject{
[SerializeReference] public NodeData nodeData;
public class NodeEditorData{
public string nodeGuid;
public Rect nodePos;
}
}

@ -143,17 +143,17 @@ namespace TNode.Editor.Tools.GraphEditorCreator{
}
//Create an NodeAttribute Editor Data Instance for the new graph editor
NodeEditorData nodeEditorData = ScriptableObject.CreateInstance<NodeEditorData>();
nodeEditorData.name = editorName;
var graphEditorData = ScriptableObject.CreateInstance<GraphEditorData>();
graphEditorData.name = editorName;
VisualTreeAsset defaultEditorTree = Resources.Load<VisualTreeAsset>("GraphEditor");
EditorUtility.SetDirty(nodeEditorData);
EditorUtility.SetDirty(graphEditorData);
//Save it at the same folder as the new graph editor
string nodeEditorDataPath = Path.Combine(path, editorName + ".asset");
AssetDatabase.CreateAsset(nodeEditorData, nodeEditorDataPath);
string grapEditorPath = Path.Combine(path, editorName + ".asset");
AssetDatabase.CreateAsset(graphEditorData, grapEditorPath);
//Wait for the new file to be imported
while (!AssetDatabase.LoadAssetAtPath<NodeEditorData>(nodeEditorDataPath)){
while (!AssetDatabase.LoadAssetAtPath<GraphEditorData>(grapEditorPath)){
EditorUtility.DisplayProgressBar("Generating Graph Editor",
"Please wait while the new graph editor is being imported", 0.5f);
EditorApplication.update();
@ -164,8 +164,8 @@ namespace TNode.Editor.Tools.GraphEditorCreator{
MonoImporter monoImporter = AssetImporter.GetAtPath(editorPath) as MonoImporter;
if (monoImporter != null)
monoImporter.SetDefaultReferences(new string[]{"nodeEditorData", "mVisualTreeAsset"},
new Object[]{nodeEditorData, defaultEditorTree});
monoImporter.SetDefaultReferences(new string[]{"graphEditorData", "mVisualTreeAsset"},
new Object[]{graphEditorData, defaultEditorTree});
//Refresh the asset ann close it

@ -16,8 +16,9 @@ namespace TNode.Models{
public NodeData() : base(){
//Object Registration
id = Guid.NewGuid().ToString();
}
[DisableOnInspector]
public string id;
public string nodeName;
public bool entryPoint;

Loading…
Cancel
Save