The DrawGizmo attribute allows you to supply a gizmo renderer for any Component.

Inheritance: System.Attribute
示例#1
0
        private static AttributeHelper.MonoGizmoMethod[] ExtractGizmos(Assembly assembly)
        {
            List <AttributeHelper.MonoGizmoMethod> list = new List <AttributeHelper.MonoGizmoMethod>();

            Type[] typesFromAssembly = AssemblyHelper.GetTypesFromAssembly(assembly);
            Type[] array             = typesFromAssembly;
            for (int i = 0; i < array.Length; i++)
            {
                Type         type    = array[i];
                MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                for (int j = 0; j < methods.GetLength(0); j++)
                {
                    MethodInfo methodInfo       = methods[j];
                    object[]   customAttributes = methodInfo.GetCustomAttributes(typeof(DrawGizmo), false);
                    object[]   array2           = customAttributes;
                    for (int k = 0; k < array2.Length; k++)
                    {
                        DrawGizmo       drawGizmo  = (DrawGizmo)array2[k];
                        ParameterInfo[] parameters = methodInfo.GetParameters();
                        if (parameters.Length != 2)
                        {
                            UnityEngine.Debug.LogWarning(string.Format("Method {0}.{1} is marked with the DrawGizmo attribute but does not take parameters (ComponentType, GizmoType) so will be ignored.", methodInfo.DeclaringType.FullName, methodInfo.Name));
                        }
                        else if (methodInfo.DeclaringType != null && methodInfo.DeclaringType.IsGenericTypeDefinition)
                        {
                            UnityEngine.Debug.LogWarning(string.Format("Method {0}.{1} is marked with the DrawGizmo attribute but is defined on a generic type definition, so will be ignored.", methodInfo.DeclaringType.FullName, methodInfo.Name));
                        }
                        else
                        {
                            AttributeHelper.MonoGizmoMethod item = default(AttributeHelper.MonoGizmoMethod);
                            if (drawGizmo.drawnType == null)
                            {
                                item.drawnType = parameters[0].ParameterType;
                            }
                            else
                            {
                                if (!parameters[0].ParameterType.IsAssignableFrom(drawGizmo.drawnType))
                                {
                                    UnityEngine.Debug.LogWarning(string.Format("Method {0}.{1} is marked with the DrawGizmo attribute but the component type it applies to could not be determined.", methodInfo.DeclaringType.FullName, methodInfo.Name));
                                    goto IL_1DD;
                                }
                                item.drawnType = drawGizmo.drawnType;
                            }
                            if (parameters[1].ParameterType != typeof(GizmoType) && parameters[1].ParameterType != typeof(int))
                            {
                                UnityEngine.Debug.LogWarning(string.Format("Method {0}.{1} is marked with the DrawGizmo attribute but does not take a second parameter of type GizmoType so will be ignored.", methodInfo.DeclaringType.FullName, methodInfo.Name));
                            }
                            else
                            {
                                item.drawGizmo = methodInfo;
                                item.options   = (int)drawGizmo.drawOptions;
                                list.Add(item);
                            }
                        }
                        IL_1DD :;
                    }
                }
            }
            return(list.ToArray());
        }