fbca658 Reorganizing the package and added documentation git-subtree-dir: NanoBrain git-subtree-split: fbca658b5975ade4aa5c0ef1294dc12ead936495
70 lines
2.6 KiB
C#
70 lines
2.6 KiB
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace NanoBrain {
|
|
|
|
public class ClusterPickerWindow : EditorWindow {
|
|
private Vector2 scroll;
|
|
private ClusterPrefab[] items = new ClusterPrefab[0];
|
|
private Action<ClusterPrefab> onPicked;
|
|
private string search = "";
|
|
|
|
public static void ShowPicker(Action<ClusterPrefab> onPicked, string title = "Select Cluster") {
|
|
var w = CreateInstance<ClusterPickerWindow>();
|
|
w.titleContent = new GUIContent(title);
|
|
w.minSize = new Vector2(360, 320);
|
|
w.onPicked = onPicked;
|
|
w.RefreshList();
|
|
w.ShowModalUtility(); // modal dialog
|
|
}
|
|
|
|
private void OnEnable() => RefreshList();
|
|
|
|
private void RefreshList() {
|
|
var guids = AssetDatabase.FindAssets("t:ClusterPrefab");
|
|
items = guids
|
|
.Select(g => AssetDatabase.LoadAssetAtPath<ClusterPrefab>(AssetDatabase.GUIDToAssetPath(g)))
|
|
.Where(b => b != null)
|
|
.OrderBy(b => b.name)
|
|
.ToArray();
|
|
}
|
|
|
|
private void OnGUI() {
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.BeginHorizontal();
|
|
EditorGUILayout.LabelField("Choose Cluster:", EditorStyles.boldLabel);
|
|
if (GUILayout.Button("Refresh", GUILayout.Width(80))) RefreshList();
|
|
GUILayout.FlexibleSpace();
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.Space();
|
|
search = EditorGUILayout.TextField(search);
|
|
|
|
EditorGUILayout.Space();
|
|
scroll = EditorGUILayout.BeginScrollView(scroll);
|
|
foreach (var it in items) {
|
|
if (!string.IsNullOrEmpty(search) && it.name.IndexOf(search, StringComparison.OrdinalIgnoreCase) < 0)
|
|
continue;
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
EditorGUILayout.LabelField(EditorGUIUtility.ObjectContent(it, typeof(ClusterPrefab)), GUILayout.Height(20));
|
|
if (GUILayout.Button("Select", GUILayout.Width(70))) {
|
|
onPicked?.Invoke(it);
|
|
Close();
|
|
return;
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
}
|
|
EditorGUILayout.EndScrollView();
|
|
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("Cancel")) { onPicked?.Invoke(null); Close(); }
|
|
GUILayout.FlexibleSpace();
|
|
EditorGUILayout.EndHorizontal();
|
|
}
|
|
}
|
|
|
|
} |