mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
ToolbarExtender
ToolbarExtender
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 20dbf6614352aaf488de29690deeab68
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb0502859d7561142a3408d0ce2a19d8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,92 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityToolbarExtender;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public class EditorPlayMode
|
||||
{
|
||||
static class ToolbarStyles
|
||||
{
|
||||
public static readonly GUIStyle ToolBarExtenderBtnStyle;
|
||||
|
||||
public static readonly GUIStyle ToolBarTextStyle;
|
||||
|
||||
public static readonly GUIStyle ToolBarButtonGuiStyle;
|
||||
|
||||
static ToolbarStyles()
|
||||
{
|
||||
ToolBarExtenderBtnStyle = new GUIStyle("Command")
|
||||
{
|
||||
fontSize = 12,
|
||||
alignment = TextAnchor.MiddleCenter,
|
||||
imagePosition = ImagePosition.ImageAbove,
|
||||
fontStyle = FontStyle.Normal,
|
||||
fixedWidth = 60
|
||||
};
|
||||
|
||||
ToolBarTextStyle = new GUIStyle(BUTTON_STYLE_NAME)
|
||||
{
|
||||
padding = new RectOffset(2, 8, 2, 2),
|
||||
alignment = TextAnchor.MiddleCenter,
|
||||
fontStyle = FontStyle.Bold
|
||||
};
|
||||
|
||||
ToolBarButtonGuiStyle = new GUIStyle(BUTTON_STYLE_NAME)
|
||||
{
|
||||
padding = new RectOffset(2, 8, 2, 2),
|
||||
alignment = TextAnchor.MiddleCenter,
|
||||
fontStyle = FontStyle.Bold
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static EditorPlayMode()
|
||||
{
|
||||
ToolbarExtender.RightToolbarGUI.Add(OnToolbarGUI);
|
||||
_resourceModeIndex = EditorPrefs.GetInt("EditorPlayMode", 0);
|
||||
}
|
||||
|
||||
private const string BUTTON_STYLE_NAME = "Tab middle";
|
||||
static GUIStyle _buttonGuiStyle;
|
||||
|
||||
private static readonly string[] _resourceModeNames =
|
||||
{
|
||||
"EditorMode (编辑器下的模拟模式)",
|
||||
"OfflinePlayMode (单机模式)",
|
||||
"HostPlayMode (联机运行模式)",
|
||||
"WebPlayMode (WebGL运行模式)"
|
||||
};
|
||||
|
||||
private static int _resourceModeIndex = 0;
|
||||
public static int ResourceModeIndex => _resourceModeIndex;
|
||||
|
||||
static void OnToolbarGUI()
|
||||
{
|
||||
EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
|
||||
{
|
||||
// GUILayout.Label("资源加载模式:",ToolbarStyles.ToolBarTextStyle);
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
GUILayout.FlexibleSpace();
|
||||
|
||||
// 资源模式
|
||||
int selectedIndex = EditorGUILayout.Popup("", _resourceModeIndex, _resourceModeNames, ToolbarStyles.ToolBarButtonGuiStyle);
|
||||
// ReSharper disable once RedundantCheckBeforeAssignment
|
||||
if (selectedIndex != _resourceModeIndex)
|
||||
{
|
||||
Debug.Log($"更改编辑器资源运行模式 : {_resourceModeNames[selectedIndex]}");
|
||||
_resourceModeIndex = selectedIndex;
|
||||
EditorPrefs.SetInt("EditorPlayMode", selectedIndex);
|
||||
}
|
||||
|
||||
GUILayout.FlexibleSpace();
|
||||
|
||||
GUILayout.Space(400);
|
||||
}
|
||||
EditorGUI.EndDisabledGroup();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab26dd2998d84a08b35c0132e1814199
|
||||
timeCreated: 1683857308
|
@@ -0,0 +1,126 @@
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityToolbarExtender;
|
||||
|
||||
namespace TEngine.SceneLauncher
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public class SceneSwitchLeftButton
|
||||
{
|
||||
private const string PreviousSceneKey = "TEngine_PreviousScenePath"; // 用于存储之前场景路径的键
|
||||
private const string IsLauncherBtn = "TEngine_IsLauncher"; // 用于存储之前是否按下launcher
|
||||
|
||||
private static readonly string SceneMain = "main";
|
||||
|
||||
private static readonly string ButtonStyleName = "Tab middle";
|
||||
private static GUIStyle _buttonGuiStyle;
|
||||
|
||||
static SceneSwitchLeftButton()
|
||||
{
|
||||
ToolbarExtender.LeftToolbarGUI.Add(OnToolbarGUI);
|
||||
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
|
||||
EditorApplication.quitting += OnEditorQuit;
|
||||
}
|
||||
|
||||
private static void OnToolbarGUI()
|
||||
{
|
||||
_buttonGuiStyle ??= new GUIStyle(ButtonStyleName)
|
||||
{
|
||||
padding = new RectOffset(2, 8, 2, 2),
|
||||
alignment = TextAnchor.MiddleCenter,
|
||||
fontStyle = FontStyle.Bold
|
||||
};
|
||||
|
||||
GUILayout.FlexibleSpace();
|
||||
if (GUILayout.Button(
|
||||
new GUIContent("Launcher", EditorGUIUtility.FindTexture("PlayButton"), "Start Scene Launcher"),
|
||||
_buttonGuiStyle))
|
||||
SceneHelper.StartScene(SceneMain);
|
||||
}
|
||||
|
||||
private static void OnPlayModeStateChanged(PlayModeStateChange state)
|
||||
{
|
||||
if (state == PlayModeStateChange.EnteredEditMode)
|
||||
{
|
||||
// 从 EditorPrefs 读取之前的场景路径
|
||||
var previousScenePath = EditorPrefs.GetString(PreviousSceneKey, string.Empty);
|
||||
if (!string.IsNullOrEmpty(previousScenePath) && EditorPrefs.GetBool(IsLauncherBtn))
|
||||
{
|
||||
EditorApplication.delayCall += () =>
|
||||
{
|
||||
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
|
||||
EditorSceneManager.OpenScene(previousScenePath);
|
||||
};
|
||||
}
|
||||
|
||||
EditorPrefs.SetBool(IsLauncherBtn, false);
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnEditorQuit()
|
||||
{
|
||||
EditorPrefs.SetString(PreviousSceneKey, "");
|
||||
EditorPrefs.SetBool(IsLauncherBtn, false);
|
||||
}
|
||||
|
||||
private static class SceneHelper
|
||||
{
|
||||
private static string _sceneToOpen;
|
||||
|
||||
public static void StartScene(string sceneName)
|
||||
{
|
||||
if (EditorApplication.isPlaying) EditorApplication.isPlaying = false;
|
||||
|
||||
// 记录当前场景路径到 EditorPrefs
|
||||
var activeScene = SceneManager.GetActiveScene();
|
||||
if (activeScene.isLoaded && activeScene.name != SceneMain)
|
||||
{
|
||||
EditorPrefs.SetString(PreviousSceneKey, activeScene.path);
|
||||
EditorPrefs.SetBool(IsLauncherBtn, true);
|
||||
}
|
||||
|
||||
_sceneToOpen = sceneName;
|
||||
EditorApplication.update += OnUpdate;
|
||||
}
|
||||
|
||||
private static void OnUpdate()
|
||||
{
|
||||
if (_sceneToOpen == null ||
|
||||
EditorApplication.isPlaying || EditorApplication.isPaused ||
|
||||
EditorApplication.isCompiling || EditorApplication.isPlayingOrWillChangePlaymode)
|
||||
return;
|
||||
|
||||
EditorApplication.update -= OnUpdate;
|
||||
|
||||
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
|
||||
{
|
||||
string[] guids = AssetDatabase.FindAssets("t:scene " + _sceneToOpen, null);
|
||||
if (guids.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("Couldn't find scene file");
|
||||
}
|
||||
else
|
||||
{
|
||||
string scenePath = null;
|
||||
// 优先打开完全匹配_sceneToOpen的场景
|
||||
for (var i = 0; i < guids.Length; i++)
|
||||
{
|
||||
scenePath = AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
if (scenePath.EndsWith("/" + _sceneToOpen + ".unity")) break;
|
||||
}
|
||||
|
||||
// 如果没有完全匹配的场景,默认显示找到的第一个场景
|
||||
if (string.IsNullOrEmpty(scenePath)) scenePath = AssetDatabase.GUIDToAssetPath(guids[0]);
|
||||
|
||||
EditorSceneManager.OpenScene(scenePath);
|
||||
EditorApplication.isPlaying = true;
|
||||
}
|
||||
}
|
||||
|
||||
_sceneToOpen = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f6d2ca3d3b34950a79294e49dd9d016
|
||||
timeCreated: 1742389721
|
@@ -0,0 +1,115 @@
|
||||
#if UNITY_EDITOR
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEditor.SceneManagement;
|
||||
|
||||
namespace UnityToolbarExtender.Examples
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public sealed class SceneSwitchLeftButton
|
||||
{
|
||||
private static List<(string sceneName, string scenePath)> m_Scenes;
|
||||
private static string[] m_SceneName;
|
||||
private static string[] m_ScenePath;
|
||||
private static int sceneSelected = 0;
|
||||
|
||||
static SceneSwitchLeftButton()
|
||||
{
|
||||
EditorApplication.projectChanged += UpdateCurrent;
|
||||
UpdateCurrent();
|
||||
ToolbarExtender.RightToolbarGUI.Add(OnToolbarGUI);
|
||||
}
|
||||
|
||||
static void UpdateCurrent()
|
||||
{
|
||||
m_Scenes = SceneSwitcher.GetAllScenesInProject();
|
||||
m_SceneName = new string[m_Scenes.Count];
|
||||
m_ScenePath = new string[m_Scenes.Count];
|
||||
for (int i = 0; i < m_Scenes.Count; i++)
|
||||
{
|
||||
var (name, path) = m_Scenes[i];
|
||||
m_SceneName[i] = name;
|
||||
m_ScenePath[i] = path;
|
||||
if (SceneManager.GetActiveScene().path == path)
|
||||
sceneSelected = i;
|
||||
}
|
||||
}
|
||||
|
||||
static void OnToolbarGUI()
|
||||
{
|
||||
if (sceneSelected >= m_SceneName.Length) //空项目0场景判断
|
||||
return;
|
||||
var size = EditorStyles.popup.CalcSize(new GUIContent(m_SceneName[sceneSelected]));
|
||||
// 创建水平布局
|
||||
//EditorGUILayout.BeginHorizontal();
|
||||
|
||||
// 将控件推到左边和右边
|
||||
//GUILayout.FlexibleSpace(); // 先占用左边的所有空间
|
||||
EditorGUILayout.LabelField("当前场景:", GUILayout.Width(55));
|
||||
int sceneSelectedNew = EditorGUILayout.Popup(sceneSelected, m_SceneName, GUILayout.Width(size.x + 5f),
|
||||
GUILayout.MinWidth(55));
|
||||
GUILayout.FlexibleSpace();
|
||||
// 结束水平布局
|
||||
//EditorGUILayout.EndHorizontal();
|
||||
if (sceneSelectedNew != sceneSelected)
|
||||
{
|
||||
sceneSelected = sceneSelectedNew;
|
||||
SceneSwitcher.PromptSaveCurrentScene();
|
||||
EditorSceneManager.OpenScene(m_ScenePath[sceneSelectedNew]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class SceneSwitcher
|
||||
{
|
||||
public static bool PromptSaveCurrentScene()
|
||||
{
|
||||
// 检查当前场景是否已保存
|
||||
if (SceneManager.GetActiveScene().isDirty)
|
||||
{
|
||||
// 提示用户是否要保存当前场景
|
||||
bool saveScene = EditorUtility.DisplayDialog(
|
||||
"Save Current Scene",
|
||||
"The current scene has unsaved changes. Do you want to save it?",
|
||||
"Save",
|
||||
"Cancel"
|
||||
);
|
||||
|
||||
// 如果用户选择“保存”,则保存当前场景
|
||||
if (saveScene)
|
||||
{
|
||||
EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
|
||||
}
|
||||
|
||||
return saveScene;
|
||||
}
|
||||
|
||||
// 如果场景已保存或者用户选择了“取消”,则返回 true,表示继续执行后续操作
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取项目中所有的场景文件,并以 (场景名, 场景路径) 的形式返回。
|
||||
/// </summary>
|
||||
public static List<(string sceneName, string scenePath)> GetAllScenesInProject()
|
||||
{
|
||||
List<(string sceneName, string scenePath)> scenes = new List<(string sceneName, string scenePath)>();
|
||||
|
||||
// 查找所有场景文件
|
||||
string[] guids = AssetDatabase.FindAssets("t:Scene");
|
||||
for (int i = 0; i < guids.Length; i++)
|
||||
{
|
||||
var guid = guids[i];
|
||||
string path = AssetDatabase.GUIDToAssetPath(guid);
|
||||
string sceneName = $"{i + 1}_{Path.GetFileNameWithoutExtension(path)}";
|
||||
scenes.Add((sceneName, path));
|
||||
}
|
||||
|
||||
return scenes;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45017e4df12424c4fb16db4708d239f0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user