#if UNITY_EDITOR using System.Collections.Generic; using System.IO; 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 : BaseWindow { [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 DoStart() { panelCache.srcImgDirPath = m_srcImgDirPath; panelCache.layoutInfoFilePath = m_layoutInfoFilePath; if (!m_cacheExist) { CalcRotScaleInfos(); PaserLayout(); } var cache = GlobalManager.Instance.cache; cache.panelCaches[m_prefab] = panelCache; GlobalManager.Instance.SaveCache(); var stageManager = UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot); var entityManager = stageManager.CreateSubManager(); entityManager.InitAllEntity(panelCache); CloseWindow(); // 打开编辑界面 EditWindow.ShowWindow(null); } [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(); var layoutInfo = layoutParser.Parser(jsonData); // 对img路径进行修正 string projectPath = Directory.GetParent(Application.dataPath).FullName; 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"; imgInfo.imgPath = Path.GetRelativePath(projectPath, imgInfo.imgPath).Replace("\\", "/"); } } panelCache.layoutInfo = layoutInfo; } } [Button("重新计算旋转和缩放信息", ButtonSizes.Medium), ShowIf(nameof(m_cacheExist))] private void CalcRotScaleInfos() { // 执行cmd var jsonData = CommandHelper.CalcRotScale(panelCache.srcImgDirPath, panelCache.TargetImgDirPath); if (jsonData == null || jsonData.data == null) return; var rotScaleInfos = panelCache.rotScaleInfos; rotScaleInfos.Clear(); string projectPath = Directory.GetParent(Application.dataPath).FullName; foreach (var kv in jsonData.data) { List rotScaleItemList = new(); rotScaleInfos[Path.GetRelativePath(projectPath, kv.Key).Replace("\\", "/")] = rotScaleItemList; foreach (var jsonItemData in kv.Value) { rotScaleItemList.Add(new () { imgPath = Path.GetRelativePath(projectPath, jsonItemData.targetPath).Replace("\\", "/"), rotiation = jsonItemData.rot, scale = new float2(jsonItemData.scale[0], jsonItemData.scale[1]) }); } } } public override string GettitleContent() { return "请选择信息后开启助手"; } public override void Init(WindowArgs args = null) { var _args = args as PanelCacheWindowArgs; m_prefabStage = _args.stage; m_prefab = AssetDatabase.LoadAssetAtPath(_args.stage.assetPath); 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 class PanelCacheWindowArgs : WindowArgs { public PrefabStage stage; } } } #endif