BITFALL/Assets/BITKit/UnityEditor/ScriptableObjectGroupEditor.cs

100 lines
2.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit.UX;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
namespace BITKit.GameEditor
{
public class ScriptableObjectGroupEditor{}
public class ScriptableObjectGroupEditor<T> : EditorWindow where T : Object
{
protected virtual string AssetsPath => $"Assets/Artists/";
protected readonly List<T> List=new();
private ListView _listView;
private VisualElement _container;
private void OnEnable()
{
var paths = AssetDatabase.FindAssets($"t:{typeof(T).Name}");
var allItem =
(
from path in paths.Select(AssetDatabase.GUIDToAssetPath)
select AssetDatabase.LoadAssetAtPath<ScriptableObject>(path)
).ToArray();
List.AddRange(allItem.Cast<T>());
var container = rootVisualElement.Create<VisualElement>();
var css = AssetDatabase.LoadAssetAtPath<StyleSheet>(BITEditorUtils.InspectorPath);
rootVisualElement.styleSheets.Add(css);
container.style.flexDirection = FlexDirection.Row;
var listViewContainer = container.Create<VisualElement>();
listViewContainer.style.flexDirection = FlexDirection.Column;
listViewContainer.Create<Label>().text = $"{typeof(T).Name},数量:{List.Count}";
listViewContainer.AddToClassList("pa-8");
_listView = listViewContainer.Create<ListView>();
_listView.makeItem = MakeItem;
_listView.bindItem = BindItem;
_listView.itemsChosen += ItemsChosen;
_listView.style.minWidth = 128;
_listView.itemsSource = List;
var scroll = container.Create<ScrollView>();
scroll.style.flexGrow = 1;
_container = scroll.Create<GroupBox>();
//_container.style.flexGrow = 1;
Debug.Log(
$"{GetType().Name} 已初始化,从{AssetsPath}获取到{List.Count}个{typeof(T).Name}");
}
private void ItemsChosen(IEnumerable<object> obj)
{
var selected = obj.FirstOrDefault() as Object;
var serializedObject = new SerializedObject(selected);
BITInspectorExtensions.FillDefaultInspector(_container,serializedObject, true);
_container.Bind(serializedObject);
}
protected virtual VisualElement MakeItem()
{
// var container = new VisualElement();
// var label = container.Create<Label>(UXConstant.ContextLabel);
// label.name = "ContextLabel";
// label.text = typeof(T).Name;
// return container;
return new Label();
}
protected virtual void BindItem(VisualElement arg1, int arg2)
{
try
{
var item = List[arg2];
arg1.Q<Label>().text = item.name;
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
}