1.4.0: Added build target picker and build button to toolbar
This commit is contained in:
parent
0b147a9144
commit
03200cc370
10 changed files with 254 additions and 7 deletions
77
Editor/Toolbar/Tools/ToolBuild.cs
Normal file
77
Editor/Toolbar/Tools/ToolBuild.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
using JetBrains.Annotations;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UTools = UnityEditor.Tools;
|
||||
|
||||
namespace Module.NavigationTool.Editor.Toolbar
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class ToolBuild : AbstractToolbarDrawer
|
||||
{
|
||||
public override bool Visible => ToolbarSettings.IsBuildEnabled;
|
||||
public override bool Enabled => !Application.isPlaying;
|
||||
public override EToolbarPlacement Placement => EToolbarPlacement.Left;
|
||||
public override int Priority => (int)EToolbarPriority.Low;
|
||||
|
||||
private static bool IS_DIRTY = true;
|
||||
private static GUIContent[] BUTTON_LIST = new GUIContent[0];
|
||||
private static GUIContent[] CONTENT_LIST = new GUIContent[0];
|
||||
private static GUIContent DROPDOWN = new GUIContent();
|
||||
|
||||
private static void Initialize()
|
||||
{
|
||||
if (!IS_DIRTY)
|
||||
return;
|
||||
|
||||
BUTTON_LIST = new[]
|
||||
{
|
||||
new GUIContent("Build"),
|
||||
new GUIContent("Build & Run")
|
||||
};
|
||||
|
||||
CONTENT_LIST = new[]
|
||||
{
|
||||
new GUIContent("Build"),
|
||||
new GUIContent("Build and Run")
|
||||
};
|
||||
|
||||
DROPDOWN = EditorGUIUtility.IconContent("d_icon dropdown");
|
||||
|
||||
IS_DIRTY = false;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
protected override void Draw(Rect rect)
|
||||
{
|
||||
var rect0 = new Rect(rect.x, rect.y, rect.width - 16.0f, rect.height);
|
||||
var rect1 = new Rect(rect0.xMax, rect.y, 16.0f, rect.height);
|
||||
|
||||
int currentSelected = ToolbarSettings.IsBuildAndRunEnabled ? 1 : 0;
|
||||
|
||||
if (GUI.Button(rect0, BUTTON_LIST[currentSelected], styles.buttonNoPadding))
|
||||
{
|
||||
BuildPlayerOptions options = BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(new BuildPlayerOptions());
|
||||
options.options |= BuildOptions.AutoRunPlayer;
|
||||
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options);
|
||||
}
|
||||
|
||||
if (GUI.Button(rect1, DROPDOWN, styles.button))
|
||||
{
|
||||
EditorUtility.DisplayCustomMenu(rect, CONTENT_LIST, currentSelected, (_, _, selected) =>
|
||||
{
|
||||
if (selected != -1)
|
||||
ToolbarSettings.IsBuildAndRunEnabled = selected == 1;
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
|
||||
public override float CalculateWidth()
|
||||
{
|
||||
return 100.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Editor/Toolbar/Tools/ToolBuild.cs.meta
Normal file
3
Editor/Toolbar/Tools/ToolBuild.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f8e26a1e2caa4d49bb92f6887ac243dd
|
||||
timeCreated: 1644777135
|
||||
143
Editor/Toolbar/Tools/ToolBuildTargetPicker.cs
Normal file
143
Editor/Toolbar/Tools/ToolBuildTargetPicker.cs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UTools = UnityEditor.Tools;
|
||||
|
||||
namespace Module.NavigationTool.Editor.Toolbar
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class ToolBuildTargetPicker : AbstractToolbarDrawer
|
||||
{
|
||||
public override bool Visible => ToolbarSettings.IsBuildEnabled;
|
||||
public override bool Enabled => !Application.isPlaying;
|
||||
public override EToolbarPlacement Placement => EToolbarPlacement.Left;
|
||||
public override int Priority => (int)EToolbarPriority.Low;
|
||||
|
||||
private static bool IS_DIRTY = true;
|
||||
private static int[] HASHES = new int[0];
|
||||
private static BuildTargetGroup[] GROUPS = new BuildTargetGroup[0];
|
||||
private static BuildTarget[] TARGETS = new BuildTarget[0];
|
||||
private static GUIContent[] TARGET_LIST = new GUIContent[0];
|
||||
private static int SELECTED_INDEX = -1;
|
||||
|
||||
private static void Initialize()
|
||||
{
|
||||
if (!IS_DIRTY)
|
||||
return;
|
||||
|
||||
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
|
||||
BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);
|
||||
|
||||
PopulateBuildTargetList();
|
||||
SELECTED_INDEX = Array.IndexOf(HASHES, GetHashCode(group, target));
|
||||
IS_DIRTY = false;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
protected override void Draw(Rect rect)
|
||||
{
|
||||
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
|
||||
BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);
|
||||
GUIContent content = SELECTED_INDEX != -1 ? GetButtonContent(group) : GUIContent.none;
|
||||
|
||||
if (GUI.Button(rect, content, styles.buttonNoPadding))
|
||||
{
|
||||
EditorUtility.DisplayCustomMenu(rect, TARGET_LIST, SELECTED_INDEX, (_, _, selected) =>
|
||||
{
|
||||
if (selected != -1 && selected != SELECTED_INDEX)
|
||||
SetBuildTargetTo(selected);
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
|
||||
public override float CalculateWidth()
|
||||
{
|
||||
return 40.0f;
|
||||
}
|
||||
|
||||
private static void SetBuildTargetTo(int index)
|
||||
{
|
||||
if (index < 0)
|
||||
return;
|
||||
|
||||
BuildTargetGroup group = GROUPS[index];
|
||||
BuildTarget target = TARGETS[index];
|
||||
|
||||
if (!EditorUserBuildSettings.SwitchActiveBuildTarget(@group, target))
|
||||
return;
|
||||
|
||||
IS_DIRTY = true;
|
||||
SELECTED_INDEX = index;
|
||||
}
|
||||
|
||||
private static void PopulateBuildTargetList()
|
||||
{
|
||||
var hashes = new List<int>();
|
||||
var groups = new List<BuildTargetGroup>();
|
||||
var targets = new List<BuildTarget>();
|
||||
var contentList = new List<GUIContent>();
|
||||
|
||||
Type type = typeof(BuildTarget);
|
||||
Array values = Enum.GetValues(type);
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var target = (BuildTarget)values.GetValue(i);
|
||||
BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target);
|
||||
|
||||
if (target < 0 || !BuildPipeline.IsBuildTargetSupported(group, target))
|
||||
continue;
|
||||
if (!IsValid(type, target))
|
||||
continue;
|
||||
|
||||
hashes.Add(GetHashCode(group, target));
|
||||
groups.Add(group);
|
||||
targets.Add(target);
|
||||
contentList.Add(new GUIContent(ObjectNames.NicifyVariableName(target.ToString())));
|
||||
}
|
||||
|
||||
HASHES = hashes.ToArray();
|
||||
GROUPS = groups.ToArray();
|
||||
TARGETS = targets.ToArray();
|
||||
TARGET_LIST = contentList.ToArray();
|
||||
}
|
||||
|
||||
private static bool IsValid(Type type, BuildTarget target)
|
||||
{
|
||||
MemberInfo[] members = type.GetMember(target.ToString(), BindingFlags.Public | BindingFlags.Static);
|
||||
|
||||
if (members.Length == 0)
|
||||
return false;
|
||||
|
||||
for (var j = 0; j < members.Length; j++)
|
||||
{
|
||||
if (members[j].GetCustomAttribute<ObsoleteAttribute>() == null)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static GUIContent GetButtonContent(BuildTargetGroup group)
|
||||
{
|
||||
var name = $"BuildSettings.{group}.Small";
|
||||
|
||||
if (group == BuildTargetGroup.WSA)
|
||||
name = "BuildSettings.Metro.Small";
|
||||
|
||||
return EditorGUIUtility.IconContent(name);
|
||||
}
|
||||
|
||||
private static int GetHashCode(BuildTargetGroup group, BuildTarget target)
|
||||
{
|
||||
return ((int)group << 16) + (int)target;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Editor/Toolbar/Tools/ToolBuildTargetPicker.cs.meta
Normal file
3
Editor/Toolbar/Tools/ToolBuildTargetPicker.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 966d40900b7547b6997863f3fbbf2cd5
|
||||
timeCreated: 1644752271
|
||||
|
|
@ -80,7 +80,7 @@ namespace Module.NavigationTool.Editor.Toolbar
|
|||
|
||||
public override float CalculateWidth()
|
||||
{
|
||||
return 100.0f;
|
||||
return 72.0f;
|
||||
}
|
||||
|
||||
private static void Focus(int instanceId)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue