Unity editor script: visible / hidden gizmos

后端 未结 2 1241
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 05:15

I want to hide or show a gizmo in programmatically.

Is this possible?

相关标签:
2条回答
  • 2021-01-24 05:25

    You also just call:

    UnityEditorInternal.InternalEditorUtility.SetShowGizmos(bool show)
    
    0 讨论(0)
  • 2021-01-24 05:42

    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 });
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题