我想以编程方式隐藏或显示 Gizmo。
这可能吗?
可以通过 Unity 构建的 API 来完成吗?没有。
虽然,这可以在反射的帮助下完成,因为 Unity 没有公开该功能的 API。这段代码不是我写的。它来自这里。
您可以随时调用ToggleGizmos(true)
orToggleGizmos(false)
函数,也可以将其用作插件,如下所示。任何名为Quick Fingers的新菜单都应添加到 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 });
}
}
}
}
}
您也只需调用:
UnityEditorInternal.InternalEditorUtility.SetShowGizmos(bool show)