105 lines
3.2 KiB
C#
105 lines
3.2 KiB
C#
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEditor.Callbacks;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
|
|
namespace Aliciza.UXTool
|
|
{
|
|
public static class UXDesinUtil
|
|
{
|
|
private static readonly string DesinLayout = Path.Combine(Def_UXGUIPath.ConfigPath, "DesignLayout.wlt");
|
|
private const string InDesignKey = "AlicizaUXTool_InDesign";
|
|
|
|
public static bool InDesign
|
|
{
|
|
get => EditorPrefs.GetBool(InDesignKey, false);
|
|
private set => EditorPrefs.SetBool(InDesignKey, value);
|
|
}
|
|
|
|
public static event System.Action OnEnterDesignMode;
|
|
public static event System.Action OnExitDesignMode;
|
|
|
|
[InitializeOnLoadMethod]
|
|
private static void Initialize()
|
|
{
|
|
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
|
|
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
|
}
|
|
|
|
private static void OnPlayModeStateChanged(PlayModeStateChange mode)
|
|
{
|
|
if (mode == PlayModeStateChange.EnteredPlayMode && InDesign)
|
|
{
|
|
ExitUIDesinger();
|
|
}
|
|
}
|
|
|
|
|
|
public static void OpenUIDesinger()
|
|
{
|
|
if (InDesign) return;
|
|
if (File.Exists(DesinLayout))
|
|
{
|
|
InDesign = true;
|
|
EditorUtility.LoadWindowLayout(DesinLayout);
|
|
PrefabStageUtils.OpenDefaultStage();
|
|
ClearProjectSettings();
|
|
OnEnterDesignMode?.Invoke();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("指定的布局文件不存在: " + DesinLayout);
|
|
}
|
|
}
|
|
|
|
public static void ExitUIDesinger()
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Def_UXGUIPath.DefaultLayoutPath))
|
|
{
|
|
InDesign = false; // 持久化状态
|
|
PrefabStageUtils.ExitStage();
|
|
EditorUtility.LoadWindowLayout(Def_UXGUIPath.DefaultLayoutPath);
|
|
OnExitDesignMode?.Invoke(); // 通知退出设计模式
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("没有找到已保存的布局");
|
|
}
|
|
}
|
|
|
|
[InitializeOnLoadMethod]
|
|
[DidReloadScripts]
|
|
private static void RestoreState()
|
|
{
|
|
// 在域重载后检查状态,如果需要恢复到设计模式
|
|
if (InDesign && File.Exists(DesinLayout))
|
|
{
|
|
var currentStage = PrefabStageUtility.GetCurrentPrefabStage();
|
|
if (currentStage != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
EditorApplication.delayCall += () =>
|
|
{
|
|
EditorUtility.LoadWindowLayout(DesinLayout);
|
|
PrefabStageUtils.OpenDefaultStage();
|
|
OnEnterDesignMode?.Invoke();
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空 Project Settings 中的特定内容
|
|
/// </summary>
|
|
private static void ClearProjectSettings()
|
|
{
|
|
UnityEditor.EditorSettings.prefabUIEnvironment = null;
|
|
UnityEditor.EditorSettings.prefabRegularEnvironment = null;
|
|
}
|
|
}
|
|
}
|