com.soviby.unity.ui.ugui-to.../Assets/Editor/Windows/PanelCacheWindow.cs
2024-10-08 23:19:05 +08:00

107 lines
3.6 KiB
C#

using System.Collections.Generic;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Unity.Mathematics;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Serialization;
namespace UguiToolkit.Editor.Windows
{
public class PanelCacheWindow : OdinEditorWindow
{
private static PanelCacheWindow _window;
[LabelText("源图片文件夹路径"), FolderPath, SerializeField]
private string m_srcImgDirPath;
[LabelText("目标图片信息文件(psd导出)"), Sirenix.OdinInspector.FilePath(Extensions = "layout.txt"), SerializeField]
private string m_layoutInfoFilePath;
private PrefabStage m_prefabStage;
private GameObject m_prefab;
private bool m_cacheExist;
private PanelCache panelCache;
[Button("开启助手", ButtonSizes.Medium)]
private void Start()
{
panelCache.srcImgDirPath = m_srcImgDirPath;
panelCache.layoutInfoFilePath = m_layoutInfoFilePath;
if (!m_cacheExist)
{
panelCache.rotScaleInfos = new();
CalcRotScaleInfos();
}
var cache = GlobalManager.Instance.cache;
cache.panelCaches[m_prefab] = panelCache;
UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot);
CloseWindow();
}
[Button("重新计算旋转和缩放信息", ButtonSizes.Medium), ShowIf(nameof(m_cacheExist))]
private void CalcRotScaleInfos()
{
// 执行cmd
var jsonData = CommandHelper.CalcRotScale(panelCache.srcImgDirPath, panelCache.TargetImgDirPath, panelCache.rotScaleInfos);
if (jsonData == null || jsonData.data == null) return;
var rotScaleInfos = panelCache.rotScaleInfos;
foreach (var kv in jsonData.data)
{
List<rotScaleInfoItem> rotScaleItemList = new();
rotScaleInfos[kv.Key] = rotScaleItemList;
foreach (var jsonItemData in kv.Value)
{
rotScaleItemList.Add(new ()
{
imgPath = jsonItemData.targetPath,
rotiation = jsonItemData.rot,
scale = new float2(jsonItemData.scale[0], jsonItemData.scale[1])
});
}
}
}
private void Init()
{
var cache = GlobalManager.Instance.cache;
if (cache.panelCaches.TryGetValue(m_prefab, out panelCache))
{
m_srcImgDirPath = panelCache.srcImgDirPath;
m_layoutInfoFilePath = panelCache.layoutInfoFilePath;
m_cacheExist = true;
}
else
{
panelCache = new PanelCache();
}
}
public static PanelCacheWindow ShowWindow(PrefabStage stage)
{
if (_window) _window.Close();
_window = CreateWindow<PanelCacheWindow>();
_window.titleContent = new ("请选择信息后开启助手");
_window.m_prefabStage = stage;
_window.m_prefab = AssetDatabase.LoadAssetAtPath<GameObject>(stage.assetPath);
_window.Show();
_window.Init();
return _window;
}
public static void CloseWindow()
{
if (_window)
{
_window.Close();
_window = null;
}
}
}
}