public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.ObjectReference)
            {
                this.DrawAsMismatchedAttribute(position, property);
                return;
            }

            bool allowSceneObject = (this.attribute is SelectableObjectAttribute) ? (this.attribute as SelectableObjectAttribute).AllowSceneObjects : true;

            _compPropDrawer.AllowSceneObject = allowSceneObject;
            _compPropDrawer.OnGUI(position, property, label);
        }
Beispiel #2
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();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.ObjectReference)
            {
                this.DrawAsMismatchedAttribute(position, property);
                return;
            }

            this.InitOnGUI();
            _compPropDrawer.AllowSceneObjects = _allowSceneObjects;
            _compPropDrawer.RestrictionType   = _inheritsFromType;
            _compPropDrawer.AllowProxy        = _allowProxy;
            _compPropDrawer.OnGUI(position, property, label);
        }
        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();
        }