93 lines
3.2 KiB
C#
93 lines
3.2 KiB
C#
#if UNITY_6000_3_OR_NEWER
|
|
using System;
|
|
using System.Reflection;
|
|
using UnityEditor;
|
|
using UnityEditor.Toolbars;
|
|
using UnityEngine;
|
|
using UnityEngine.Scripting;
|
|
|
|
namespace Module.NavigationTool.Editor.Burst.Toolbar
|
|
{
|
|
internal static class MainToolbarBurstElement
|
|
{
|
|
[Preserve]
|
|
[MainToolbarElement("Toolbar/Burst", ussName = "", defaultDockIndex = 0, defaultDockPosition = MainToolbarDockPosition.Middle, menuPriority = 500)]
|
|
public static MainToolbarElement Draw()
|
|
{
|
|
Styles.Initialize();
|
|
|
|
var isBurstEnabled = IsBurstEnabled();
|
|
var icon = isBurstEnabled ? Styles.IconEnabled : Styles.IconDisabled;
|
|
var content = new MainToolbarContent(icon, "Enable/disable burst compiler");
|
|
return new MainToolbarToggle(content, IsBurstEnabled(), _ =>
|
|
{
|
|
EditorApplication.ExecuteMenuItem("Jobs/Burst/Enable Compilation");
|
|
MainToolbar.Refresh("Toolbar/Burst");
|
|
});
|
|
}
|
|
|
|
private static bool IsBurstEnabled()
|
|
{
|
|
const string typeFullname = "Unity.Burst.Editor.BurstEditorOptions, Unity.Burst";
|
|
const string propertyName = "EnableBurstCompilation";
|
|
|
|
var type = Type.GetType(typeFullname, false, true);
|
|
|
|
if (type == null)
|
|
{
|
|
Debug.LogWarningFormat("Failed to find type with name: {0}", typeFullname);
|
|
return false;
|
|
}
|
|
|
|
var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
|
|
|
|
if (property == null)
|
|
{
|
|
Debug.LogWarningFormat("Failed to find property with name: {0} on type: {1}", propertyName, typeFullname);
|
|
return false;
|
|
}
|
|
|
|
return (bool)property.GetValue(null);
|
|
}
|
|
|
|
private static class Styles
|
|
{
|
|
private static bool _isInitialized;
|
|
public static Texture2D IconEnabled;
|
|
public static Texture2D IconDisabled;
|
|
|
|
private const string EditorIconFileNameLight = "tex_icon_burst_light";
|
|
private const string EditorIconFileNameDark = "tex_icon_burst_dark";
|
|
|
|
public static void Initialize()
|
|
{
|
|
if (_isInitialized)
|
|
return;
|
|
|
|
IconEnabled = LoadIcon("_enabled");
|
|
IconDisabled = LoadIcon("_disabled");
|
|
_isInitialized = true;
|
|
}
|
|
|
|
private static Texture2D LoadIcon(string postfix)
|
|
{
|
|
var iconPrefix = EditorGUIUtility.isProSkin ? EditorIconFileNameDark : EditorIconFileNameLight;
|
|
var filename = $"{iconPrefix}{postfix}";
|
|
var guids = AssetDatabase.FindAssets("t:texture " + filename);
|
|
|
|
for (var i = 0; i < guids.Length; i++)
|
|
{
|
|
var path = AssetDatabase.GUIDToAssetPath(guids[i]);
|
|
var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
|
|
|
|
if (tex != null)
|
|
return tex;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|