protected void DrawComponent(Rect position, SerializedProperty property, GUIContent label)
        {
            SpecificFieldAttribute v_attr = attribute as SpecificFieldAttribute;

            System.Type v_attrType = v_attr != null ? v_attr.AcceptedType : typeof(object);
            DrawComponent(position, property, label, fieldInfo != null && fieldInfo.FieldType != null ? fieldInfo.FieldType : v_attrType);
        }
Beispiel #2
0
        protected override void DrawComponent(Rect position, SerializedProperty property, GUIContent label, System.Type p_type)
        {
            Color v_oldColor      = GUI.backgroundColor;
            int   v_selectorWidth = _isArrayOfComponents ? 40 : 30;

            System.Type            v_type = p_type;
            SpecificFieldAttribute v_attr = attribute as SpecificFieldAttribute;

            if (v_attr != null && IsSameOrSubClassOrImplementInterface(v_type, v_attr.AcceptedType))
            {
                //Get Usefull Fields
                UnityEngine.Object v_oldValue         = property.objectReferenceValue;
                Component          v_selfAsComponent  = GetSelfAsComponent(property, p_type);
                List <Component>   v_components       = GetComponents(property, v_type, p_type);
                string[]           v_componentsString = GetStringArrayFromComponents(v_components);
                int v_currentIndex = GetCurrentComponentIndex(v_components, v_selfAsComponent);

                //Get Rects
                Rect v_objectRect = new Rect(position.x, position.y, Mathf.Max(0, position.width - v_selectorWidth), position.height);
                Rect v_indexRect  = new Rect(position.x + v_objectRect.width, position.y, position.width - v_objectRect.width, position.height);

                //Draw Object Field
                UnityEngine.Object v_newObject = EditorGUI.ObjectField(v_objectRect, label, property.objectReferenceValue, v_type, true);

                //Draw PopUp
                GUI.backgroundColor = v_currentIndex == -1 ? Color.red : Color.green;
                int v_newIndex = EditorGUI.Popup(v_indexRect, v_currentIndex, v_componentsString);
                GUI.backgroundColor = v_oldColor;

                //Apply
                TryApplyValue(property, v_oldValue, v_newObject, v_currentIndex, v_newIndex, v_components);
            }
        }
        protected virtual void CheckIfNeedRecalc(bool p_isArray)
        {
            SpecificFieldAttribute v_attr = attribute as SpecificFieldAttribute;

            if (v_attr != null)
            {
                System.Type v_acceptedType  = v_attr.AcceptedType;
                System.Type v_type          = this.fieldInfo != null ? this.fieldInfo.FieldType : v_attr.AcceptedType;
                System.Type v_convertedType = GetAcceptedFilterTypeFromContainerType(v_type);
                if (_needRecalc && v_type != null)
                {
                    _needRecalc          = false;
                    _isComponent         = IsSameOrSubClassOrImplementInterface(v_type, v_acceptedType);
                    _isArrayOfComponents = (v_type.IsArray || IsSameOrSubClassOrImplementInterface(v_type, typeof(IList))) && IsSameOrSubClassOrImplementInterface(v_convertedType, v_acceptedType);
                }
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            bool v_oldGuiEnabled          = GUI.enabled;
            SpecificFieldAttribute v_attr = attribute as SpecificFieldAttribute;

            if (v_attr != null && v_attr.ReadOnly)
            {
                GUI.enabled = false;
            }
            CheckIfNeedRecalc(property.isArray);
            if (_isComponent)
            {
                DrawComponent(position, property, label);
            }
            else if (_isArrayOfComponents)
            {
                DrawComponent(position, property, label, GetAcceptedFilterTypeFromContainerType(fieldInfo.FieldType));
            }
            else
            {
                EditorGUI.PropertyField(position, property, label, true);
            }
            GUI.enabled = v_oldGuiEnabled;
        }