Unity editor script: visible / hidden gizmos

*爱你&永不变心* 提交于 2019-12-04 05:13:56

问题


I want to hide or show a gizmo in programmatically.

Is this possible?


回答1:


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



回答2:


You also just call:

UnityEditorInternal.InternalEditorUtility.SetShowGizmos(bool show)


来源:https://stackoverflow.com/questions/37267021/unity-editor-script-visible-hidden-gizmos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!