mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 01:06:01 +08:00
New component: SceneGraph, lets you create a graph inside the scene which can reference scene objects.
This commit is contained in:
parent
172bdbb7e5
commit
6a69cdaf9c
8
Examples.meta
Normal file
8
Examples.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3cfe6eabeed0aa44e8d9d54b308a461f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
76
Scripts/Editor/SceneGraphEditor.cs
Normal file
76
Scripts/Editor/SceneGraphEditor.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/Editor/SceneGraphEditor.cs.meta
Normal file
11
Scripts/Editor/SceneGraphEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aea725adabc311f44b5ea8161360a915
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
23
Scripts/SceneGraph.cs
Normal file
23
Scripts/SceneGraph.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XNode;
|
||||
|
||||
namespace XNode {
|
||||
/// <summary> Lets you instantiate a node graph in the scene. This allows you to reference in-scene objects. </summary>
|
||||
public class SceneGraph : MonoBehaviour {
|
||||
public NodeGraph graph;
|
||||
}
|
||||
|
||||
/// <summary> Derive from this class to create a SceneGraph with a specific graph type. </summary>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// public class MySceneGraph : SceneGraph<MyGraph> {
|
||||
///
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class SceneGraph<T> : SceneGraph where T : NodeGraph {
|
||||
public new T graph { get { return base.graph as T; } set { base.graph = value; } }
|
||||
}
|
||||
}
|
||||
11
Scripts/SceneGraph.cs.meta
Normal file
11
Scripts/SceneGraph.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7915171fc13472a40a0162003052d2db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user