Search code examples
c#unity-game-enginescene

Hide Custom Script Gizmo In Scene Unity


From Unity Manual:

Unity allows you to assign custom icons for GameObjects and scripts. These icons display in the Scene view, along with built-in icons for items such as Lights and Cameras. Use the Gizmos menu to control how icons are drawn in the Scene view.

This icon is also displayed in the Scene View As Shown. But if multiple objects have the script and are closer together the scene view looks like a mess, especially in the 2D gizmos view:

upload_2022-12-25_15-22-28.png

How can I remove the icon from the scene view and keep it in the inspector and project window?

I have tried using the GizmoUtility.SetGizmoEnabled function. But it didn't work!


Solution

  • I've been trying to solve this issue myself for some time, and It seems the only way to automate disabling the script icon gizmo is by disabling the icon gizmos of your specific MonoBehaviour through reflection.

    Here's where I found my solution. The last answer by Acegikmo gives a concise method that solves the issue perfectly. https://answers.unity.com/questions/851470/how-to-hide-gizmos-by-script.html

    static MethodInfo setIconEnabled;
    static MethodInfo SetIconEnabled => setIconEnabled = setIconEnabled ??
        Assembly.GetAssembly( typeof(Editor) )
        ?.GetType( "UnityEditor.AnnotationUtility" )
        ?.GetMethod( "SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic );
     
    public static void SetGizmoIconEnabled( Type type, bool on ) {
        if( SetIconEnabled == null ) return;
        const int MONO_BEHAVIOR_CLASS_ID = 114; // https://docs.unity3d.com/Manual/ClassIDReference.html
        SetIconEnabled.Invoke( null, new object[] { MONO_BEHAVIOR_CLASS_ID, type.Name, on ? 1 : 0 } );
    }
    

    You just need to call the method SetGizmoIconEnabled from anywhere for the Type of your MonoBehaviour and it should work.


    The alternative is to manually find the Gizmo dropdown menu in the Scene view and disable the script Gizmo from there:

    enter image description here