public static void SetGizmoIconEnabled(Type script, bool enabled)
        {
#if UNITY_2019_1_OR_NEWER
            var annotations = AnnotationUtility.GetAnnotations();
            var annotation  = annotations.FirstOrDefault(x => x.scriptClass.Contains(script.Name));
            AnnotationUtility.SetIconEnabled(annotation.classID, annotation.scriptClass, enabled ? 1 : 0);
#else
            Type annotationUtility = typeof(Editor).Assembly.GetType("UnityEditor.AnnotationUtility");

            //Case 1294866 : Seems that getting the annotation array remove the warning
            //Might be initializing something that is missing otherwise
            MethodInfo getAnnotations = annotationUtility.GetMethod("GetAnnotations",
                                                                    BindingFlags.Static | BindingFlags.NonPublic,
                                                                    null,
                                                                    new Type[] { },
                                                                    null);
            var annotations = getAnnotations.Invoke(null, new object[] {});

            MethodInfo setGizmoIconEnabled = annotationUtility.GetMethod("SetIconEnabled",
                                                                         BindingFlags.Static | BindingFlags.NonPublic,
                                                                         null,
                                                                         new Type[] { typeof(int), typeof(string), typeof(int) },
                                                                         null);
            var name = script.Name;
            setGizmoIconEnabled.Invoke(null, new object[] { 114, name, enabled ? 1 : 0 });
#endif
        }
        internal bool IsGridAnnotationEnabled()
        {
            var annotations = AnnotationUtility.GetAnnotations();

            foreach (var annotation in annotations)
            {
                if (annotation.classID == m_GridType.persistentTypeID)
                {
                    return(annotation.gizmoEnabled > 0);
                }
            }
            return(false);
        }
        public static void SetGizmoIconEnabled(Type script, bool enabled)
        {
#if UNITY_2019_1_OR_NEWER
            var annotations = AnnotationUtility.GetAnnotations();
            var annotation  = annotations.FirstOrDefault(x => x.scriptClass.Contains(script.Name));
            AnnotationUtility.SetIconEnabled(annotation.classID, annotation.scriptClass, enabled ? 1 : 0);
#else
            Type       annotationUtility   = typeof(Editor).Assembly.GetType("UnityEditor.AnnotationUtility");
            MethodInfo setGizmoIconEnabled = annotationUtility.GetMethod("SetIconEnabled",
                                                                         BindingFlags.Static | BindingFlags.NonPublic,
                                                                         null,
                                                                         new Type[] { typeof(int), typeof(string), typeof(int) },
                                                                         null);
            var name = script.Name;
            setGizmoIconEnabled.Invoke(null, new object[] { 114, name, enabled ? 1 : 0 });
#endif
        }
Example #4
0
        // 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;
                }
            }
        }
 /// <summary>
 /// 获取CustomAttribute Value
 /// </summary>
 /// <typeparam name="T">Attribute的子类型</typeparam>
 /// <param name="sourceType">头部标有CustomAttribute类的类型</param>
 /// <param name="attributeValueAction">取Attribute具体哪个属性值的匿名函数</param>
 /// <param name="name">field name或property name</param>
 /// <returns>返回Attribute的值,没有则返回null</returns>
 public static string GetCustomAttributeValue <T>(this Type sourceType,
                                                  Func <T, string> attributeValueAction,
                                                  string name) where T : Attribute
 {
     return(AnnotationUtility.GetAttributeValue(sourceType, attributeValueAction, name));
 }