Last active
April 23, 2023 21:02
-
-
Save soraphis/a0101d3c9591cc4a8dfcff96ba11b9dc to your computer and use it in GitHub Desktop.
Pendant for the https://github.com/marijnz/unity-toolbar-extender just for the bottom status bar. Add
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
using System; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
namespace UnityAppStatusBarExtender | |
{ | |
public class AppStatusBarCallback | |
{ | |
public static class Styles | |
{ | |
public static readonly GUIStyle background = "AppToolbar"; | |
public static readonly GUIStyle statusIcon = "StatusBarIcon"; | |
} | |
static Type m_statusbarType = typeof(Editor).Assembly.GetType("UnityEditor.AppStatusBar"); | |
static Type m_consoleWindowType = typeof(Editor).Assembly.GetType("UnityEditor.ConsoleWindow"); | |
static Type m_ViewType = typeof(Editor).Assembly.GetType("UnityEditor.View"); | |
#if UNITY_2023_1_OR_NEWER | |
static Type m_ShortcutHelperWindowType = | |
typeof(ShortcutManager).Assembly.GetType("UnityEditor.ShortcutManagement.HelperWindow"); | |
#endif | |
// sadly the AppStatusBar still uses IMGUI... so there is no easy way to inject stuff after some VisualElement | |
// that's why we need to re-create the "OldOnGUI()" method https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/AppStatusBar.cs | |
// while adding our InjectionPoints. | |
// We can do so using Detours: (https://tryfinally.dev/detours-redirecting-csharp-methods-at-runtime) | |
// needs to be put in an asmdef that allows unsafe code, though. | |
// so ... get all the used members: | |
private static MethodInfo m_OldOnGUIMethodInfo = null; | |
private static MethodInfo m_ConsoleWindowLoadIconsMethodInfo = null; | |
private static MethodInfo m_ProgressGetMaxElapsedTimeMethodInfo = null; | |
private static FieldInfo m_ShowProgresFieldInfo = null; | |
private static PropertyInfo m_PositionPropertyInfo = null; | |
private static MethodInfo m_DrawStatusTextMethodInfo = null; | |
private static MethodInfo m_StatusBarShortcutsMethodInfo = null; | |
private static MethodInfo m_DrawSpecialModeLabelMethodInfo = null; | |
private static MethodInfo m_DrawProgressBarMethodInfo = null; | |
private static MethodInfo m_DrawDebuggerToggleMethodInfo = null; | |
private static FieldInfo m_DrawExtraFeaturesFieldInfo = null; | |
private static MethodInfo m_DrawCacheServerToggleMethodInfo = null; | |
private static MethodInfo m_DrawBakeModeMethodInfo = null; | |
private static MethodInfo m_DrawRefreshStatusMethodInfo = null; | |
private static MethodInfo m_ShowRepaintsMethodInfo = null; | |
const float k_ShowProgressThreshold = 0.5f; | |
public static Action OnBeforeProgress; | |
public static Action OnAfterProgress; | |
public static Action OnAfterButtons; | |
static AppStatusBarCallback() | |
{ | |
// TODO: all static variables | |
var bfIPP = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; | |
var bfSPP = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; | |
m_OldOnGUIMethodInfo = m_statusbarType.GetMethod("OldOnGUI", bfIPP); | |
m_ConsoleWindowLoadIconsMethodInfo = m_consoleWindowType.GetMethod("LoadIcons", bfSPP); | |
m_ProgressGetMaxElapsedTimeMethodInfo = typeof(Progress).GetMethod("GetMaxElapsedTimeMethodInfo", bfSPP); | |
m_ShowProgresFieldInfo = m_statusbarType.GetField("m_ShowProgress", bfIPP); | |
m_PositionPropertyInfo = m_ViewType.GetProperty("position", bfIPP); | |
m_DrawStatusTextMethodInfo = m_statusbarType.GetMethod("DrawStatusText", bfIPP); | |
#if UNITY_2023_1_OR_NEWER | |
m_StatusBarShortcutsMethodInfo = m_ShortcutHelperWindowType.GetMethod("StatusBarShortcuts", bfSPP); | |
#endif | |
m_DrawSpecialModeLabelMethodInfo = m_statusbarType.GetMethod("DrawSpecialModeLabel", bfIPP); | |
m_DrawProgressBarMethodInfo = m_statusbarType.GetMethod("DrawProgressBar", bfIPP); | |
m_DrawDebuggerToggleMethodInfo = m_statusbarType.GetMethod("DrawDebuggerToggle", bfIPP); | |
m_DrawExtraFeaturesFieldInfo = m_statusbarType.GetField("m_DrawExtraFeatures", bfIPP); | |
m_DrawCacheServerToggleMethodInfo = m_statusbarType.GetMethod("DrawCacheServerToggle", bfIPP); | |
m_DrawBakeModeMethodInfo = m_statusbarType.GetMethod("DrawBakeMode", bfIPP); | |
m_DrawRefreshStatusMethodInfo = m_statusbarType.GetMethod("DrawRefreshStatus", bfIPP); | |
m_ShowRepaintsMethodInfo = typeof(EditorGUI).GetMethod("ShowRepaints", bfSPP); | |
//EditorApplication.update -= OnUpdate; | |
//EditorApplication.update += OnUpdate; | |
var replacementMethod = | |
DetourUtility.MethodInfoForMethodCall(() => OldOnGUIReplacement(null)); | |
// DetourUtility.TryDetourFromTo(m_OldOnGUIMethodInfo, replacementMethod); | |
} | |
// when the editor recompiles, this method throws quite a few NullReferenceExceptions | |
static void OldOnGUIReplacement(object appStatusBarSelf) | |
{ | |
m_ConsoleWindowLoadIconsMethodInfo.Invoke(null, Array.Empty<object>()); | |
// GUI.color = EditorApplication.isPlayingOrWillChangePlaymode ? HostView.kPlayModeDarken : Color.white; | |
var position = (Rect) m_PositionPropertyInfo.GetValue(appStatusBarSelf, Array.Empty<object>()); | |
if (Event.current.type == EventType.Layout) | |
{ | |
var showProgress = Progress.running | |
&& ((float) m_ProgressGetMaxElapsedTimeMethodInfo.Invoke(null, | |
Array.Empty<object>())) > k_ShowProgressThreshold; | |
m_ShowProgresFieldInfo.SetValue(appStatusBarSelf, showProgress); | |
} | |
else if (Event.current.type == EventType.Repaint) | |
{ | |
Styles.background.Draw(new Rect(0, 0, position.width, position.height), false, false, false, false); | |
} | |
GUILayout.BeginHorizontal(GUILayout.MaxWidth(position.width)); | |
{ | |
GUILayout.Space(2); | |
m_DrawStatusTextMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
#if UNITY_2023_1_OR_NEWER | |
if (EditorPrefs.GetBool("EnableHelperBar", false)) | |
{ | |
m_StatusBarShortcutsMethodInfo.Invoke(null, Array.Empty<object>()); | |
} | |
else | |
#endif | |
GUILayout.FlexibleSpace(); | |
SafeInvoke(OnBeforeProgress); | |
var drawExtraFeatures = (bool) m_DrawExtraFeaturesFieldInfo.GetValue(appStatusBarSelf); | |
if (drawExtraFeatures) | |
m_DrawSpecialModeLabelMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
m_DrawProgressBarMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
SafeInvoke(OnAfterProgress); | |
m_DrawDebuggerToggleMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
if (drawExtraFeatures) | |
{ | |
m_DrawCacheServerToggleMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
m_DrawBakeModeMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
} | |
m_DrawRefreshStatusMethodInfo.Invoke(appStatusBarSelf, Array.Empty<object>()); | |
SafeInvoke(OnAfterButtons); | |
} | |
GUILayout.EndHorizontal(); | |
m_ShowRepaintsMethodInfo.Invoke(null, Array.Empty<object>()); | |
} | |
private static void SafeInvoke(Action arg) | |
{ | |
try | |
{ | |
arg?.Invoke(); | |
} | |
catch(Exception e) | |
{ | |
if(GUILayout.Button(EditorGUIUtility.IconContent("console.warnicon"), AppStatusBarCallback.Styles.statusIcon)) | |
{ | |
Debug.LogError(e); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage example: | |
using System; | |
using System.Reflection; | |
using UnityAppStatusBarExtender; | |
using UnityEditor; | |
using UnityEngine; | |
[InitializeOnLoad] | |
public static class MyStatusBarExtensions | |
{ | |
static MyStatusBarExtensions() | |
{ | |
AppStatusBarCallback.OnAfterButtons += OnAfterProgress; | |
} | |
private static void OnAfterProgress() | |
{ | |
var content = EditorGUIUtility.TrIconContent("d_Exposure"); | |
using (new EditorGUIUtility.IconSizeScope(new Vector2(16, 16))) | |
{ | |
if(GUILayout.Button(content, AppStatusBarCallback.Styles.statusIcon)) | |
{ | |
Debug.Log("Hey"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment