1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

Added a big "edit graph" button to Node and Graph inspectors.

I was very confused that this isn't a thing already.
This commit is contained in:
Lumos 2019-12-21 20:33:34 +01:00
parent 5005b5e4f9
commit ede650c369
2 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,128 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace XNodeEditor {
[CustomEditor(typeof(XNode.NodeGraph), true)]
public class GlobalGraphEditor : Editor {
public override void OnInspectorGUI() {
serializedObject.Update();
var graphObject = serializedObject.targetObject as XNode.NodeGraph;
var button = GUILayout.Button("Edit graph", GUILayout.Height(40));
if (button && graphObject != null) {
NodeEditorWindow.Open(graphObject);
}
GUILayout.Space(EditorGUIUtility.singleLineHeight);
GUILayout.Label("Raw data", "BoldLabel");
DrawDefaultInspector();
serializedObject.ApplyModifiedProperties();
}
}
[CustomEditor(typeof(XNode.Node), true)]
public class GlobalNodeEditor : Editor {
private SerializedProperty graph;
private void OnEnable() {
graph = serializedObject.FindProperty("graph");
}
public override void OnInspectorGUI() {
serializedObject.Update();
var graphObject = GetTargetObjectOfProperty(graph) as XNode.NodeGraph;
var button = GUILayout.Button("Edit graph", GUILayout.Height(40));
if (button && graphObject != null) {
NodeEditorWindow.Open(graphObject);
}
GUILayout.Space(EditorGUIUtility.singleLineHeight);
GUILayout.Label("Raw data", "BoldLabel");
// Now draw the node itself.
DrawDefaultInspector();
serializedObject.ApplyModifiedProperties();
}
// HELPER METHODS BELOW RETRIEVED FROM https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBaseEditor/EditorHelper.cs
// BASED ON POST https://forum.unity.com/threads/get-a-general-object-value-from-serializedproperty.327098/#post-2309545
// AND ADJUSTED SLIGHTLY AFTERWARDS
/// <summary>
/// Gets the object the property represents.
/// </summary>
/// <param name="prop"></param>
/// <returns></returns>
private static object GetTargetObjectOfProperty(SerializedProperty prop) {
if (prop == null) return null;
var path = prop.propertyPath.Replace(".Array.data[", "[");
object obj = prop.serializedObject.targetObject;
var elements = path.Split('.');
foreach (var element in elements) {
if (element.Contains("[")) {
var elementName = element.Substring(0, element.IndexOf("[", StringComparison.Ordinal));
var index = Convert.ToInt32(element.Substring(element.IndexOf("[", StringComparison.Ordinal))
.Replace("[", "")
.Replace("]", ""));
obj = GetValue_Imp(obj, elementName, index);
}
else {
obj = GetValue_Imp(obj, element);
}
}
return obj;
}
private static object GetValue_Imp(object source, string name) {
if (source == null)
return null;
var type = source.GetType();
while (type != null) {
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (f != null)
return f.GetValue(source);
var p = type.GetProperty(name,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (p != null)
return p.GetValue(source, null);
type = type.BaseType;
}
return null;
}
private static object GetValue_Imp(object source, string name, int index) {
if (!(GetValue_Imp(source, name) is IEnumerable enumerable)) return null;
var enm = enumerable.GetEnumerator();
//while (index-- >= 0)
// enm.MoveNext();
//return enm.Current;
for (var i = 0; i <= index; i++) {
if (!enm.MoveNext()) return null;
}
return enm.Current;
}
}
}

View File

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