Files
plugin-library/Assets/00.StaryEvoTools/Editor/BuildAsset/ChangePlayerMode/ChangePlayerMode.cs

183 lines
7.2 KiB
C#
Raw Normal View History

2025-03-31 11:16:52 +08:00
using System;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace Stary.Evo.Editor
{
2025-07-02 16:28:08 +08:00
public class ChangePlayerSchema
2025-03-31 11:16:52 +08:00
{
public static PLayerMode PLayerMode
{
get => _pLayerMode;
set => SetPlayerMode(value);
}
private static PLayerMode _pLayerMode;
2026-04-16 22:04:55 +08:00
// 主模式菜单路径
2025-09-10 10:53:08 +08:00
private const string EditorSimulateMode = "Evo/Schema/ChangePlayer/EditorSimulateMode(编辑器调试模式)";
2026-01-05 17:58:53 +08:00
private const string HostPlayMode = "Evo/Schema/ChangePlayer/HostPlayMode(联机运行模式)";
2026-03-18 10:29:55 +08:00
private const string LocalPlayMode = "Evo/Schema/ChangePlayer/LocalPlayMode(本地运行模式)";
2026-04-16 23:48:13 +08:00
private const string WebPlayMode = "Evo/Schema/ChangePlayer/WebPlayMode(Web运行模式)";
2026-04-16 22:04:55 +08:00
// Web 子模式菜单路径(作为 WebPlayMode 的子菜单)
private const string WebNormalSubMode = "Evo/Schema/ChangePlayer/WebPlayMode(Web运行模式)/WebGL 普通模式";
private const string WechatMiniSubMode = "Evo/Schema/ChangePlayer/WebPlayMode(Web运行模式)/微信小程序模式";
#region
2025-03-31 11:16:52 +08:00
2026-04-16 22:04:55 +08:00
[MenuItem(EditorSimulateMode, false, 3)]
2025-03-31 11:16:52 +08:00
private static void SetEditorMode() => SetPlayerMode(PLayerMode.EDITOR_SIMULATEMODE);
2026-04-16 22:04:55 +08:00
[MenuItem(HostPlayMode, false, 3)]
2025-03-31 11:16:52 +08:00
private static void SetHostMode() => SetPlayerMode(PLayerMode.HOST_PLAYMODE);
2026-04-16 22:04:55 +08:00
[MenuItem(LocalPlayMode, false, 3)]
2026-03-18 10:29:55 +08:00
private static void SetLocalMode() => SetPlayerMode(PLayerMode.LOCAL_PLAYMODE);
2026-04-16 23:48:13 +08:00
2026-04-16 22:04:55 +08:00
#endregion
2025-03-31 11:16:52 +08:00
2026-04-16 22:04:55 +08:00
#region Web
[MenuItem(WebNormalSubMode, false, 4)]
private static void SetWebNormalMode() => SetWebSubMode(WebSubMode.WEB_NORMAL);
[MenuItem(WechatMiniSubMode, false, 4)]
private static void SetWechatMiniMode() => SetWebSubMode(WebSubMode.WECHAT_MINIGAME);
#endregion
#region
[MenuItem(EditorSimulateMode, true, 3)]
[MenuItem(HostPlayMode, true, 3)]
[MenuItem(LocalPlayMode, true, 3)]
private static bool ValidateMainModeMenu()
2025-03-31 11:16:52 +08:00
{
2026-01-07 18:20:13 +08:00
string platform = CustomEditorPrefs.GetString("ChangePlayerSchema");
2025-03-31 11:16:52 +08:00
Menu.SetChecked(EditorSimulateMode, platform == PLayerMode.EDITOR_SIMULATEMODE.ToString());
Menu.SetChecked(HostPlayMode, platform == PLayerMode.HOST_PLAYMODE.ToString());
2026-03-18 10:29:55 +08:00
Menu.SetChecked(LocalPlayMode, platform == PLayerMode.LOCAL_PLAYMODE.ToString());
2025-03-31 11:16:52 +08:00
return true;
}
2026-04-16 23:48:13 +08:00
[MenuItem(WebNormalSubMode, false)]
2026-04-16 22:04:55 +08:00
private static bool ValidateWebNormalMenu()
{
// 只在 WebPlayMode 下启用子菜单
bool isWebMode = _pLayerMode == PLayerMode.WEB_PLAYMODE;
string subMode = CustomEditorPrefs.GetString("WebSubMode", WebSubMode.WEB_NORMAL.ToString());
Menu.SetChecked(WebNormalSubMode, subMode == WebSubMode.WEB_NORMAL.ToString());
return isWebMode;
}
2026-04-16 23:48:13 +08:00
[MenuItem(WechatMiniSubMode, false)]
2026-04-16 22:04:55 +08:00
private static bool ValidateWechatMiniMenu()
{
bool isWebMode = _pLayerMode == PLayerMode.WEB_PLAYMODE;
string subMode = CustomEditorPrefs.GetString("WebSubMode", WebSubMode.WEB_NORMAL.ToString());
Menu.SetChecked(WechatMiniSubMode, subMode == WebSubMode.WECHAT_MINIGAME.ToString());
return isWebMode;
}
#endregion
2025-07-02 16:28:08 +08:00
public static void SetPlayerMode(PLayerMode mode)
2025-03-31 11:16:52 +08:00
{
var currentTarget = EditorUserBuildSettings.selectedBuildTargetGroup;
if (currentTarget == BuildTargetGroup.Unknown) return;
2026-04-16 22:04:55 +08:00
// 获取当前宏定义并清理所有相关的模式定义(包括主模式和子模式)
2025-03-31 11:16:52 +08:00
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentTarget)
.Split(';')
2026-04-16 22:04:55 +08:00
.Where(d => !string.IsNullOrEmpty(d))
2025-03-31 11:16:52 +08:00
.Where(d => !Enum.GetNames(typeof(PLayerMode)).Contains(d))
2026-04-16 22:04:55 +08:00
.Where(d => !Enum.GetNames(typeof(WebSubMode)).Contains(d))
2025-03-31 11:16:52 +08:00
.ToList();
// 添加新模式
defines.Add(mode.ToString());
2026-04-16 22:04:55 +08:00
2025-03-31 11:16:52 +08:00
_pLayerMode = mode;
2026-01-07 18:20:13 +08:00
CustomEditorPrefs.SetString("ChangePlayerSchema", _pLayerMode.ToString());
2026-04-16 22:04:55 +08:00
// 如果切换到 Web 模式,恢复之前保存的子模式或默认使用普通模式
if (mode == PLayerMode.WEB_PLAYMODE)
{
string savedSubMode = CustomEditorPrefs.GetString("WebSubMode", WebSubMode.WEB_NORMAL.ToString());
defines.Add(savedSubMode);
}
ApplyDefines(defines);
Debug.Log($"[ChangePlayerSchema] 切换至主模式: {mode}");
}
public static void SetWebSubMode(WebSubMode subMode)
{
if (_pLayerMode != PLayerMode.WEB_PLAYMODE)
{
Debug.LogWarning("[ChangePlayerSchema] 请先切换到 WebPlayMode 再设置子模式");
return;
}
var currentTarget = EditorUserBuildSettings.selectedBuildTargetGroup;
// 清理现有子模式宏,保留其他所有宏
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentTarget)
.Split(';')
.Where(d => !string.IsNullOrEmpty(d))
.Where(d => !Enum.GetNames(typeof(WebSubMode)).Contains(d))
.ToList();
// 添加新的子模式宏
defines.Add(subMode.ToString());
CustomEditorPrefs.SetString("WebSubMode", subMode.ToString());
ApplyDefines(defines);
Debug.Log($"[ChangePlayerSchema] Web 子模式切换至: {subMode}");
}
private static void ApplyDefines(System.Collections.Generic.List<string> defines)
{
var currentTarget = EditorUserBuildSettings.selectedBuildTargetGroup;
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTarget, string.Join(";", defines));
2025-03-31 11:16:52 +08:00
AssetDatabase.Refresh();
EditorApplication.delayCall += () =>
{
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
2026-04-16 22:04:55 +08:00
Debug.Log($"[ChangePlayerSchema] 当前编译符号: {string.Join(";", defines)}");
2025-03-31 11:16:52 +08:00
};
}
2026-04-16 22:04:55 +08:00
// 初始化时恢复上次保存的模式(可在 Editor 初始化时调用)
[InitializeOnLoadMethod]
private static void RestoreMode()
{
string savedMode = CustomEditorPrefs.GetString("ChangePlayerSchema", PLayerMode.EDITOR_SIMULATEMODE.ToString());
if (Enum.TryParse<PLayerMode>(savedMode, out var mode))
{
_pLayerMode = mode;
}
}
2025-03-31 11:16:52 +08:00
}
2026-04-16 22:04:55 +08:00
// 主模式枚举
2025-03-31 11:16:52 +08:00
public enum PLayerMode
{
2026-04-16 22:04:55 +08:00
EDITOR_SIMULATEMODE, // 编辑器调试模式
HOST_PLAYMODE, // 联机运行模式
LOCAL_PLAYMODE, // 本地运行模式
WEB_PLAYMODE // Web 运行模式(包含子模式)
}
// Web 子模式枚举
public enum WebSubMode
{
WEB_NORMAL, // WebGL 普通模式
WECHAT_MINIGAME // 微信小程序模式
2025-03-31 11:16:52 +08:00
}
}