コード例 #1
0
        public RuntimeNativeSerializedProperty(RuntimeSerializedObject runtimeSerializedObject, FieldInfo propertyField, System.Type type, string propertyPath, string name, int depth, System.Func <object> getValueCallback, System.Action <object> setValueCallback)
        {
            Editable = true;

            RuntimeSerializedObject = runtimeSerializedObject;
            PropertyType            = type;
            PropertyPath            = propertyPath;
            Name        = name;
            DisplayName = name;
            DisplayName = Regex.Replace(DisplayName, UppercasePattern, match => { var str = match.ToString(); return(char.ToUpperInvariant(str[0]) + str.Substring(1)); });
            DisplayName = Regex.Replace(DisplayName, SeparatePattern, match => { var str = match.ToString(); return(str + SpaceStr); });
            Depth       = depth;

            Type = type.ToString();

            this.GetValueCallback += getValueCallback;
            this.SetValueCallback += setValueCallback;

            if (propertyField != null)
            {
                var attrs = propertyField.GetCustomAttributes(typeof(FixedBufferAttribute), false);
                if (attrs != null && attrs.Length > 0)
                {
                    var attr     = (FixedBufferAttribute)attrs[0];
                    var property = new RuntimeNativeSerializedProperty(RuntimeSerializedObject, null, RuntimeSerializedPropertyType.FixedBufferSize, string.Format(CombinePathLayout, PropertyPath, SizeStr), SizeStr, Depth + 1, () =>
                    {
                        return(attr.Length);
                    }, null);

                    IsFixedBuffer          = true;
                    property.Editable      = false;
                    property.IsFixedBuffer = true;

                    Properties = new List <RuntimeNativeSerializedProperty>()
                    {
                        property
                    };
                    VisibleProperties = new List <RuntimeNativeSerializedProperty>()
                    {
                        property
                    };

                    var elementType = attr.ElementType;

                    for (int i = 0; i < attr.Length; i++)
                    {
                        var index       = i;
                        var elementName = string.Format(CombineNameLayout, ElementStr, i);
                        var element     = new RuntimeNativeSerializedProperty(RuntimeSerializedObject, null, elementType, string.Format(CombinePathLayout, PropertyPath, elementName), elementName, Depth + 1, () =>
                        {
                            return(FixedBufferUtility.GetValue(Value, attr, index));
                        }, (val) =>
                        {
                            Value = FixedBufferUtility.SetValue(Value, PropertyType, attr, index, val);
                        });

                        Properties.Add(element);
                        VisibleProperties.Add(element);
                    }
                }
            }

            if (!IsFixedBuffer && PropertyType.HasChildren())
            {
                var fields = PropertyType.GetAllInstanceFieldsFromCached();
                if (fields != null)
                {
                    Properties = new List <RuntimeNativeSerializedProperty>();
                    foreach (var field in fields)
                    {
                        var property = new RuntimeNativeSerializedProperty(RuntimeSerializedObject, field, field.FieldType, string.Format(CombinePathLayout, PropertyPath, field.Name), field.Name, Depth + 1, () =>
                        {
                            return(field.GetValue(Value));
                        }, (val) =>
                        {
                            var obj = Value;
                            field.SetValue(obj, val);
                            Value = obj;
                        });

                        Properties.Add(property);

                        if (field.IsVisible())
                        {
                            if (VisibleProperties == null)
                            {
                                VisibleProperties = new List <RuntimeNativeSerializedProperty>();
                            }
                            VisibleProperties.Add(property);
                        }
                    }
                }
            }

            if (IsArray)
            {
                ArraySizeChanged += OnArraySizeChanged;
                var property = new RuntimeNativeSerializedProperty(RuntimeSerializedObject, null, RuntimeSerializedPropertyType.ArraySize, string.Format(CombinePathLayout, PropertyPath, SizeStr), SizeStr, Depth + 1, () =>
                {
                    return((int)getCountMethod.Invoke(Value, null));
                }, (val) =>
                {
                    ArraySizeChanged.Invoke((int)val);
                });
                property.ArraySizeChanged += OnArraySizeChanged;

                foreach (var method in PropertyType.GetMethods())
                {
                    if (PropertyType.IsArray)
                    {
                        if (method.Name.ToLower() == Get_LengthStr)
                        {
                            getCountMethod          = method;
                            property.getCountMethod = method;
                        }
                        else if (method.Name.ToLower() == GetStr)
                        {
                            getMethod          = method;
                            property.getMethod = method;
                        }
                        else if (method.Name.ToLower() == SetStr)
                        {
                            setMethod          = method;
                            property.setMethod = method;
                        }
                    }
                    else
                    {
                        if (method.Name.ToLower() == Get_CountStr)
                        {
                            getCountMethod          = method;
                            property.getCountMethod = method;
                        }
                        else if (method.Name.ToLower() == Get_ItemStr)
                        {
                            getMethod          = method;
                            property.getMethod = method;
                        }
                        else if (method.Name.ToLower() == Set_ItemStr)
                        {
                            setMethod          = method;
                            property.setMethod = method;
                        }
                        else if (method.Name.ToLower() == AddStr)
                        {
                            addMethod          = method;
                            property.addMethod = method;
                        }
                        else if (method.Name.ToLower() == RemoveRangeStr)
                        {
                            removerangeMethod          = method;
                            property.removerangeMethod = method;
                        }
                    }
                }

                Properties = new List <RuntimeNativeSerializedProperty>()
                {
                    property
                };
                VisibleProperties = new List <RuntimeNativeSerializedProperty>()
                {
                    property
                };

                ApplyModifiedArray();
            }
        }