Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/Item/ScriptableItemEditorWindow.cs

150 lines
5.0 KiB
C#

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using BITKit;
using BITKit.GameEditor;
using Cysharp.Threading.Tasks;
using Project.B.Item;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Debug = UnityEngine.Debug;
namespace Net.Project.B.Item
{
public class ScriptableItemEditorWindow : ScriptableObjectGroupEditor<ScriptableItem>
{
protected override string AssetsPath => "Assets/Artists/Configs/Items/";
protected string IconsPath => "Assets/Artists/Arts/Icons/Gen_Items/";
[MenuItem("Tools/Scriptable Editor/Item")]
public static void Open()
{
GetWindow<ScriptableItemEditorWindow>().Show();
}
protected override void OnEnable()
{
base.OnEnable();
{
var button = ToolBarContainer.Create<Button>();
button.text = "从Project已选的资产创建Item";
button.clicked += CreateFromProject;
}
}
private void CreateFromProject()
{
var list = new List<GameObject>();
foreach (var obj in Selection.objects)
{
if (obj is not GameObject go) continue;
list.Add(go);
}
var nameList = string.Join('\n', list.Select(x => x.name).Take(3));
if (list.Count > 3)
{
nameList += $"\n...一共:{list.Count}个";
}
if (list.Count is 0)
{
EditorUtility.DisplayDialog("错误", "没有已选的GameObjects","OK");
return;
}
if(EditorUtility.DisplayDialog("是否要创建以下Items",nameList,"确认","取消") is false)return;
var stopWatch = Stopwatch.StartNew();
foreach (var go in list)
{
var path = Path.Combine(AssetsPath, $"{go.name}.asset");
if (File.Exists(Path.Combine(Environment.CurrentDirectory, path)))
{
Debug.LogWarning($"已存在:{path},跳过");
continue;
}
var instance = CreateInstance<ScriptableItem>();
instance.id = go.GetInstanceID();
Sprite sprite = null;
foreach (var guid in AssetDatabase.FindAssets($"{go.name} t:sprite"))
{
sprite= AssetDatabase.LoadAssetAtPath<Sprite>(AssetDatabase.GUIDToAssetPath(guid));
break;
}
if (!sprite)
{
var preview = AssetPreview.GetAssetPreview(go);
if (!preview)
{
for (var i = 0; i < 32; i++)
{
preview = AssetPreview.GetAssetPreview(go);
if(preview)break;
Thread.Sleep(100);
}
}
var previewPath = Path.Combine(IconsPath, go.name+".png");
var pngPath = Path.Combine(Environment.CurrentDirectory, previewPath);
new DirectoryInfo(Path.Combine(Environment.CurrentDirectory,IconsPath)).Create();
File.WriteAllBytes(pngPath, preview.EncodeToPNG());
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
var importer = AssetImporter.GetAtPath(previewPath).As<TextureImporter>();
importer.textureType = TextureImporterType.Sprite;
importer.SaveAndReimport();
AssetDatabase.Refresh();
instance.icon = AssetDatabase.LoadAssetAtPath<Sprite>(previewPath);
}
else
{
instance.icon = sprite;
}
instance.displayName = new Reference(go.name);
instance.description = new Reference($"{go.name}:description");
instance.model = go.transform;
AssetDatabase.CreateAsset(instance, path);
AssetDatabase.SaveAssets();
}
RebuildList();
stopWatch.Stop();
Debug.Log($"创建{list.Count}个任务,耗时{stopWatch.ElapsedMilliseconds}毫秒");
}
protected override void BindItem(VisualElement arg1, int arg2)
{
base.BindItem(arg1, arg2);
var item = List[arg2];
arg1.Q<Label>().text = $"({item.Id}){item.name}";
arg1.Get<VisualElement>().style.backgroundImage = new(item.Icon);
}
}
}
#endif