static bool GetGizmo(int targetID, string className) { FieldInfo classIdField = AnnotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance); FieldInfo scriptClassField = AnnotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance); FieldInfo gizmoEnabledField = AnnotationType.GetField("gizmoEnabled", BindingFlags.Public | BindingFlags.Instance);; foreach (object annotation in (Array)Annotations) { int classId = (int)classIdField.GetValue(annotation); string scriptClass = (string)scriptClassField.GetValue(annotation); if (classId == targetID && (string.IsNullOrEmpty(className) || className.Equals(scriptClass))) { return((int)gizmoEnabledField.GetValue(annotation) == 1); } } return(false); }
// https://docs.unity3d.com/Manual/ClassIDReference.html static void ToggleGizmo(int targetClassID, string className, bool bValue) { MethodInfo setGizmoEnabled = AnnotationUtility.GetMethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic); FieldInfo classIdField = AnnotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance); FieldInfo scriptClassField = AnnotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance); foreach (object annotation in (Array)Annotations) { int classId = (int)classIdField.GetValue(annotation); string scriptClass = (string)scriptClassField.GetValue(annotation); if (classId == targetClassID && (string.IsNullOrEmpty(className) || className.Equals(scriptClass))) { setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, bValue ? 1 : 0 }); break; } } }
static AnnotationUtility() { try { AnnotationUtilityType = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.AnnotationUtility", true); AnnotationUtility_GetAnnotations = AnnotationUtilityType.GetMethod("GetAnnotations", BindingFlags.Static | BindingFlags.NonPublic); AnnotationUtility_SetGizmoEnabled = AnnotationUtilityType.GetMethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic); AnnotationUtility_SetIconEnabled = AnnotationUtilityType.GetMethod("SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic); if (AnnotationUtility_GetAnnotations == null) { throw new MissingMemberException(AnnotationUtilityType.FullName, "GetAnnotations"); } if (AnnotationUtility_SetGizmoEnabled == null) { throw new MissingMemberException(AnnotationUtilityType.FullName, "SetGizmoEnabled"); } if (AnnotationUtility_SetIconEnabled == null) { throw new MissingMemberException(AnnotationUtilityType.FullName, "SetIconEnabled"); } AnnotationType = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.Annotation", true); Annotation_classID = AnnotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance); Annotation_scriptClass = AnnotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance); Annotation_iconEnabled = AnnotationType.GetField("iconEnabled", BindingFlags.Public | BindingFlags.Instance); Annotation_gizmoEnabled = AnnotationType.GetField("gizmoEnabled", BindingFlags.Public | BindingFlags.Instance); if (Annotation_classID == null) { throw new MissingMemberException(AnnotationType.FullName, "classID"); } if (Annotation_scriptClass == null) { throw new MissingMemberException(AnnotationType.FullName, "scriptClass"); } if (Annotation_iconEnabled == null) { throw new MissingMemberException(AnnotationType.FullName, "iconEnabled"); } if (Annotation_gizmoEnabled == null) { throw new MissingMemberException(AnnotationType.FullName, "gizmoEnabled"); } unityAnnotations = new Dictionary <int, Annotation>(); scriptAnnotations = new Dictionary <string, Annotation>(); var rawAnnotations = (Array)AnnotationUtility_GetAnnotations.Invoke(null, null); foreach (var rawAnnotation in rawAnnotations) { var annotation = new Annotation(rawAnnotation); if (annotation.classID == EditorUnityObjectUtility.MonoBehaviourClassID) { // Can happen when multiple types have the same name if (!scriptAnnotations.ContainsKey(annotation.scriptClass)) { scriptAnnotations.Add(annotation.scriptClass, annotation); } } else { unityAnnotations.Add(annotation.classID, annotation); } } } catch (Exception ex) { throw new UnityEditorInternalException(ex); } }