Skip to content

Instantly share code, notes, and snippets.

@Manamongods
Last active January 21, 2022 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manamongods/c6a2797784b57f0b52e9276e9d319e91 to your computer and use it in GitHub Desktop.
Save Manamongods/c6a2797784b57f0b52e9276e9d319e91 to your computer and use it in GitHub Desktop.
(For 2022.1) In new Unity versions, collapsing a component doesn't hide its gizmos anymore, which makes it slower to temporarily hide a component's gizmos. This helps that out by adding options to the right click, the context menu, of a component
// Creative Commons 0
#define SINGLE_BUTTON // Comment this if you want individual buttons
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
internal static class GizmosContextMenu
{
#if SINGLE_BUTTON
// One button to enable/disable
[MenuItem("CONTEXT/Component/Toggle Gizmos")]
private static void ToggleComponentGizmos(MenuCommand menuCommand)
{
var type = menuCommand.context.GetType();
if (GizmoUtility.TryGetGizmoInfo(type, out GizmoInfo gi))
{
bool enable = !gi.gizmoEnabled;
GizmoUtility.SetGizmoEnabled(type, enable, true);
#if true
if (enable)
Debug.Log("Enabled Gizmos for: " + type.Name);
else
Debug.Log("Disabled Gizmos for: " + type.Name);
#endif
}
}
#else
// Individual buttons for enabling/disabling
[MenuItem("CONTEXT/Component/Disable Gizmos", true)]
private static bool ValidateDisableComponentGizmos(MenuCommand menuCommand)
{
return GizmoUtility.TryGetGizmoInfo(menuCommand.context.GetType(), out GizmoInfo gi) && gi.gizmoEnabled;
}
[MenuItem("CONTEXT/Component/Disable Gizmos")]
private static void DisableComponentGizmos(MenuCommand menuCommand)
{
GizmoUtility.SetGizmoEnabled(menuCommand.context.GetType(), false, true);
}
[MenuItem("CONTEXT/Component/Enable Gizmos", true)]
private static bool ValidateEnableComponentGizmos(MenuCommand menuCommand)
{
return GizmoUtility.TryGetGizmoInfo(menuCommand.context.GetType(), out GizmoInfo gi) && !gi.gizmoEnabled;
}
[MenuItem("CONTEXT/Component/Enable Gizmos")]
private static void EnableComponentGizmos(MenuCommand menuCommand)
{
GizmoUtility.SetGizmoEnabled(menuCommand.context.GetType(), true, true);
}
#endif
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment