Unity编辑器脚本:可见/隐藏的小玩意儿

时间:2016-05-17 04:15:33

标签: c# unity3d unity3d-editor

我想以编程方式隐藏或显示Gizmo。

enter image description here

这可能吗?

2 个答案:

答案 0 :(得分:1)

可以使用Unity构建的API来完成吗? 即可。

虽然可以反射的帮助下完成,因为Unity没有公开该功能的API。我没有写这段代码。它来自here

您可以随时调用ToggleGizmos(true)ToggleGizmos(false)功能,也可以将其用作下面的插件。任何名为快速手指的新菜单都应添加到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 });
                }
            }
        }
    }
}

答案 1 :(得分:0)

您也可以致电:

UnityEditorInternal.InternalEditorUtility.SetShowGizmos(bool show)