com.soviby.unity.ui.ugui-to.../Assets/Editor/Windows/PanelCacheWindow.cs

143 lines
5.0 KiB
C#
Raw Normal View History

2024-10-23 17:30:59 +00:00
#if UNITY_EDITOR
using System.Collections.Generic;
2024-10-09 16:12:59 +00:00
using System.IO;
2024-10-08 15:19:05 +00:00
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)
{
CalcRotScaleInfos();
2024-10-09 16:12:59 +00:00
PaserLayout();
2024-10-08 15:19:05 +00:00
}
var cache = GlobalManager.Instance.cache;
cache.panelCaches[m_prefab] = panelCache;
2024-10-21 16:20:39 +00:00
GlobalManager.Instance.SaveCache();
var stageManager = UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot);
2024-10-23 17:30:59 +00:00
var entityManager = stageManager.CreateSubManager<EntityManager>();
entityManager.InitAllEntity(panelCache);
2024-10-08 15:19:05 +00:00
CloseWindow();
}
2024-10-09 16:12:59 +00:00
[Button("解析Layout.txt", ButtonSizes.Medium), ShowIf(nameof(m_cacheExist))]
private void PaserLayout()
{
if (string.IsNullOrEmpty(m_layoutInfoFilePath))
{
Debug.LogError("layoutInfoFilePath 为空");
return;
}
ILayoutParser layoutParser = new DefaultLayoutParser();
using (StreamReader reader = File.OpenText(m_layoutInfoFilePath))
{
var jsonData = reader.ReadToEnd();
2024-10-21 16:20:39 +00:00
var layoutInfo = layoutParser.Parser(jsonData);
2024-10-23 17:30:59 +00:00
// 对img路径进行修正
foreach (var elementInfo in layoutInfo)
{
var imgInfo = elementInfo as LayoutInfo.ImageInfo;
if (imgInfo != null)
{
imgInfo.imgPath = System.IO.Path.Join(panelCache.TargetImgDirPath, imgInfo.imgPath) + ".png";
}
}
2024-10-21 16:20:39 +00:00
panelCache.layoutInfo = layoutInfo;
2024-10-09 16:12:59 +00:00
}
}
2024-10-08 15:19:05 +00:00
[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;
2024-10-21 16:20:39 +00:00
rotScaleInfos.Clear();
2024-10-08 15:19:05 +00:00
foreach (var kv in jsonData.data)
{
2024-10-08 16:32:08 +00:00
List<RotScaleInfoItem> rotScaleItemList = new();
2024-10-08 15:19:05 +00:00
rotScaleInfos[kv.Key] = rotScaleItemList;
foreach (var jsonItemData in kv.Value)
{
rotScaleItemList.Add(new ()
{
2024-10-21 16:20:39 +00:00
imgPath = Path.GetRelativePath(Application.dataPath, jsonItemData.targetPath),
2024-10-08 15:19:05 +00:00
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;
}
}
2024-10-09 16:12:59 +00:00
2024-10-08 15:19:05 +00:00
}
2024-10-23 17:30:59 +00:00
}
#endif