1.8.0: Added target frame rate and PlayerPrefs.Delete to toolbar

- Added: Tooltips on all buttons and labels in toolbar tools
- Added: Target frame rate with `[min;max]` slider in toolbar
- Added: `PlayerPrefs` delete button added to toolbar
- Added: Option to select Single/Additive scene load/unload to scene picker
- Added: All `IToolbarSettings` will be grouped with a foldout-toggle in Preferences
This commit is contained in:
Anders Ejlersen 2022-12-07 22:10:09 +01:00
parent 10d07b986f
commit fe82c5c9fa
22 changed files with 669 additions and 48 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.SceneManagement;
@ -17,31 +18,48 @@ namespace Module.NavigationTool.Editor.Toolbar
public override EToolbarPlacement Placement => EToolbarPlacement.Right;
public override int Priority => (int)EToolbarPriority.Medium;
private static bool IS_DIRTY = true;
private static int SELECTED_INDEX = -1;
private static string[] OPTIONS = new string[0];
private static string[] PATHS = new string[0];
private static readonly List<int> SELECTED_INDICES = new List<int>();
private static readonly StringBuilder STRING_BUILDER = new StringBuilder();
private static string SCENE_LABEL = string.Empty;
private static string[] SCENE_NAMES = new string[0];
private static string[] SCENE_SHORT_NAMES = new string[0];
private static string[] SCENE_PATHS = new string[0];
private static readonly GUIContent LABEL_ADDITIVE = new GUIContent(string.Empty, "Toggles between Single and Additive scene loading");
private static bool IS_DIRTY = true;
private static void Initialize()
{
if (!IS_DIRTY)
return;
SELECTED_INDICES.Clear();
var listNames = new List<string>();
var listShortNames = new List<string>();
var listPaths = new List<string>();
InitializeBuildSettingsScenes(listNames, listPaths);
InitializeRemainingScenes(listNames, listPaths);
OPTIONS = listNames.ToArray();
PATHS = listPaths.ToArray();
Scene activeScene = SceneManager.GetActiveScene();
SELECTED_INDEX = listPaths.IndexOf(activeScene.path);
InitializeBuildSettingsScenes(listNames, listShortNames, listPaths);
InitializeRemainingScenes(listNames, listShortNames, listPaths);
for (var i = 0; i < SceneManager.sceneCount; i++)
{
Scene scene = SceneManager.GetSceneAt(i);
int index = listPaths.IndexOf(scene.path);
if (index != -1)
SELECTED_INDICES.Add(listPaths.IndexOf(scene.path));
}
SCENE_NAMES = listNames.ToArray();
SCENE_SHORT_NAMES = listShortNames.ToArray();
SCENE_PATHS = listPaths.ToArray();
RefreshSceneLabel();
IS_DIRTY = false;
}
private static void InitializeBuildSettingsScenes(List<string> listNames, List<string> listPaths)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void InitializeBuildSettingsScenes(List<string> listNames, List<string> listShortNames, List<string> listPaths)
{
EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
@ -50,12 +68,14 @@ namespace Module.NavigationTool.Editor.Toolbar
if (string.IsNullOrEmpty(scenes[i].path))
continue;
listNames.Add($"{Path.GetFileNameWithoutExtension(scenes[i].path)}");
listNames.Add(Path.GetFileNameWithoutExtension(scenes[i].path));
listShortNames.Add(listNames[i]);
listPaths.Add(scenes[i].path);
}
}
private static void InitializeRemainingScenes(List<string> listNames, List<string> listPaths)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void InitializeRemainingScenes(List<string> listNames, List<string> listShortNames, List<string> listPaths)
{
List<string> sortByAssetLabels = ToolbarScenePickerSettings.SceneAssetLabels;
var assetLabelToScenes = new SceneSortElement[sortByAssetLabels.Count + 1];
@ -93,6 +113,7 @@ namespace Module.NavigationTool.Editor.Toolbar
assetLabelToScenes[index] = new SceneSortElement();
assetLabelToScenes[index].names.Add(sceneName);
assetLabelToScenes[index].shortNames.Add(Path.GetFileName(sceneName));
assetLabelToScenes[index].paths.Add(path);
}
@ -104,13 +125,15 @@ namespace Module.NavigationTool.Editor.Toolbar
continue;
listNames.Add(string.Empty);
listShortNames.Add(string.Empty);
listPaths.Add(string.Empty);
listNames.AddRange(e.names);
listShortNames.AddRange(e.shortNames);
listPaths.AddRange(e.paths);
}
}
public override void Update()
{
Initialize();
@ -119,29 +142,81 @@ namespace Module.NavigationTool.Editor.Toolbar
protected override void Draw(Rect rect)
{
Initialize();
int temp = EditorGUI.Popup(rect, SELECTED_INDEX, OPTIONS, styles.popup);
var rect0 = new Rect(rect.x, rect.y, rect.width - 24.0f, rect.height);
var rect1 = new Rect(rect0.xMax, rect.y, 24.0f, rect.height);
if (temp <= -1 || temp == SELECTED_INDEX || OPTIONS.Length == 0)
return;
if (string.IsNullOrEmpty(PATHS[temp]))
return;
if (GUI.Button(rect0, SCENE_LABEL, styles.popup))
ShowDropDown(rect0);
try
bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive;
bool temp = EditorGUI.Toggle(rect1, isScenePickerSetAsAdditive, styles.button);
GUI.Label(rect1, temp ? styles.iconPlusSmall : styles.iconPlusTiny, styles.labelCenter);
GUI.Label(rect1, LABEL_ADDITIVE, styles.labelCenter);
if (temp != isScenePickerSetAsAdditive)
ToolbarScenePickerSettings.IsScenePickerSetAsAdditive = temp;
}
private static void ShowDropDown(Rect rect)
{
var menu = new GenericMenu();
for (var i = 0; i < SCENE_NAMES.Length; i++)
{
Scene scene = EditorSceneManager.OpenScene(PATHS[temp], OpenSceneMode.Single);
if (scene.isLoaded)
SELECTED_INDEX = temp;
int sceneIndex = i;
menu.AddItem(new GUIContent(SCENE_NAMES[i]), SELECTED_INDICES.Contains(i), () => SelectScene(sceneIndex));
}
catch (Exception e)
menu.DropDown(rect);
}
private static void SelectScene(int index)
{
string path = SCENE_PATHS[index];
Scene scene = SceneManager.GetSceneByPath(path);
bool isScenePickerSetAsAdditive = ToolbarScenePickerSettings.IsScenePickerSetAsAdditive;
if (scene.isLoaded)
{
Debug.LogWarningFormat("Failed to load scene ({0}), due to exception: {1}", PATHS[temp], e.Message);
if (SceneManager.sceneCount == 1)
return;
EditorSceneManager.CloseScene(scene, true);
SELECTED_INDICES.Remove(index);
}
else if (isScenePickerSetAsAdditive)
{
EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Additive);
SELECTED_INDICES.Add(index);
}
else
{
EditorSceneManager.OpenScene(SCENE_PATHS[index], OpenSceneMode.Single);
SELECTED_INDICES.Clear();
SELECTED_INDICES.Add(index);
}
RefreshSceneLabel();
}
private static void RefreshSceneLabel()
{
STRING_BUILDER.Clear();
for (var i = 0; i < SELECTED_INDICES.Count; i++)
{
if (i > 0)
STRING_BUILDER.Append(", ");
STRING_BUILDER.Append(SCENE_SHORT_NAMES[SELECTED_INDICES[i]]);
}
SCENE_LABEL = STRING_BUILDER.ToString();
}
public override float CalculateWidth()
{
return 100.0f;
return 124.0f;
}
public static void SetAsDirty()
@ -152,6 +227,7 @@ namespace Module.NavigationTool.Editor.Toolbar
private sealed class SceneSortElement
{
public readonly List<string> names = new List<string>();
public readonly List<string> shortNames = new List<string>();
public readonly List<string> paths = new List<string>();
}
}