You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
4.0 KiB
C#
103 lines
4.0 KiB
C#
using System;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEditor.UIElements;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace LanLib.Singleton.EditorUtilities
|
|
{
|
|
public class SingletonWindow : EditorWindow
|
|
{
|
|
const int SourceUpdateInterval = 100;
|
|
const string UIRootGuid = "f019694f4240ea24281a93f5aab2f756";
|
|
const string UIElementGuid = "a33c335b67caf3e41af9c2753dcbb734";
|
|
|
|
[MenuItem("Tools/LanLib/Singletons")]
|
|
private static void ShowWindow()
|
|
{
|
|
var window = GetWindow<SingletonWindow>();
|
|
window.titleContent = new GUIContent("Singletons");
|
|
window.Show();
|
|
}
|
|
|
|
private void CreateGUI()
|
|
{
|
|
var uiRootAsset = GetVisualTreeAssetByGUID(UIRootGuid);
|
|
var uiElementAsset = GetVisualTreeAssetByGUID(UIElementGuid);
|
|
uiRootAsset.CloneTree(rootVisualElement);
|
|
var lst = rootVisualElement.Q<ListView>("lst");
|
|
(lst.makeItem, lst.bindItem) = BindItem();
|
|
rootVisualElement.schedule.Execute(UpdateSource).Every(SourceUpdateInterval);
|
|
|
|
(Func<VisualElement>, Action<VisualElement, int>) BindItem()
|
|
{
|
|
// GameObject gameObject = null;
|
|
// Label txtName = null;
|
|
// VisualElement btnInspect = null;
|
|
// VisualElement btnDestroy = null;
|
|
// MaskField mask = null;
|
|
return (OnCreate, OnBind);
|
|
|
|
VisualElement OnCreate()
|
|
{
|
|
var ele = uiElementAsset.CloneTree();
|
|
var txtName = ele.Q<Label>("txtName");
|
|
var btnInspect = ele.Q("btnInspect");
|
|
var btnDestroy = ele.Q("btnDestroy");
|
|
var mask = ele.Q<MaskField>("mask");
|
|
mask.choices = Enum.GetNames(typeof(HideFlags))[1..^2].ToList();
|
|
mask.RegisterValueChangedCallback(e =>
|
|
{
|
|
var go = GetGameObject();
|
|
if (go) go.hideFlags = (HideFlags)e.newValue;
|
|
});
|
|
btnDestroy.RegisterCallback<ClickEvent>(_ =>
|
|
{
|
|
var go = GetGameObject();
|
|
if (go) DestroyImmediate(go);
|
|
});
|
|
btnInspect.RegisterCallback<ClickEvent>(_ =>
|
|
{
|
|
var go = GetGameObject();
|
|
if (go) Selection.activeGameObject = go;
|
|
// EditorUtility.OpenPropertyEditor(go);
|
|
});
|
|
txtName.RegisterCallback<ClickEvent>(_ =>
|
|
{
|
|
var go = GetGameObject();
|
|
if (go) Selection.activeGameObject = go;
|
|
});
|
|
return ele;
|
|
GameObject GetGameObject() => ele.userData as GameObject;
|
|
}
|
|
|
|
void OnBind(VisualElement ele, int idx)
|
|
{
|
|
var txtName = ele.Q<Label>("txtName");
|
|
var mask = ele.Q<MaskField>("mask");
|
|
var gameObject = lst.itemsSource[idx] as GameObject;
|
|
if (gameObject != null)
|
|
{
|
|
txtName.text = gameObject.name;
|
|
mask.value = (int)gameObject.hideFlags;
|
|
ele.userData = gameObject;
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateSource()
|
|
{
|
|
lst.itemsSource = Resources.FindObjectsOfTypeAll<SingletonBase>()
|
|
.Select(comp => comp.gameObject)
|
|
.Distinct()
|
|
.Where(o => o)
|
|
.ToList();
|
|
lst.RefreshItems();
|
|
}
|
|
}
|
|
|
|
VisualTreeAsset GetVisualTreeAssetByGUID(string guid) =>
|
|
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(AssetDatabase.GUIDToAssetPath(guid));
|
|
}
|
|
} |