From 6a69cdaf9c63890572fd35659d29953771d5b9c2 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Fri, 3 Apr 2020 11:09:57 +0200 Subject: [PATCH] New component: SceneGraph, lets you create a graph inside the scene which can reference scene objects. --- Examples.meta | 8 +++ Scripts/Editor/SceneGraphEditor.cs | 76 +++++++++++++++++++++++++ Scripts/Editor/SceneGraphEditor.cs.meta | 11 ++++ Scripts/SceneGraph.cs | 23 ++++++++ Scripts/SceneGraph.cs.meta | 11 ++++ 5 files changed, 129 insertions(+) create mode 100644 Examples.meta create mode 100644 Scripts/Editor/SceneGraphEditor.cs create mode 100644 Scripts/Editor/SceneGraphEditor.cs.meta create mode 100644 Scripts/SceneGraph.cs create mode 100644 Scripts/SceneGraph.cs.meta diff --git a/Examples.meta b/Examples.meta new file mode 100644 index 0000000..231b3e2 --- /dev/null +++ b/Examples.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3cfe6eabeed0aa44e8d9d54b308a461f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Editor/SceneGraphEditor.cs b/Scripts/Editor/SceneGraphEditor.cs new file mode 100644 index 0000000..911f66c --- /dev/null +++ b/Scripts/Editor/SceneGraphEditor.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; +using XNode; + +namespace XNodeEditor { + [CustomEditor(typeof(SceneGraph), true)] + public class SceneGraphEditor : Editor { + private SceneGraph sceneGraph; + private bool removeSafely; + private Type graphType; + + public override void OnInspectorGUI() { + if (sceneGraph.graph == null) { + if (GUILayout.Button("New graph", GUILayout.Height(40))) { + if (graphType == null) { + Type[] graphTypes = NodeEditorReflection.GetDerivedTypes(typeof(NodeGraph)); + GenericMenu menu = new GenericMenu(); + for (int i = 0; i < graphTypes.Length; i++) { + Type graphType = graphTypes[i]; + menu.AddItem(new GUIContent(graphType.Name), false, () => CreateGraph(graphType)); + } + menu.ShowAsContext(); + } else { + CreateGraph(graphType); + } + } + } else { + if (GUILayout.Button("Open graph", GUILayout.Height(40))) { + NodeEditorWindow.Open(sceneGraph.graph); + } + if (removeSafely) { + GUILayout.BeginHorizontal(); + GUILayout.Label("Really remove graph?"); + GUI.color = new Color(1, 0.8f, 0.8f); + if (GUILayout.Button("Remove")) { + removeSafely = false; + sceneGraph.graph = null; + } + GUI.color = Color.white; + if (GUILayout.Button("Cancel")) { + removeSafely = false; + } + GUILayout.EndHorizontal(); + } else { + GUI.color = new Color(1, 0.8f, 0.8f); + if (GUILayout.Button("Remove graph")) { + removeSafely = true; + } + GUI.color = Color.white; + } + } + } + + private void OnEnable() { + sceneGraph = target as SceneGraph; + Type sceneGraphType = sceneGraph.GetType(); + if (sceneGraphType == typeof(SceneGraph)) { + graphType = null; + } else { + Type baseType = sceneGraphType.BaseType; + if (baseType.IsGenericType) { + graphType = sceneGraphType = baseType.GetGenericArguments() [0]; + } + } + } + + public void CreateGraph(Type type) { + serializedObject.Update(); + sceneGraph.graph = ScriptableObject.CreateInstance(type) as NodeGraph; + serializedObject.ApplyModifiedProperties(); + } + } +} \ No newline at end of file diff --git a/Scripts/Editor/SceneGraphEditor.cs.meta b/Scripts/Editor/SceneGraphEditor.cs.meta new file mode 100644 index 0000000..e1bf0b2 --- /dev/null +++ b/Scripts/Editor/SceneGraphEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aea725adabc311f44b5ea8161360a915 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/SceneGraph.cs b/Scripts/SceneGraph.cs new file mode 100644 index 0000000..bb2774f --- /dev/null +++ b/Scripts/SceneGraph.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using XNode; + +namespace XNode { + /// Lets you instantiate a node graph in the scene. This allows you to reference in-scene objects. + public class SceneGraph : MonoBehaviour { + public NodeGraph graph; + } + + /// Derive from this class to create a SceneGraph with a specific graph type. + /// + /// + /// public class MySceneGraph : SceneGraph { + /// + /// } + /// + /// + public class SceneGraph : SceneGraph where T : NodeGraph { + public new T graph { get { return base.graph as T; } set { base.graph = value; } } + } +} \ No newline at end of file diff --git a/Scripts/SceneGraph.cs.meta b/Scripts/SceneGraph.cs.meta new file mode 100644 index 0000000..c7978b6 --- /dev/null +++ b/Scripts/SceneGraph.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7915171fc13472a40a0162003052d2db +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: