Beispiel #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!this.ValidateFieldType())
            {
                EditorGUI.PropertyField(position, property, label);
                return;
            }

            EditorGUI.BeginProperty(position, label, property);

            //get base type
            var attrib           = this.attribute as ComponentTypeRestrictionAttribute;
            var inheritsFromType = attrib.InheritsFromType ?? typeof(Component);

            bool isArray   = this.fieldInfo.FieldType.IsListType();
            var  fieldType = (isArray) ? this.fieldInfo.FieldType.GetElementTypeOfListType() : this.fieldInfo.FieldType;

            if (attrib.HideTypeDropDown)
            {
                //draw object field
                var fieldObjType = (inheritsFromType.IsInterface) ? typeof(Component) : inheritsFromType;
                var comp         = SPEditorGUI.ComponentField(position, label, property.objectReferenceValue as Component, fieldObjType, true, fieldType);
                if (comp == null)
                {
                    property.objectReferenceValue = null;
                }
                else if (TypeUtil.IsType(comp.GetType(), inheritsFromType))
                {
                    property.objectReferenceValue = comp;
                }
                else
                {
                    property.objectReferenceValue = comp.GetComponent(inheritsFromType);
                }
            }
            else
            {
                //draw complex field
                if (_selectComponentDrawer == null)
                {
                    _selectComponentDrawer = new SelectableComponentPropertyDrawer();
                }

                _selectComponentDrawer.RestrictionType = inheritsFromType;
                _selectComponentDrawer.ShowXButton     = true;

                _selectComponentDrawer.OnGUI(position, property, label);
            }

            EditorGUI.EndProperty();
        }
        private void _targetList_DrawElement(Rect area, int index, bool isActive, bool isFocused)
        {
            var element = _targetList.serializedProperty.GetArrayElementAtIndex(index);

            var trigProp = element.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG);
            var actProp  = element.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_ACTIVATIONTYPE);
            //var act = (TriggerActivationType)actProp.enumValueIndex;
            var act = actProp.GetEnumValue <TriggerActivationType>();

            const float MARGIN = 1.0f;

            if (act == TriggerActivationType.TriggerAllOnTarget)
            {
                //Draw Triggerable - this is the simple case to make a clean designer set up for newbs
                var trigRect  = new Rect(area.xMin, area.yMin + MARGIN, area.width, EditorGUIUtility.singleLineHeight);
                var trigLabel = new GUIContent("Target");
                EditorGUI.BeginProperty(trigRect, trigLabel, trigProp);
                trigProp.objectReferenceValue = SPEditorGUI.ComponentField(trigRect,
                                                                           trigLabel,
                                                                           ValidateTriggerableTargAsMechanism(trigProp.objectReferenceValue) as Component,
                                                                           typeof(ITriggerableMechanism),
                                                                           true);
                EditorGUI.EndProperty();
            }
            else
            {
                //Draw Triggerable - this forces the user to use the advanced settings, not for newbs
                var trigRect  = new Rect(area.xMin, area.yMin + MARGIN, area.width, EditorGUIUtility.singleLineHeight);
                var trigLabel = new GUIContent("Advanced Target", "A target is not set, see advanced settings section to set a target.");

                if (trigProp.objectReferenceValue != null)
                {
                    var        obj      = trigProp.objectReferenceValue;
                    var        trigType = trigProp.objectReferenceValue.GetType();
                    GUIContent extraLabel;
                    switch (act)
                    {
                    case TriggerActivationType.SendMessage:
                        extraLabel = new GUIContent("(SendMessage) " + obj.name);
                        break;

                    case TriggerActivationType.TriggerSelectedTarget:
                        extraLabel = new GUIContent("(TriggerSelectedTarget) " + obj.name + " -> " + trigType.Name);
                        break;

                    case TriggerActivationType.CallMethodOnSelectedTarget:
                        extraLabel = new GUIContent("(CallMethodOnSelectedTarget) " + obj.name + " -> " + trigType.Name + "." + element.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_METHODNAME).stringValue);
                        break;

                    default:
                        extraLabel = GUIContent.none;
                        break;
                    }
                    EditorGUI.LabelField(trigRect, trigLabel, extraLabel);
                }
                else
                {
                    EditorGUI.LabelField(trigRect, trigLabel, new GUIContent("No Target"), new GUIStyle("Label")
                    {
                        alignment = TextAnchor.MiddleCenter
                    });
                }
            }

            ReorderableListHelper.DrawDraggableElementDeleteContextMenu(_targetList, area, index, isActive, isFocused);
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!this.ValidateFieldType())
            {
                EditorGUI.PropertyField(position, property, label);
                return;
            }

            EditorGUI.BeginProperty(position, label, property);

            //get base type
            var attrib = this.attribute as TypeRestrictionAttribute;

            bool isArray              = this.fieldInfo.FieldType.IsListType();
            var  fieldType            = (isArray) ? this.fieldInfo.FieldType.GetElementTypeOfListType() : this.fieldInfo.FieldType;
            bool fieldIsComponentType = TypeUtil.IsType(fieldType, typeof(Component));
            bool objIsComponentType   = property.objectReferenceValue is Component;
            var  inheritsFromType     = attrib.InheritsFromType ?? ((fieldIsComponentType) ? typeof(Component) : fieldType);

            if (attrib.HideTypeDropDown || !objIsComponentType)
            {
                //draw object field
                if (fieldIsComponentType)
                {
                    var fieldCompType = (TypeUtil.IsType(fieldType, typeof(Component))) ? fieldType : typeof(Component);
                    var comp          = SPEditorGUI.ComponentField(position, label, property.objectReferenceValue as Component, inheritsFromType, true, fieldCompType);
                    if (comp == null)
                    {
                        property.objectReferenceValue = null;
                    }
                    else
                    {
                        property.objectReferenceValue = ObjUtil.GetAsFromSource(inheritsFromType, comp) as UnityEngine.Object;
                    }
                    //else if (TypeUtil.IsType(comp.GetType(), inheritsFromType))
                    //    property.objectReferenceValue = comp;
                    //else
                    //    property.objectReferenceValue = comp.GetComponent(inheritsFromType);
                }
                else
                {
                    var obj = EditorGUI.ObjectField(position, label, property.objectReferenceValue, fieldType, true);
                    if (obj == null)
                    {
                        property.objectReferenceValue = null;
                    }
                    else
                    {
                        property.objectReferenceValue = ObjUtil.GetAsFromSource(inheritsFromType, obj) as UnityEngine.Object;
                    }
                }
            }
            else
            {
                //draw complex field
                if (_selectComponentDrawer == null)
                {
                    _selectComponentDrawer = new SelectableComponentPropertyDrawer();
                }

                _selectComponentDrawer.RestrictionType = inheritsFromType ?? typeof(Component);
                _selectComponentDrawer.ShowXButton     = true;

                _selectComponentDrawer.OnGUI(position, property, label);
            }

            EditorGUI.EndProperty();
        }