Navigation tools upgraded to use the new MainToolbarElement API
This commit is contained in:
parent
708b99f763
commit
3e1602162c
52 changed files with 837 additions and 66 deletions
103
Editor/Toolbar/MainToolbarElements/MainToolbarBuildElement.cs
Normal file
103
Editor/Toolbar/MainToolbarElements/MainToolbarBuildElement.cs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#if UNITY_6000_3_OR_NEWER
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Toolbars;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Scripting;
|
||||
|
||||
namespace Module.NavigationTool.Editor.Toolbar
|
||||
{
|
||||
internal static class MainToolbarBuildElement
|
||||
{
|
||||
[Preserve]
|
||||
[MainToolbarElement("Toolbar/Build", ussName = "", defaultDockIndex = 0, defaultDockPosition = MainToolbarDockPosition.Middle, menuPriority = 500)]
|
||||
public static IEnumerable<MainToolbarElement> Draw()
|
||||
{
|
||||
var target = EditorUserBuildSettings.activeBuildTarget;
|
||||
var group = BuildPipeline.GetBuildTargetGroup(target);
|
||||
|
||||
return new List<MainToolbarElement>(2)
|
||||
{
|
||||
new MainToolbarDropdown(new MainToolbarContent("Build", "Select build or build & run options"), OnBuildDropdownOpened),
|
||||
new MainToolbarDropdown(GetButtonContent(group), OnBuildTargetDropdownOpened)
|
||||
};
|
||||
}
|
||||
|
||||
private static void OnBuildDropdownOpened(Rect rect)
|
||||
{
|
||||
var menu = new GenericMenu();
|
||||
menu.AddItem(new GUIContent("Build", "Build project with current EditorBuildSettings and target platform"), false, () => Build(false));
|
||||
menu.AddItem(new GUIContent("Build & Run", "Build & Run project with current EditorBuildSettings and target platform"), false, () => Build(false));
|
||||
menu.DropDown(rect);
|
||||
}
|
||||
|
||||
private static void OnBuildTargetDropdownOpened(Rect rect)
|
||||
{
|
||||
var menu = new GenericMenu();
|
||||
var type = typeof(BuildTarget);
|
||||
var values = Enum.GetValues(type);
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var target = (BuildTarget)values.GetValue(i);
|
||||
var group = BuildPipeline.GetBuildTargetGroup(target);
|
||||
|
||||
if (target < 0 || !BuildPipeline.IsBuildTargetSupported(group, target))
|
||||
continue;
|
||||
if (!IsValid(type, target))
|
||||
continue;
|
||||
|
||||
menu.AddItem(new GUIContent(ObjectNames.NicifyVariableName(target.ToString())), false, () => SetBuildTargetTo(group, target));
|
||||
}
|
||||
|
||||
menu.DropDown(rect);
|
||||
}
|
||||
|
||||
private static void SetBuildTargetTo(BuildTargetGroup group, BuildTarget target)
|
||||
{
|
||||
if (EditorUserBuildSettings.SwitchActiveBuildTarget(group, target))
|
||||
MainToolbar.Refresh("Toolbar/Build Targets");
|
||||
}
|
||||
|
||||
private static bool IsValid(Type type, BuildTarget target)
|
||||
{
|
||||
var 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";
|
||||
|
||||
var iconContent = EditorGUIUtility.IconContent(name);
|
||||
var content = new GUIContent(iconContent.image, "Select target platform for build");
|
||||
return content;
|
||||
}
|
||||
|
||||
private static void Build(bool withAutorun)
|
||||
{
|
||||
var options = BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(new BuildPlayerOptions());
|
||||
|
||||
if (withAutorun)
|
||||
options.options |= BuildOptions.AutoRunPlayer;
|
||||
|
||||
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue