mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
Experimental: Swap editor styles by reflection
This commit is contained in:
parent
b9b5bf8d9f
commit
d481075e66
49
Scripts/Editor/EditorStylesHacks.cs
Normal file
49
Scripts/Editor/EditorStylesHacks.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace XNodeEditor {
|
||||||
|
public static class EditorStylesHacks {
|
||||||
|
private static FieldInfo EditorLabelField;
|
||||||
|
private static FieldInfo EditorFoldoutField;
|
||||||
|
private static FieldInfo EditorStylesInstanceField;
|
||||||
|
private static EditorStyles EditorStylesInstance;
|
||||||
|
|
||||||
|
private static readonly BindingFlags EditorStylesBindingFlags =
|
||||||
|
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance;
|
||||||
|
|
||||||
|
public static GUIStyle Label {
|
||||||
|
get => EditorStyles.label;
|
||||||
|
set {
|
||||||
|
CacheEditorStylesInstance();
|
||||||
|
if (EditorLabelField == null) {
|
||||||
|
EditorLabelField = typeof(EditorStyles).GetField("m_Label", EditorStylesBindingFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorLabelField.SetValue(EditorStylesInstance, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GUIStyle Foldout {
|
||||||
|
get => EditorStyles.foldout;
|
||||||
|
set {
|
||||||
|
CacheEditorStylesInstance();
|
||||||
|
if (EditorFoldoutField == null) {
|
||||||
|
EditorFoldoutField = typeof(EditorStyles).GetField("m_Foldout", EditorStylesBindingFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorFoldoutField.SetValue(EditorStylesInstance, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CacheEditorStylesInstance() {
|
||||||
|
if (EditorStylesInstanceField == null) {
|
||||||
|
EditorStylesInstanceField = typeof(EditorStyles).GetField("s_Current", EditorStylesBindingFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (EditorStylesInstance == null) {
|
||||||
|
EditorStylesInstance = EditorStylesInstanceField.GetValue(null) as EditorStyles;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Scripts/Editor/EditorStylesHacks.cs.meta
Normal file
3
Scripts/Editor/EditorStylesHacks.cs.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9850cac2240045df9ade822de8020091
|
||||||
|
timeCreated: 1555066067
|
||||||
@ -2,7 +2,6 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditorInternal;
|
using UnityEditorInternal;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -300,13 +299,26 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void DrawStyledPropertyField(SerializedProperty property, GUIContent label, GUIStyle style, bool showBacking = true, bool includeChildren = false) {
|
private static void DrawStyledPropertyField(SerializedProperty property, GUIContent label, GUIStyle style, bool showBacking = true, bool includeChildren = false) {
|
||||||
|
label = label != null ? label : new GUIContent(property.displayName);
|
||||||
|
|
||||||
|
GUIStyle oldFoldout = EditorStylesHacks.Foldout;
|
||||||
|
GUIStyle oldLabel = EditorStylesHacks.Label;
|
||||||
|
|
||||||
|
EditorStylesHacks.Label = style;
|
||||||
|
EditorStylesHacks.Foldout = NodeEditorResources.styles.foldout;
|
||||||
|
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
// Display a label
|
|
||||||
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), style, GUILayout.MaxWidth(70));
|
if (showBacking) {
|
||||||
// Display an editable property field
|
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
|
||||||
if(showBacking)
|
} else {
|
||||||
EditorGUILayout.PropertyField(property, emptyLabel, includeChildren, GUILayout.MinWidth(30));
|
EditorGUILayout.LabelField(label, style, GUILayout.MinWidth(30));
|
||||||
|
}
|
||||||
|
|
||||||
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.EndHorizontal();
|
||||||
|
|
||||||
|
EditorStylesHacks.Label = oldLabel;
|
||||||
|
EditorStylesHacks.Foldout = oldFoldout;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ReorderableList CreateReorderableList(string fieldName, List<XNode.NodePort> instancePorts, SerializedProperty arrayData, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint, Action<ReorderableList> onCreation) {
|
private static ReorderableList CreateReorderableList(string fieldName, List<XNode.NodePort> instancePorts, SerializedProperty arrayData, Type type, SerializedObject serializedObject, XNode.NodePort.IO io, XNode.Node.ConnectionType connectionType, XNode.Node.TypeConstraint typeConstraint, Action<ReorderableList> onCreation) {
|
||||||
|
|||||||
@ -16,9 +16,9 @@ namespace XNodeEditor {
|
|||||||
// Styles
|
// Styles
|
||||||
public static Styles styles { get { return _styles != null ? _styles : _styles = new Styles(); } }
|
public static Styles styles { get { return _styles != null ? _styles : _styles = new Styles(); } }
|
||||||
public static Styles _styles = null;
|
public static Styles _styles = null;
|
||||||
public static GUIStyle OutputPort { get { return new GUIStyle(EditorStyles.label) { alignment = TextAnchor.UpperRight }; } }
|
public static GUIStyle OutputPort { get { return styles.outputPort;} }
|
||||||
public class Styles {
|
public class Styles {
|
||||||
public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight, nodeProperty;
|
public GUIStyle inputPort, nodeHeader, nodeBody, tooltip, nodeHighlight, nodeProperty, outputPort, foldout;
|
||||||
|
|
||||||
public Styles() {
|
public Styles() {
|
||||||
GUIStyle baseStyle = new GUIStyle(EditorStyles.label);
|
GUIStyle baseStyle = new GUIStyle(EditorStyles.label);
|
||||||
@ -26,9 +26,15 @@ namespace XNodeEditor {
|
|||||||
baseStyle.normal.textColor = Color.white;
|
baseStyle.normal.textColor = Color.white;
|
||||||
baseStyle.onNormal.textColor = Color.white;
|
baseStyle.onNormal.textColor = Color.white;
|
||||||
|
|
||||||
inputPort = new GUIStyle(baseStyle);
|
outputPort = new GUIStyle(baseStyle);
|
||||||
|
|
||||||
|
inputPort = new GUIStyle(EditorStyles.label);
|
||||||
inputPort.alignment = TextAnchor.UpperLeft;
|
inputPort.alignment = TextAnchor.UpperLeft;
|
||||||
|
|
||||||
|
foldout = new GUIStyle(EditorStyles.foldout);
|
||||||
|
foldout.normal.textColor = baseStyle.normal.textColor;
|
||||||
|
foldout.onNormal.textColor = baseStyle.onNormal.textColor;
|
||||||
|
|
||||||
nodeProperty = new GUIStyle(baseStyle);
|
nodeProperty = new GUIStyle(baseStyle);
|
||||||
|
|
||||||
nodeHeader = new GUIStyle();
|
nodeHeader = new GUIStyle();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user