How to hide Gizmos by script

Hey there!

In the scene view there’s a drop down menu where you can enable/disable Gizmos of a specific type (for example PolygonCollider2D).
How can I access these Gizmo settings by an EditorScript?

I have an Editor-Script with two buttons to enable/disable my custom SceneView-Editor:

    private void StartEditing()
    {
        Tools.current = Tool.None;
        editing = true;

        SceneView sv = EditorWindow.GetWindow<SceneView>();
        sv.maximized = true;

        //DISABLE GIZMOS
        //sv.???

        SceneView.RepaintAll();
    }


    private void StopEditing()
    {
        Tools.current = Tool.Move;
        editing = false;

        SceneView sv = EditorWindow.GetWindow<SceneView>();
        sv.maximized = false;

        //ENABLE GIZMOS AGAIN
        //sv.???

        SceneView.RepaintAll();
    }

Is there any possibility?

Just to be able to check my question as answered: Here’s my solution, which worked.

using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
using System.Collections;

public class CustomEditorUtilities
{
    public static void ToggleGizmos(bool gizmosOn)
    {
        int val = gizmosOn ? 1 : 0;
        Assembly asm = Assembly.GetAssembly(typeof(Editor));
        Type type = asm.GetType("UnityEditor.AnnotationUtility");
        if (type != null)
        {
            MethodInfo getAnnotations = type.GetMethod("GetAnnotations", BindingFlags.Static | BindingFlags.NonPublic);
            MethodInfo setGizmoEnabled = type.GetMethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic);
            MethodInfo setIconEnabled = type.GetMethod("SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic);
            var annotations = getAnnotations.Invoke(null, null);
            foreach (object annotation in (IEnumerable)annotations)
            {
                Type annotationType = annotation.GetType();
                FieldInfo classIdField = annotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance);
                FieldInfo scriptClassField = annotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance);
                if (classIdField != null && scriptClassField != null)
                {
                    int classId = (int)classIdField.GetValue(annotation);
                    string scriptClass = (string)scriptClassField.GetValue(annotation);
                    setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, val });
                    setIconEnabled.Invoke(null, new object[] { classId, scriptClass, val });
                }
            }
        }
    }
}

Source of the script

A big thank you to meat5000 showing me the link :wink:

If you want to toggle only a specific MonoBehaviour scene view icon, here’s a lil snippet!

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 } );
}

If the goal is to toggle ON or OFF all gizmos, these lines should suffice:

// to toggle OFF gizmos:
SceneView.lastActiveSceneView.drawGizmos = false;

// to toggle ON gizmos:
SceneView.lastActiveSceneView.drawGizmos = true;

It is equivalent to toggling the “Gizmos” button in the upper-right corner of the scene view window.

More information about the SceneView API can be found here: Unity - Scripting API: SceneView

If you need an out of the box solution I’ve released an asset some time ago called VoltGizmos, which has a scripting api where you can hide and show gizmos for given script types, and much more.


You can check it out HERE