mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
Simplified implementation
This commit is contained in:
parent
ede650c369
commit
1d43244a8f
@ -6,45 +6,33 @@ using UnityEditor;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace XNodeEditor {
|
namespace XNodeEditor {
|
||||||
|
/// <summary> Override graph inspector to show an 'Open Graph' button at the top </summary>
|
||||||
[CustomEditor(typeof(XNode.NodeGraph), true)]
|
[CustomEditor(typeof(XNode.NodeGraph), true)]
|
||||||
public class GlobalGraphEditor : Editor {
|
public class GlobalGraphEditor : Editor {
|
||||||
public override void OnInspectorGUI() {
|
public override void OnInspectorGUI() {
|
||||||
serializedObject.Update();
|
serializedObject.Update();
|
||||||
|
|
||||||
var graphObject = serializedObject.targetObject as XNode.NodeGraph;
|
if (GUILayout.Button("Edit graph", GUILayout.Height(40))) {
|
||||||
|
NodeEditorWindow.Open(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.Space(EditorGUIUtility.singleLineHeight);
|
||||||
GUILayout.Label("Raw data", "BoldLabel");
|
GUILayout.Label("Raw data", "BoldLabel");
|
||||||
|
|
||||||
DrawDefaultInspector();
|
DrawDefaultInspector();
|
||||||
|
|
||||||
serializedObject.ApplyModifiedProperties();
|
serializedObject.ApplyModifiedProperties();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[CustomEditor(typeof(XNode.Node), true)]
|
[CustomEditor(typeof(XNode.Node), true)]
|
||||||
public class GlobalNodeEditor : Editor {
|
public class GlobalNodeEditor : Editor {
|
||||||
private SerializedProperty graph;
|
|
||||||
|
|
||||||
private void OnEnable() {
|
|
||||||
graph = serializedObject.FindProperty("graph");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnInspectorGUI() {
|
public override void OnInspectorGUI() {
|
||||||
serializedObject.Update();
|
serializedObject.Update();
|
||||||
|
|
||||||
var graphObject = GetTargetObjectOfProperty(graph) as XNode.NodeGraph;
|
if (GUILayout.Button("Edit graph", GUILayout.Height(40))) {
|
||||||
|
SerializedProperty graphProp = serializedObject.FindProperty("graph");
|
||||||
var button = GUILayout.Button("Edit graph", GUILayout.Height(40));
|
NodeEditorWindow.Open(graphProp.objectReferenceValue as XNode.NodeGraph);
|
||||||
if (button && graphObject != null) {
|
|
||||||
NodeEditorWindow.Open(graphObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GUILayout.Space(EditorGUIUtility.singleLineHeight);
|
GUILayout.Space(EditorGUIUtility.singleLineHeight);
|
||||||
@ -52,77 +40,8 @@ namespace XNodeEditor {
|
|||||||
|
|
||||||
// Now draw the node itself.
|
// Now draw the node itself.
|
||||||
DrawDefaultInspector();
|
DrawDefaultInspector();
|
||||||
|
|
||||||
serializedObject.ApplyModifiedProperties();
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user