Unity编辑器脚本:可见/隐藏小控件 [英] Unity editor script: visible / hidden gizmos

查看:630
本文介绍了Unity编辑器脚本:可见/隐藏小控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式隐藏或显示小控件.

I want to hide or show a gizmo in programmatically.

这可能吗?

推荐答案

可以使用Unity内置的API完成此操作吗? .

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

尽管,可以反射的帮助下完成,因为Unity并未公开该功能的API.我没有写这段代码.它来自此处.

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.

您可以随时调用ToggleGizmos(true)ToggleGizmos(false)函数,也可以将其用作如下所示的插件.任何称为 Quick Fingers 的新 Menu 都应添加到Unity.

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

这篇关于Unity编辑器脚本:可见/隐藏小控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆