NanoBrain-unitypackage/Editor/ClusterPrefab_Drawer.cs

84 lines
3.3 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor;
namespace NanoBrain.Unity {
[CustomPropertyDrawer(typeof(ClusterPrefab))]
class ClusterPrefab_Drawer : PropertyDrawer {
public static void Insepctor(SerializedObject serializedObject, string propertyName) {
EditorGUILayout.PropertyField(serializedObject.FindProperty(propertyName));
}
// Cache VisualElement per property path to avoid recreating every frame
static Dictionary<string, VisualElement> s_cache = new Dictionary<string, VisualElement>();
const float padding = 4f;
const float elementHeight = 64f; // height reserved for the VisualElement
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
float height = EditorGUIUtility.singleLineHeight;
if (property.objectReferenceValue != null) {
height += padding + elementHeight;
height = 500;
}
return height;
}
static Dictionary<string, bool> s_foldouts = new Dictionary<string, bool>();
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
label = EditorGUI.BeginProperty(position, label, property);
// Begin indent block
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Draw the object field on the top line
Rect fieldRect = new(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
EditorGUI.PropertyField(fieldRect, property, label);
if (property.objectReferenceValue is ClusterPrefab prefab) {
// key per field instance
string key = property.propertyPath + "_" + property.serializedObject.targetObject.GetEntityId();
if (!s_foldouts.TryGetValue(key, out bool isOpen)) isOpen = true;
// foldout header rect
Rect headerRect = new Rect(fieldRect.x, fieldRect.yMax + 4f, fieldRect.width, EditorGUIUtility.singleLineHeight);
isOpen = EditorGUI.Foldout(headerRect, isOpen, "Graph", true);
s_foldouts[key] = isOpen;
if (isOpen) {
// content rect below header
Rect drawRect = new Rect(fieldRect.x, headerRect.yMax + 2f, fieldRect.width, 450f);
// IMGUIContainer should be inserted here
ClusterView.Render(drawRect, prefab.cluster, property);
}
}
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
// public ClusterViewer.GraphView CreateViewer(Cluster cluster, GameObject gameObject) {
// VisualElement mainContainer = new() {
// style = {
// flexDirection = FlexDirection.Row,
// minHeight = 450
// }
// };
// ClusterViewer.GraphView graph = new(cluster);
// graph.style.flexGrow = 1;
// mainContainer.Add(graph);
// root.Add(mainContainer);
// graph.SetGraph(gameObject);
// return graph;
// }
}
}