1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-04 14:24:54 +08:00

Can now save NodeGraph as asset via NodeGraphAsset

This commit is contained in:
Thor Brigsted 2017-09-21 21:30:12 +02:00
parent 72cd983f7b
commit 3c206b03df
7 changed files with 81 additions and 15 deletions

BIN
Examples/NewNodeGraph.asset Normal file

Binary file not shown.

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 1452552e49747df45b25f74d307c25b6
timeCreated: 1506021986
licenseType: Free
NativeFormatImporter:
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -32,12 +32,11 @@ public partial class NodeEditorWindow {
public void FileContextMenu() {
GenericMenu contextMenu = new GenericMenu();
contextMenu.AddItem(new GUIContent("Create New"), false, null);
contextMenu.AddItem(new GUIContent("Load"), false, Load);
contextMenu.AddItem(new GUIContent("Create New"), false, New);
contextMenu.AddSeparator("");
contextMenu.AddItem(new GUIContent("Save"), false, Save);
contextMenu.AddItem(new GUIContent("Save As"), false, null);
contextMenu.AddItem(new GUIContent("Save As"), false, SaveAs);
contextMenu.DropDown(new Rect(5f, 17f, 0f, 0f));
}

View File

@ -2,39 +2,61 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.IO;
using UnityEditor.Callbacks;
[InitializeOnLoad]
public partial class NodeEditorWindow : EditorWindow {
string saved;
public partial class NodeEditorWindow : EditorWindow {
public Dictionary<NodePort, Vector2> portConnectionPoints { get { return _portConnectionPoints; } }
private Dictionary<NodePort, Vector2> _portConnectionPoints = new Dictionary<NodePort, Vector2>();
private Dictionary<NodePort, Rect> portRects = new Dictionary<NodePort, Rect>();
public NodeGraphAsset graphAsset;
public NodeGraph graph { get { return _graph != null ? _graph : _graph = new NodeGraph(); } }
public NodeGraph _graph;
public NodeGraph _graph;
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
private Vector2 _panOffset;
private Vector2 _panOffset;
public float zoom { get { return _zoom; } set { _zoom = Mathf.Clamp(value, 1f, 5f); Repaint(); } }
private float _zoom = 1;
partial void OnEnable();
[MenuItem("Window/UNEC")]
static void Init() {
static NodeEditorWindow Init() {
NodeEditorWindow w = CreateInstance<NodeEditorWindow>();
w.titleContent = new GUIContent("UNEC");
w.wantsMouseMove = true;
w.Show();
return w;
}
public void Save() {
saved = graph.Serialize();
if (graphAsset == null) SaveAs();
else if (AssetDatabase.Contains(graphAsset)) {
graphAsset.json = graph.Serialize();
EditorUtility.SetDirty(graphAsset);
AssetDatabase.SaveAssets();
}
else SaveAs();
}
public void Load() {
_graph = NodeGraph.Deserialize(saved);
public void SaveAs() {
string path = EditorUtility.SaveFilePanelInProject("Save NodeGraph", "NewNodeGraph", "asset", "");
if (string.IsNullOrEmpty(path)) return;
else {
NodeGraphAsset existingGraphAsset = AssetDatabase.LoadAssetAtPath<NodeGraphAsset>(path);
if (existingGraphAsset != null) graphAsset = existingGraphAsset;
else {
graphAsset = new NodeGraphAsset();
AssetDatabase.CreateAsset(graphAsset, path);
}
graphAsset.json = graph.Serialize();
EditorUtility.SetDirty(graphAsset);
AssetDatabase.SaveAssets();
}
}
public void New() {
_graph = new NodeGraph();
}
private void DraggableWindow(int windowID) {
@ -60,4 +82,15 @@ public partial class NodeEditorWindow : EditorWindow {
selectedNode = node;
}
[OnOpenAsset(0)]
public static bool OnOpen(int instanceID, int line) {
NodeGraphAsset nodeGraphAsset = EditorUtility.InstanceIDToObject(instanceID) as NodeGraphAsset;
if (nodeGraphAsset != null) {
NodeEditorWindow w = Init();
w.graphAsset = nodeGraphAsset;
w._graph = nodeGraphAsset.nodeGraph;
return true;
}
return false;
}
}

View File

@ -87,7 +87,7 @@ public class NodeGraph {
}
private class NodeTyper {
public string nodeType;
public string nodeType = "Node";
}
}

13
Scripts/NodeGraphAsset.cs Normal file
View File

@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[CreateAssetMenu(fileName = "NewNodeGraph", menuName = "Node Graph")]
public class NodeGraphAsset : ScriptableObject {
public string json { get { return _json; } set { _json = value; _nodeGraph = null; } }
[SerializeField] private string _json;
public NodeGraph nodeGraph { get { return _nodeGraph != null ? _nodeGraph : _nodeGraph = NodeGraph.Deserialize(json); } }
[NonSerialized] private NodeGraph _nodeGraph = null;
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 803fd86571b50524f807cf1e86777b1a
timeCreated: 1506020816
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: