com.soviby.unity.ui.ugui-to.../Assets/Editor/Windows/PanelCacheWindow.cs
2024-12-10 18:14:38 +08:00

116 lines
3.6 KiB
C#

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Unity.Mathematics;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace UguiToolkit.Editor.Windows
{
public class PanelCacheWindow : BaseWindow<PanelCacheWindow>
{
[LabelText("是否竖版"), OnValueChanged(nameof(OnIsVerticalChanged)),SerializeField]
private bool m_isVertical = false;
[Title("psd导出数据设置")]
[LabelText("layout.txt文件"), Sirenix.OdinInspector.FilePath(AbsolutePath = true, Extensions = "layout.txt"), SerializeField]
private string m_layoutInfoFilePath;
private PrefabStage m_prefabStage;
private GameObject m_prefab;
private PanelCache m_panelCache;
[Button("开启助手", ButtonSizes.Medium)]
private void DoStart()
{
if (string.IsNullOrEmpty(m_layoutInfoFilePath))
{
EditorUtility.DisplayDialog("提示", "请先填写配置", "好的");
return;
}
var stageManager = UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot, m_prefab);
stageManager.gameObject.AddComponent<UnityMainThreadDispatcher>();
// 打开编辑界面
EditWindow.ShowWindow(null);
var targetImgDirPath = PanelCache.GetTargetImgDirPath(m_layoutInfoFilePath);
var panelCache = new PanelCache(m_layoutInfoFilePath, m_isVertical);
panelCache.layoutInfo = CacheScriptObject.PaserLayout(m_layoutInfoFilePath, targetImgDirPath);
if (m_panelCache != null)
{
panelCache.Copy(m_panelCache);
// 清理多余数据
panelCache.InitRotScaleInfos();
}
// 保存缓存
GlobalManager.Instance.SaveCache(m_prefab, panelCache);
var entityManager = stageManager.CreateSubManager<EntityManager>();
// stageManager.CreateSubManager<SelectionManager>();
entityManager.Init(panelCache);
CloseWindow();
}
[Button("清理缓存", ButtonSizes.Medium)]
private void ClearCache()
{
if (m_panelCache != null)
{
m_panelCache.ClearRotScaleInfos();
GlobalManager.Instance.SaveCache(m_prefab, m_panelCache);
}
}
private void OnIsVerticalChanged()
{
UpdatePanelCache();
}
private void UpdatePanelCache()
{
var cache = GlobalManager.Instance.cache;
var panelCache = GlobalManager.Instance.GetCache(m_prefab, m_isVertical);
if (panelCache != null)
{
m_layoutInfoFilePath = panelCache.layoutInfoFilePath;
m_panelCache = panelCache;
}
else {
m_layoutInfoFilePath = null;
m_panelCache = null;
}
}
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<GameObject>(_args.stage.assetPath);
UpdatePanelCache();
}
public class PanelCacheWindowArgs : WindowArgs
{
public PrefabStage stage;
}
}
}
#endif