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

90 lines
3.4 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;
2025-09-10 10:53:08 +08:00
private const string EditorSimulateMode = "Evo/Schema/ChangePlayer/EditorSimulateMode(编辑器调试模式)";
private const string OfflinePlayMode = "Evo/Schema/ChangePlayer/OfflinePlayMode(本地运行模式)";
private const string HostPlayMode = "Evo/Schema/ChangePlayer/HostPlayMode(服务器运行模式)";
private const string WebPlayMode = "Evo/Schema/ChangePlayer/WebPlayMode(Web运行模式)";
2025-03-31 11:16:52 +08:00
[MenuItem(EditorSimulateMode)]
private static void SetEditorMode() => SetPlayerMode(PLayerMode.EDITOR_SIMULATEMODE);
[MenuItem(OfflinePlayMode)]
private static void SetOfflineMode() => SetPlayerMode(PLayerMode.OFFLINE_PLAYMODE);
[MenuItem(HostPlayMode)]
private static void SetHostMode() => SetPlayerMode(PLayerMode.HOST_PLAYMODE);
2025-07-02 16:28:08 +08:00
// [MenuItem(WebPlayMode)]
// private static void SetWebMode() => SetPlayerMode(PLayerMode.WEB_PLAYMODE);
2025-03-31 11:16:52 +08:00
[MenuItem(EditorSimulateMode, true)]
private static bool ValidateModeMenu()
{
2025-07-02 16:28:08 +08:00
string platform = EditorPrefs.GetString("ChangePlayerSchema");
2025-03-31 11:16:52 +08:00
Menu.SetChecked(EditorSimulateMode, platform == PLayerMode.EDITOR_SIMULATEMODE.ToString());
Menu.SetChecked(OfflinePlayMode, platform == PLayerMode.OFFLINE_PLAYMODE.ToString());
Menu.SetChecked(HostPlayMode, platform == PLayerMode.HOST_PLAYMODE.ToString());
2025-07-02 16:28:08 +08:00
//Menu.SetChecked(WebPlayMode, platform == PLayerMode.WEB_PLAYMODE.ToString());
2025-03-31 11:16:52 +08:00
Debug.LogError("CheckPlatform");
return true;
}
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;
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(currentTarget)
.Split(';')
.Where(d => !Enum.GetNames(typeof(PLayerMode)).Contains(d))
.ToList();
// 添加新模式
defines.Add(mode.ToString());
PlayerSettings.SetScriptingDefineSymbolsForGroup(currentTarget, string.Join(";", defines));
Debug.Log($"当前编译符号: {string.Join(";", defines)}"); // 添加调试日志
_pLayerMode = mode;
2025-07-02 16:28:08 +08:00
EditorPrefs.SetString("ChangePlayerSchema", _pLayerMode.ToString());
ValidateModeMenu();
2025-03-31 11:16:52 +08:00
AssetDatabase.Refresh();
// 添加解决方案文件重新生成逻辑
EditorApplication.delayCall += () =>
{
EditorApplication.ExecuteMenuItem("Assets/Open C# Project");
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
Debug.Log("已强制重新生成解决方案文件");
};
}
}
public enum PLayerMode
{
//编辑仿真模式,
EDITOR_SIMULATEMODE,
//单机运行模式
OFFLINE_PLAYMODE,
//联机运行模式
HOST_PLAYMODE,
2025-07-02 16:28:08 +08:00
// //web运行模式
// WEB_PLAYMODE
2025-03-31 11:16:52 +08:00
}
}