update
This commit is contained in:
parent
171ba7b2d6
commit
7137cc0c81
@ -1,26 +0,0 @@
|
|||||||
// using Paps.UnityToolbarExtenderUIToolkit;
|
|
||||||
// using UnityEditor;
|
|
||||||
// using UnityEngine;
|
|
||||||
// using UnityEngine.UIElements;
|
|
||||||
//
|
|
||||||
// [MainToolbarElement(id: "BuildSettingWindow", alignment: ToolbarAlign.Right, order: 3)]
|
|
||||||
// public class BuildSettingWindow : IMGUIContainer
|
|
||||||
// {
|
|
||||||
// private GUIContent buildBtContent;
|
|
||||||
//
|
|
||||||
// public void InitializeElement()
|
|
||||||
// {
|
|
||||||
// buildBtContent = EditorGUIUtility.TrTextContentWithIcon("Build App/Hotfix", "打新包/打热更", EditorGUIUtility.IconContent("d_BuildSettings.Standalone").image);
|
|
||||||
// onGUIHandler = MyGUIMethod;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void MyGUIMethod()
|
|
||||||
// {
|
|
||||||
// GUILayout.BeginHorizontal();
|
|
||||||
//
|
|
||||||
// if (GUILayout.Button(buildBtContent, EditorStyles.toolbarButton, GUILayout.MaxWidth(125)))
|
|
||||||
// Debug.Log("GUI Button clicked");
|
|
||||||
//
|
|
||||||
// GUILayout.EndHorizontal();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: b7b9f8865a200e04b81804580b6d1de1
|
|
||||||
timeCreated: 1741240351
|
|
||||||
24
Editor/ToolBarExtension/EditorToolFunctionAttribute.cs
Normal file
24
Editor/ToolBarExtension/EditorToolFunctionAttribute.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace AlicizaX.Editor.Extension
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
|
||||||
|
public class EditorToolFunctionAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string ToolMenuPath { get; private set; }
|
||||||
|
public int MenuOrder { get; private set; }
|
||||||
|
public MethodInfo MethodInfo { get; private set; }
|
||||||
|
|
||||||
|
public EditorToolFunctionAttribute(string menu, int menuOrder = 0)
|
||||||
|
{
|
||||||
|
this.ToolMenuPath = menu;
|
||||||
|
MenuOrder = menuOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetMethodInfo(MethodInfo methodInfo)
|
||||||
|
{
|
||||||
|
MethodInfo = methodInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b242a864126249459c576ae04b84bd3c
|
||||||
|
timeCreated: 1775562565
|
||||||
8
Editor/ToolBarExtension/New.meta
Normal file
8
Editor/ToolBarExtension/New.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 826b6ceb6b80b0e4da54e2d8c6178bfd
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
161
Editor/ToolBarExtension/New/EditorQuickToolbarDropdown.cs
Normal file
161
Editor/ToolBarExtension/New/EditorQuickToolbarDropdown.cs
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
#if UNITY_6000_3_OR_NEWER
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEditor.Toolbars;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace AlicizaX.Editor.Extension
|
||||||
|
{
|
||||||
|
[InitializeOnLoad]
|
||||||
|
public static class EditorQuickToolbarDropdown
|
||||||
|
{
|
||||||
|
private const string ElementPath = "AlicizaX/EditorQuickTools";
|
||||||
|
private const string Tooltip = "Open editor quick tools";
|
||||||
|
|
||||||
|
private static readonly Texture2D ToolIcon;
|
||||||
|
private static readonly List<ToolEntry> ToolEntries;
|
||||||
|
|
||||||
|
private sealed class ToolEntry
|
||||||
|
{
|
||||||
|
public string MenuPath;
|
||||||
|
public int MenuOrder;
|
||||||
|
public MethodInfo MethodInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
static EditorQuickToolbarDropdown()
|
||||||
|
{
|
||||||
|
ToolIcon = GetIcon("CustomTool") ?? GetIcon("Settings");
|
||||||
|
ToolEntries = CollectToolEntries();
|
||||||
|
}
|
||||||
|
|
||||||
|
[MainToolbarElement(ElementPath, defaultDockPosition = MainToolbarDockPosition.Right, defaultDockIndex = 1)]
|
||||||
|
public static MainToolbarElement CreateElement()
|
||||||
|
{
|
||||||
|
return new MainToolbarDropdown(
|
||||||
|
new MainToolbarContent("Tools", ToolIcon, Tooltip),
|
||||||
|
ShowDropdownMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ShowDropdownMenu(Rect dropdownRect)
|
||||||
|
{
|
||||||
|
var menu = new GenericMenu();
|
||||||
|
|
||||||
|
if (ToolEntries.Count == 0)
|
||||||
|
{
|
||||||
|
menu.AddDisabledItem(new GUIContent("No tools found"));
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var toolEntry in ToolEntries)
|
||||||
|
{
|
||||||
|
var capturedToolEntry = toolEntry;
|
||||||
|
menu.AddItem(
|
||||||
|
new GUIContent(capturedToolEntry.MenuPath),
|
||||||
|
false,
|
||||||
|
() => InvokeTool(capturedToolEntry));
|
||||||
|
}
|
||||||
|
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ToolEntry> CollectToolEntries()
|
||||||
|
{
|
||||||
|
var toolEntries = new List<ToolEntry>();
|
||||||
|
|
||||||
|
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||||
|
{
|
||||||
|
if (assembly.FullName.Contains("Sirenix", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type[] types;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
types = assembly.GetTypes();
|
||||||
|
}
|
||||||
|
catch (ReflectionTypeLoadException exception)
|
||||||
|
{
|
||||||
|
types = exception.Types.Where(type => type != null).ToArray();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var type in types)
|
||||||
|
{
|
||||||
|
MethodInfo[] methods;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var method in methods)
|
||||||
|
{
|
||||||
|
var attribute = method.GetCustomAttribute<EditorToolFunctionAttribute>();
|
||||||
|
if (attribute == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
toolEntries.Add(new ToolEntry
|
||||||
|
{
|
||||||
|
MenuPath = attribute.ToolMenuPath,
|
||||||
|
MenuOrder = attribute.MenuOrder,
|
||||||
|
MethodInfo = method
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toolEntries.Sort((left, right) =>
|
||||||
|
{
|
||||||
|
var orderCompare = left.MenuOrder.CompareTo(right.MenuOrder);
|
||||||
|
return orderCompare != 0
|
||||||
|
? orderCompare
|
||||||
|
: string.Compare(left.MenuPath, right.MenuPath, StringComparison.OrdinalIgnoreCase);
|
||||||
|
});
|
||||||
|
|
||||||
|
return toolEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void InvokeTool(ToolEntry toolEntry)
|
||||||
|
{
|
||||||
|
if (toolEntry.MethodInfo == null || !toolEntry.MethodInfo.IsStatic)
|
||||||
|
{
|
||||||
|
Debug.LogError("Tool method is not static or could not be found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toolEntry.MethodInfo.GetParameters().Length != 0)
|
||||||
|
{
|
||||||
|
Debug.LogError($"Tool method '{toolEntry.MethodInfo.Name}' must be parameterless.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
toolEntry.MethodInfo.Invoke(null, null);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Debug.LogException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Texture2D GetIcon(string iconName)
|
||||||
|
{
|
||||||
|
return EditorGUIUtility.IconContent(iconName).image as Texture2D;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 219127dd6a8ebe143996214d98d2b475
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
161
Editor/ToolBarExtension/New/LocalizationToolbarDropdown.cs
Normal file
161
Editor/ToolBarExtension/New/LocalizationToolbarDropdown.cs
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
#if UNITY_6000_3_OR_NEWER
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using AlicizaX.Localization.Editor;
|
||||||
|
using AlicizaX.Localization.Runtime;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEditor.SceneManagement;
|
||||||
|
using UnityEditor.Toolbars;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace AlicizaX.Editor.Extension
|
||||||
|
{
|
||||||
|
[InitializeOnLoad]
|
||||||
|
public static class LocalizationToolbarDropdown
|
||||||
|
{
|
||||||
|
private const string ElementPath = "AlicizaX/Localization";
|
||||||
|
private const string Tooltip = "Switch editor localization preview language";
|
||||||
|
|
||||||
|
private static readonly Texture2D SettingsIcon;
|
||||||
|
private static string s_LastKnownLanguage;
|
||||||
|
|
||||||
|
static LocalizationToolbarDropdown()
|
||||||
|
{
|
||||||
|
SettingsIcon = GetIcon("Settings");
|
||||||
|
s_LastKnownLanguage = GetSelectedLanguage();
|
||||||
|
EditorApplication.update += RefreshWhenLanguageChanges;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MainToolbarElement(ElementPath, defaultDockPosition = MainToolbarDockPosition.Right, defaultDockIndex = 0)]
|
||||||
|
public static MainToolbarElement CreateElement()
|
||||||
|
{
|
||||||
|
return new MainToolbarDropdown(
|
||||||
|
new MainToolbarContent(GetToolbarLabel(), SettingsIcon, Tooltip),
|
||||||
|
ShowDropdownMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RefreshWhenLanguageChanges()
|
||||||
|
{
|
||||||
|
var selectedLanguage = GetSelectedLanguage();
|
||||||
|
if (selectedLanguage == s_LastKnownLanguage)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
s_LastKnownLanguage = selectedLanguage;
|
||||||
|
MainToolbar.Refresh(ElementPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ShowDropdownMenu(Rect dropdownRect)
|
||||||
|
{
|
||||||
|
var selectedLanguage = GetSelectedLanguage();
|
||||||
|
var menu = new GenericMenu();
|
||||||
|
var languageNames = GetLanguageTypeNames();
|
||||||
|
|
||||||
|
if (languageNames.Count == 0)
|
||||||
|
{
|
||||||
|
menu.AddDisabledItem(new GUIContent("No language options"));
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var languageName in languageNames)
|
||||||
|
{
|
||||||
|
var capturedLanguageName = languageName;
|
||||||
|
menu.AddItem(
|
||||||
|
new GUIContent(capturedLanguageName),
|
||||||
|
string.Equals(selectedLanguage, capturedLanguageName, StringComparison.Ordinal),
|
||||||
|
() => SetSelectedLanguage(capturedLanguageName));
|
||||||
|
}
|
||||||
|
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SetSelectedLanguage(string languageName)
|
||||||
|
{
|
||||||
|
EditorPrefs.SetString(LocalizationComponent.PrefsKey, languageName);
|
||||||
|
s_LastKnownLanguage = languageName;
|
||||||
|
InvokeOnValidateInScene();
|
||||||
|
MainToolbar.Refresh(ElementPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IReadOnlyList<string> GetLanguageTypeNames()
|
||||||
|
{
|
||||||
|
return LocalizationConfiguration.Instance != null
|
||||||
|
? LocalizationConfiguration.Instance.LanguageTypeNames
|
||||||
|
: Array.Empty<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetSelectedLanguage()
|
||||||
|
{
|
||||||
|
return EditorPrefs.GetString(LocalizationComponent.PrefsKey, "None");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetToolbarLabel()
|
||||||
|
{
|
||||||
|
return GetSelectedLanguage();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void InvokeOnValidateInScene()
|
||||||
|
{
|
||||||
|
var targetType = FindType("UnityEngine.UI.UXTextMeshPro");
|
||||||
|
if (targetType == null)
|
||||||
|
{
|
||||||
|
Debug.LogWarning("Could not find type UnityEngine.UI.UXTextMeshPro.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var onValidateMethod = targetType.GetMethod(
|
||||||
|
"OnValidate",
|
||||||
|
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||||
|
|
||||||
|
if (onValidateMethod == null)
|
||||||
|
{
|
||||||
|
Debug.LogWarning($"Could not find OnValidate on {targetType.Name}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var components = GameObject.FindObjectsOfType(targetType)
|
||||||
|
.OfType<Component>()
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (components.Count == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sceneToMarkDirty = EditorSceneManager.GetActiveScene();
|
||||||
|
|
||||||
|
foreach (var component in components)
|
||||||
|
{
|
||||||
|
Undo.RecordObject(component, "Invoke OnValidate");
|
||||||
|
onValidateMethod.Invoke(component, null);
|
||||||
|
EditorUtility.SetDirty(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorSceneManager.MarkSceneDirty(sceneToMarkDirty);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Type FindType(string fullName)
|
||||||
|
{
|
||||||
|
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||||
|
{
|
||||||
|
var type = assembly.GetType(fullName);
|
||||||
|
if (type != null)
|
||||||
|
{
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Texture2D GetIcon(string iconName)
|
||||||
|
{
|
||||||
|
return EditorGUIUtility.IconContent(iconName).image as Texture2D;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 13ca60a407539344ba0c7fed723066cf
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
90
Editor/ToolBarExtension/New/ResourceModeToolbarDropdown.cs
Normal file
90
Editor/ToolBarExtension/New/ResourceModeToolbarDropdown.cs
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#if UNITY_6000_3_OR_NEWER
|
||||||
|
using AlicizaX.Resource.Runtime;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEditor.Toolbars;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace AlicizaX.Editor.Extension
|
||||||
|
{
|
||||||
|
[InitializeOnLoad]
|
||||||
|
public static class ResourceModeToolbarDropdown
|
||||||
|
{
|
||||||
|
private const string ElementPath = "AlicizaX/ResourceMode";
|
||||||
|
private const string Tooltip = "配置 App 运行时资源模式";
|
||||||
|
|
||||||
|
private static readonly string[] ResourceModeNames =
|
||||||
|
{
|
||||||
|
"Editor",
|
||||||
|
"Offline",
|
||||||
|
"Host",
|
||||||
|
"Webgl"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static readonly Texture2D SettingsIcon;
|
||||||
|
private static int s_LastKnownModeIndex;
|
||||||
|
|
||||||
|
static ResourceModeToolbarDropdown()
|
||||||
|
{
|
||||||
|
SettingsIcon = EditorGUIUtility.IconContent("Settings").image as Texture2D;
|
||||||
|
s_LastKnownModeIndex = GetSelectedModeIndex();
|
||||||
|
EditorApplication.update += RefreshWhenModeChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MainToolbarElement(ElementPath, defaultDockPosition = MainToolbarDockPosition.Middle, defaultDockIndex = 101)]
|
||||||
|
public static MainToolbarElement CreateElement()
|
||||||
|
{
|
||||||
|
return new MainToolbarDropdown(
|
||||||
|
new MainToolbarContent(GetToolbarLabel(), SettingsIcon, Tooltip),
|
||||||
|
ShowDropdownMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RefreshWhenModeChanged()
|
||||||
|
{
|
||||||
|
var selectedModeIndex = GetSelectedModeIndex();
|
||||||
|
if (selectedModeIndex == s_LastKnownModeIndex)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
s_LastKnownModeIndex = selectedModeIndex;
|
||||||
|
MainToolbar.Refresh(ElementPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ShowDropdownMenu(Rect dropdownRect)
|
||||||
|
{
|
||||||
|
var currentIndex = GetSelectedModeIndex();
|
||||||
|
var menu = new GenericMenu();
|
||||||
|
|
||||||
|
for (var i = 0; i < ResourceModeNames.Length; i++)
|
||||||
|
{
|
||||||
|
var modeIndex = i;
|
||||||
|
menu.AddItem(
|
||||||
|
new GUIContent(ResourceModeNames[modeIndex]),
|
||||||
|
currentIndex == modeIndex,
|
||||||
|
() => SetSelectedMode(modeIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SetSelectedMode(int modeIndex)
|
||||||
|
{
|
||||||
|
var safeModeIndex = Mathf.Clamp(modeIndex, 0, ResourceModeNames.Length - 1);
|
||||||
|
EditorPrefs.SetInt(ResourceComponent.PrefsKey, safeModeIndex);
|
||||||
|
s_LastKnownModeIndex = safeModeIndex;
|
||||||
|
MainToolbar.Refresh(ElementPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetSelectedModeIndex()
|
||||||
|
{
|
||||||
|
var storedIndex = EditorPrefs.GetInt(ResourceComponent.PrefsKey, 0);
|
||||||
|
return Mathf.Clamp(storedIndex, 0, ResourceModeNames.Length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetToolbarLabel()
|
||||||
|
{
|
||||||
|
return $"Res:{ResourceModeNames[GetSelectedModeIndex()]}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ec58d6908c969b848a6fcb2637e7f0a5
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
183
Editor/ToolBarExtension/New/SwitchSceneToolbarDropdown.cs
Normal file
183
Editor/ToolBarExtension/New/SwitchSceneToolbarDropdown.cs
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
#if UNITY_6000_3_OR_NEWER
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEditor.SceneManagement;
|
||||||
|
using UnityEditor.Toolbars;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace AlicizaX.Editor.Extension
|
||||||
|
{
|
||||||
|
[InitializeOnLoad]
|
||||||
|
public static class SwitchSceneToolbarDropdown
|
||||||
|
{
|
||||||
|
private const string ElementPath = "AlicizaX/SwitchScene";
|
||||||
|
private const string Tooltip = "Switch the active scene";
|
||||||
|
|
||||||
|
private static readonly string[] SearchRoots =
|
||||||
|
{
|
||||||
|
"Assets/Bundles/",
|
||||||
|
"Assets/Scenes/"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static readonly Texture2D UnityLogoIcon;
|
||||||
|
|
||||||
|
private struct SceneEntry
|
||||||
|
{
|
||||||
|
public string ScenePath;
|
||||||
|
public string DisplayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
static SwitchSceneToolbarDropdown()
|
||||||
|
{
|
||||||
|
UnityLogoIcon = GetIcon("UnityLogo");
|
||||||
|
EditorSceneManager.sceneOpened += OnSceneOpened;
|
||||||
|
EditorSceneManager.activeSceneChangedInEditMode += OnActiveSceneChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MainToolbarElement(ElementPath, defaultDockPosition = MainToolbarDockPosition.Left, defaultDockIndex = 1)]
|
||||||
|
public static MainToolbarElement CreateElement()
|
||||||
|
{
|
||||||
|
return new MainToolbarDropdown(
|
||||||
|
new MainToolbarContent(GetToolbarLabel(), UnityLogoIcon, Tooltip),
|
||||||
|
ShowDropdownMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void OnSceneOpened(UnityEngine.SceneManagement.Scene scene, OpenSceneMode mode)
|
||||||
|
{
|
||||||
|
MainToolbar.Refresh(ElementPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void OnActiveSceneChanged(UnityEngine.SceneManagement.Scene previousScene, UnityEngine.SceneManagement.Scene newScene)
|
||||||
|
{
|
||||||
|
MainToolbar.Refresh(ElementPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ShowDropdownMenu(Rect dropdownRect)
|
||||||
|
{
|
||||||
|
var menu = new GenericMenu
|
||||||
|
{
|
||||||
|
allowDuplicateNames = true
|
||||||
|
};
|
||||||
|
|
||||||
|
var scenes = CollectScenes();
|
||||||
|
var activeScenePath = EditorSceneManager.GetActiveScene().path;
|
||||||
|
|
||||||
|
if (scenes.Count == 0)
|
||||||
|
{
|
||||||
|
menu.AddDisabledItem(new GUIContent("No scenes found"));
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < scenes.Count; i++)
|
||||||
|
{
|
||||||
|
var sceneEntry = scenes[i];
|
||||||
|
menu.AddItem(
|
||||||
|
new GUIContent(sceneEntry.DisplayName),
|
||||||
|
string.Equals(sceneEntry.ScenePath, activeScenePath, StringComparison.OrdinalIgnoreCase),
|
||||||
|
() => SwitchScene(sceneEntry.ScenePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
menu.DropDown(dropdownRect);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<SceneEntry> CollectScenes()
|
||||||
|
{
|
||||||
|
var sceneEntries = new List<SceneEntry>();
|
||||||
|
var sceneGuids = AssetDatabase.FindAssets("t:Scene", SearchRoots);
|
||||||
|
|
||||||
|
foreach (var sceneGuid in sceneGuids)
|
||||||
|
{
|
||||||
|
var scenePath = AssetDatabase.GUIDToAssetPath(sceneGuid);
|
||||||
|
sceneEntries.Add(new SceneEntry
|
||||||
|
{
|
||||||
|
ScenePath = scenePath,
|
||||||
|
DisplayName = BuildDisplayName(scenePath)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
sceneEntries.Sort((left, right) => string.Compare(left.DisplayName, right.DisplayName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
return sceneEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string BuildDisplayName(string scenePath)
|
||||||
|
{
|
||||||
|
var sceneName = Path.GetFileNameWithoutExtension(scenePath);
|
||||||
|
var sceneDirectory = NormalizePath(Path.GetDirectoryName(scenePath));
|
||||||
|
|
||||||
|
foreach (var searchRoot in SearchRoots)
|
||||||
|
{
|
||||||
|
var normalizedRoot = NormalizePath(searchRoot).TrimEnd('/');
|
||||||
|
if (!sceneDirectory.StartsWith(normalizedRoot, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.Equals(sceneDirectory, normalizedRoot, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return sceneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
var relativeDirectory = Path.GetRelativePath(normalizedRoot, sceneDirectory)
|
||||||
|
.Replace('\\', '/')
|
||||||
|
.Trim('/');
|
||||||
|
|
||||||
|
return string.IsNullOrEmpty(relativeDirectory)
|
||||||
|
? sceneName
|
||||||
|
: $"{relativeDirectory}/{sceneName}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return sceneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string NormalizePath(string path)
|
||||||
|
{
|
||||||
|
return string.IsNullOrEmpty(path)
|
||||||
|
? string.Empty
|
||||||
|
: path.Replace('\\', '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SwitchScene(string scenePath)
|
||||||
|
{
|
||||||
|
var currentScene = EditorSceneManager.GetActiveScene();
|
||||||
|
if (currentScene.IsValid() && currentScene.isDirty)
|
||||||
|
{
|
||||||
|
var optionIndex = EditorUtility.DisplayDialogComplex(
|
||||||
|
"Warning",
|
||||||
|
$"Current scene '{currentScene.name}' has unsaved changes. Save before switching?",
|
||||||
|
"Save",
|
||||||
|
"Cancel",
|
||||||
|
"Don't Save");
|
||||||
|
|
||||||
|
switch (optionIndex)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
if (!EditorSceneManager.SaveOpenScenes())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetToolbarLabel()
|
||||||
|
{
|
||||||
|
var activeSceneName = EditorSceneManager.GetActiveScene().name;
|
||||||
|
return string.IsNullOrEmpty(activeSceneName) ? "Switch Scene" : activeSceneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Texture2D GetIcon(string iconName)
|
||||||
|
{
|
||||||
|
return EditorGUIUtility.IconContent(iconName).image as Texture2D;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c78e30693afdf094c84a1f7097ba4a3a
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
8
Editor/ToolBarExtension/Old.meta
Normal file
8
Editor/ToolBarExtension/Old.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9fb5e37d60c0d9544b4ca58a3e2c7b52
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
#if !UNITY_6000_3_OR_NEWER
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,7 +10,7 @@ using UnityEngine.UIElements;
|
|||||||
|
|
||||||
namespace AlicizaX.Editor.Extension
|
namespace AlicizaX.Editor.Extension
|
||||||
{
|
{
|
||||||
[Paps.UnityToolbarExtenderUIToolkit.MainToolbarElement("EditorQuickToolBar", alignment: Paps.UnityToolbarExtenderUIToolkit.ToolbarAlign.Right, order: 1)]
|
[MainToolbarElement("EditorQuickToolBar", alignment: ToolbarAlign.Right, order: 1)]
|
||||||
public class EditorQuickToolBar : IMGUIContainer
|
public class EditorQuickToolBar : IMGUIContainer
|
||||||
{
|
{
|
||||||
private GUIContent toolsDropBtContent;
|
private GUIContent toolsDropBtContent;
|
||||||
@ -60,3 +61,5 @@ namespace AlicizaX.Editor.Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
#if !UNITY_6000_3_OR_NEWER
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -7,25 +8,6 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace AlicizaX.Editor.Extension
|
namespace AlicizaX.Editor.Extension
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
|
|
||||||
public class EditorToolFunctionAttribute : Attribute
|
|
||||||
{
|
|
||||||
public string ToolMenuPath { get; private set; }
|
|
||||||
public int MenuOrder { get; private set; }
|
|
||||||
public MethodInfo MethodInfo { get; private set; }
|
|
||||||
|
|
||||||
public EditorToolFunctionAttribute(string menu, int menuOrder = 0)
|
|
||||||
{
|
|
||||||
this.ToolMenuPath = menu;
|
|
||||||
MenuOrder = menuOrder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetMethodInfo(MethodInfo methodInfo)
|
|
||||||
{
|
|
||||||
MethodInfo = methodInfo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static class EditorToolFunctionAttributeCollector
|
internal static class EditorToolFunctionAttributeCollector
|
||||||
{
|
{
|
||||||
public static List<EditorToolFunctionAttribute> Attributes = new List<EditorToolFunctionAttribute>();
|
public static List<EditorToolFunctionAttribute> Attributes = new List<EditorToolFunctionAttribute>();
|
||||||
@ -89,3 +71,5 @@ namespace AlicizaX.Editor.Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
#if !UNITY_6000_3_OR_NEWER
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -11,7 +12,7 @@ using UnityEditor.SceneManagement;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
[Paps.UnityToolbarExtenderUIToolkit.MainToolbarElement("LocalizationDropdownField", alignment: Paps.UnityToolbarExtenderUIToolkit.ToolbarAlign.Right, order: 0)]
|
[MainToolbarElement("LocalizationDropdownField", alignment: ToolbarAlign.Right, order: 0)]
|
||||||
public class LocalizationDropdownField : IMGUIContainer
|
public class LocalizationDropdownField : IMGUIContainer
|
||||||
{
|
{
|
||||||
private GUIContent appConfigBtContent;
|
private GUIContent appConfigBtContent;
|
||||||
@ -71,8 +72,7 @@ public class LocalizationDropdownField : IMGUIContainer
|
|||||||
|
|
||||||
public static void InvokeOnValidateInScene()
|
public static void InvokeOnValidateInScene()
|
||||||
{
|
{
|
||||||
|
Type targetType = Utility.Assembly.GetType("UnityEngine.UI.UXTextMeshPro");
|
||||||
System.Type targetType = AlicizaX.Utility.Assembly.GetType("UnityEngine.UI.UXTextMeshPro");
|
|
||||||
MethodInfo onValidateMethod = targetType.GetMethod("OnValidate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
|
MethodInfo onValidateMethod = targetType.GetMethod("OnValidate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
|
||||||
|
|
||||||
if (onValidateMethod == null)
|
if (onValidateMethod == null)
|
||||||
@ -109,3 +109,5 @@ public class LocalizationDropdownField : IMGUIContainer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
#if !UNITY_6000_3_OR_NEWER
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using AlicizaX.Resource.Runtime;
|
using AlicizaX.Resource.Runtime;
|
||||||
using Paps.UnityToolbarExtenderUIToolkit;
|
using Paps.UnityToolbarExtenderUIToolkit;
|
||||||
@ -9,7 +10,7 @@ using UnityEngine.UIElements;
|
|||||||
|
|
||||||
namespace AlicizaX.Editor.Extension
|
namespace AlicizaX.Editor.Extension
|
||||||
{
|
{
|
||||||
[Paps.UnityToolbarExtenderUIToolkit.MainToolbarElement("ResourceModeDropdownField", alignment: Paps.UnityToolbarExtenderUIToolkit.ToolbarAlign.Right, order: 0)]
|
[MainToolbarElement("ResourceModeDropdownField", alignment: ToolbarAlign.Right, order: 0)]
|
||||||
public class ResourceModeDropdownField : IMGUIContainer
|
public class ResourceModeDropdownField : IMGUIContainer
|
||||||
{
|
{
|
||||||
private GUIContent appConfigBtContent;
|
private GUIContent appConfigBtContent;
|
||||||
@ -62,3 +63,5 @@ namespace AlicizaX.Editor.Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,4 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
#if !UNITY_6000_3_OR_NEWER
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using AlicizaX;
|
||||||
using Paps.UnityToolbarExtenderUIToolkit;
|
using Paps.UnityToolbarExtenderUIToolkit;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.SceneManagement;
|
using UnityEditor.SceneManagement;
|
||||||
@ -6,7 +9,7 @@ using UnityEngine;
|
|||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
[Paps.UnityToolbarExtenderUIToolkit.MainToolbarElement(id: "SwitchSceneToolBar", alignment: Paps.UnityToolbarExtenderUIToolkit.ToolbarAlign.Left, order: 1)]
|
[MainToolbarElement(id: "SwitchSceneToolBar", alignment: ToolbarAlign.Left, order: 1)]
|
||||||
public class SwitchSceneToolBar : IMGUIContainer
|
public class SwitchSceneToolBar : IMGUIContainer
|
||||||
{
|
{
|
||||||
private GUIContent switchSceneBtContent;
|
private GUIContent switchSceneBtContent;
|
||||||
@ -54,14 +57,14 @@ public class SwitchSceneToolBar : IMGUIContainer
|
|||||||
{
|
{
|
||||||
var scenePath = AssetDatabase.GUIDToAssetPath(sceneGuids[i]);
|
var scenePath = AssetDatabase.GUIDToAssetPath(sceneGuids[i]);
|
||||||
sceneAssetList.Add(scenePath);
|
sceneAssetList.Add(scenePath);
|
||||||
string fileDir = System.IO.Path.GetDirectoryName(scenePath);
|
string fileDir = Path.GetDirectoryName(scenePath);
|
||||||
bool isInRootDir = AlicizaX.Utility.Path.GetRegularPath(BundleScenePath).TrimEnd('/') ==
|
bool isInRootDir = Utility.Path.GetRegularPath(BundleScenePath).TrimEnd('/') ==
|
||||||
AlicizaX.Utility.Path.GetRegularPath(fileDir).TrimEnd('/');
|
Utility.Path.GetRegularPath(fileDir).TrimEnd('/');
|
||||||
var sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
|
var sceneName = Path.GetFileNameWithoutExtension(scenePath);
|
||||||
string displayName = sceneName;
|
string displayName = sceneName;
|
||||||
if (!isInRootDir)
|
if (!isInRootDir)
|
||||||
{
|
{
|
||||||
var sceneDir = System.IO.Path.GetRelativePath(RootScenePath, fileDir);
|
var sceneDir = Path.GetRelativePath(RootScenePath, fileDir);
|
||||||
displayName = $"{sceneDir}/{sceneName}";
|
displayName = $"{sceneDir}/{sceneName}";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,3 +102,5 @@ public class SwitchSceneToolBar : IMGUIContainer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user