com.alicizax.unity.animatio.../Editor/Graph/SearchWindowProvider.cs

65 lines
3.3 KiB
C#
Raw Normal View History

2025-02-07 16:05:13 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using AlicizaX.AnimationFlow.Runtime;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
namespace AlicizaX.AnimationFlow.Editor {
public class SearchWindowProvider : ScriptableObject, ISearchWindowProvider {
private GraphWindow graphWindow;
private GraphView graphView;
private Texture2D icon;
public void Initialize(GraphWindow graphWindow, GraphView graphView) {
this.graphWindow = graphWindow;
this.graphView = graphView;
icon = new Texture2D(1, 1);
icon.SetPixel(0, 0, Color.clear);
icon.Apply();
}
List<SearchTreeEntry> ISearchWindowProvider.CreateSearchTree(SearchWindowContext context) {
List<SearchTreeEntry> entries = new() {
new SearchTreeGroupEntry(new GUIContent("Create Node",icon))
};
Dictionary<string, List<Type>> dicType = new();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (var type in assembly.GetTypes()) {
if (type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(ActionNode))) {
CategoryAttribute categoryAttribute = type.GetCustomAttributes(typeof(CategoryAttribute), true).FirstOrDefault() as CategoryAttribute;
if (categoryAttribute == null || string.IsNullOrEmpty(categoryAttribute.category)) {
NameAttribute nameAttribute = type.GetCustomAttributes(typeof(NameAttribute), true).FirstOrDefault() as NameAttribute;
entries.Add(new SearchTreeEntry(new GUIContent(nameAttribute != null && !string.IsNullOrEmpty(nameAttribute.name) ? nameAttribute.name : type.Name, icon)) { level = 1, userData = type });
} else {
if (dicType.TryGetValue(categoryAttribute.category, out var list)) {
list.Add(type);
} else {
dicType.Add(categoryAttribute.category, new List<Type>() { type });
}
}
}
}
}
foreach (var kv in dicType) {
entries.Add(new SearchTreeGroupEntry(new GUIContent(kv.Key), 1));
foreach (Type type in kv.Value) {
NameAttribute nameAttribute = type.GetCustomAttributes(typeof(NameAttribute), true).FirstOrDefault() as NameAttribute;
entries.Add(new SearchTreeEntry(new GUIContent(nameAttribute != null && !string.IsNullOrEmpty(nameAttribute.name) ? nameAttribute.name : type.Name, icon)) { level = 2, userData = type });
}
}
return entries;
}
bool ISearchWindowProvider.OnSelectEntry(SearchTreeEntry searchTreeEntry, SearchWindowContext context) {
var type = searchTreeEntry.userData as Type;
var data = Activator.CreateInstance(type) as ActionNode;
data.uuid = Guid.NewGuid().ToString();
data.nodePos = graphView.contentViewContainer.WorldToLocal(context.screenMousePosition - graphWindow.position.position);
graphView.AddNodeView(data);
return true;
}
}
}