public void SetHandler(InspectableProperty property, InspectableHandler handler)
        {
            int key = GetPropertyHash(property);

            InspectableHandlers[key] = handler;
        }
예제 #2
0
        public static InspectableHandler GetHandler(InspectableProperty property)
        {
            if (property == null)
            {
                return(s_SharedNullHandler);
            }

            // Don't use custom drawers in debug mode
            if (property.InspectableObject.SerializedObject.InspectorMode() != InspectorMode.Normal)
            {
                return(s_SharedNullHandler);
            }

            // If the drawer is cached, use the cached drawer
            InspectableHandler handler = InspectableHandlerCache.GetHandler(property);

            if (handler != null)
            {
                return(handler);
            }

            Type propertyType = null;
            List <PropertyAttribute> attributes = null;
            FieldInfo field = null;

            // Determine if SerializedObject target is a script or a builtin type
            UnityEngine.Object targetObject = property.InspectableObject.SerializedObject.targetObject;
            if (targetObject is MonoBehaviour || targetObject is ScriptableObject)
            {
                // For scripts, use reflection to get FieldInfo for the member the property represents
                field = GetFieldInfoFromProperty(property, out propertyType);

                // Use reflection to see if this member has an attribute
                attributes = GetFieldAttributes(field);
            }
            else
            {
                // For builtin types, look if we hardcoded an attribute for this property
                // First initialize the hardcoded properties if not already done
                if (s_BuiltinAttributes == null)
                {
                    PopulateBuiltinAttributes();
                }

                if (attributes == null)
                {
                    attributes = GetBuiltinAttributes(property);
                }
            }

            handler = s_NextHandler;

            if (attributes != null)
            {
                for (int i = attributes.Count - 1; i >= 0; i--)
                {
                    handler.HandleAttribute(attributes[i], field, propertyType);
                }
            }

            // Field has no CustomPropertyDrawer attribute with matching drawer so look for default drawer for field type
            if (!handler.HasInspectableDrawer && propertyType != null)
            {
                handler.HandleDrawnType(propertyType, propertyType, field, null);
            }

            if (handler.Empty)
            {
                InspectableHandlerCache.SetHandler(property, s_SharedNullHandler);
                handler = s_SharedNullHandler;
            }
            else
            {
                InspectableHandlerCache.SetHandler(property, handler);
                s_NextHandler = new InspectableHandler();
            }

            return(handler);
        }