mjk6026
mjk6026

Reputation: 1484

Unity editor script: visible / hidden gizmos

I want to hide or show a gizmo in programmatically.

enter image description here

Is this possible?

Upvotes: 2

Views: 3274

Answers (2)

Robertk92
Robertk92

Reputation: 1

You also just call:

UnityEditorInternal.InternalEditorUtility.SetShowGizmos(bool show)

Upvotes: 0

Programmer
Programmer

Reputation: 125445

Can it be done with built it API from Unity? No.

Although, this can be done with the help of reflection since Unity did not expose the API for that functionality. I did not write this code. It's from here.

You can call the ToggleGizmos(true) or ToggleGizmos(false) function anytime or you can use it as a plugin like below. Any new Menu called Quick Fingers should be added to Unity.

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

public class SceneViewGizmos {
    private static bool _globalGizmosOn;


    [MenuItem("Quick Fingers/Scene View/Toggle Gizmos &%g")] private static void ToggleAllSceneGizmos() {
        _globalGizmosOn = !_globalGizmosOn;
        ToggleGizmos(_globalGizmosOn);
    }

    [MenuItem("Quick Fingers/Scene View/Disable All Gizmos")] private static void DisableAllSceneGizmos() {
        _globalGizmosOn = false;
        ToggleGizmos(_globalGizmosOn);
    }

    [MenuItem("Quick Fingers/Scene View/Enable All Gizmos")] private static void EnableAllSceneGizmos() {
        _globalGizmosOn = true;
        ToggleGizmos(_globalGizmosOn);
    }

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

Upvotes: 3

Related Questions