private void ApplyModifiedArray()
        {
            if (Properties.Count > 1)
            {
                Properties.RemoveRange(1, Properties.Count - 1);
            }
            if (VisibleProperties.Count > 1)
            {
                VisibleProperties.RemoveRange(1, VisibleProperties.Count - 1);
            }

            var array = Value;

            if (array == null && PropertyType.IsArray)
            {
                array = System.Array.CreateInstance(ArrayElementType, 0);
                Value = array;
            }
            for (int i = 0; i < ArraySize; i++)
            {
                var index       = i;
                var elementName = string.Format(CombineNameLayout, ElementStr, i);
                var element     = new RuntimeNativeSerializedProperty(RuntimeSerializedObject, null, ArrayElementType, string.Format(CombinePathLayout, PropertyPath, elementName), elementName, Depth + 1, () =>
                {
                    return(getMethod.Invoke(Value, new object[] { index }));
                }, (val) =>
                {
                    setMethod.Invoke(Value, new object[] { index, val });
                    Value = array;
                });

                Properties.Add(element);
                VisibleProperties.Add(element);
            }
        }
Esempio n. 2
0
        public RuntimeSerializedObject(object obj, object target, int id)
        {
            var type = target.GetType();

            if (obj is RuntimeSerializedProperty)
            {
                var runtimeSerializedProperty = obj as RuntimeSerializedProperty;
                owner = runtimeSerializedProperty.RuntimeSerializedObject;
                path  = runtimeSerializedProperty.PropertyPath;
            }
            else if (obj is SerializedProperty)
            {
                var serializedProperty = obj as SerializedProperty;
                owner = serializedProperty.serializedObject;
                path  = serializedProperty.propertyPath;
            }
            else
            {
                Debug.LogError(obj + "it must be RuntimeSerializedProperty type or SerializedProperty type!");
            }
            instanceID        = id;
            Target            = target;
            Name              = string.Format(CombineNameAndID, type.Name, instanceID);
            Type              = type.ToString();
            Properties        = new List <RuntimeNativeSerializedProperty>();
            VisibleProperties = new List <RuntimeNativeSerializedProperty>();

            var header = new RuntimeNativeSerializedProperty(this, null, typeof(System.Nullable), Name, Name, 0, null, null);

            Properties.Add(header);
            VisibleProperties.Add(header);

            var fields = type.GetAllInstanceFieldsFromCached();

            if (fields != null)
            {
                foreach (var field in fields)
                {
                    var property = new RuntimeNativeSerializedProperty(this, field, field.FieldType, field.Name, field.Name, 0, () =>
                    {
                        return(field.GetValue(Target));
                    }, (val) =>
                    {
                        field.SetValue(Target, val);
                        HasModifiedProperties = true;
                    });
                    Properties.Add(property);
                    if (field.IsVisible())
                    {
                        VisibleProperties.Add(property);
                    }
                }
            }
        }
 public bool NextVisible(bool enterChildren = false)
 {
     if (enterChildren && HasVisibleChildren)
     {
         nativeProperty = nativeProperty.VisibleProperties[0];
         return(true);
     }
     else
     {
         string path     = PropertyPath;
         string lastPath = null;
         while (path != lastPath)
         {
             var properties = GetVisiblePropertiesAtPath(path);
             for (int i = 0; i < properties.Count - 1; i++)
             {
                 if (Depth > properties[i].Depth && PropertyPath.StartsWith(properties[i].PropertyPath + StopStr))
                 {
                     nativeProperty = properties[i + 1];
                     return(true);
                 }
                 else if (EqualContents(properties[i], nativeProperty))
                 {
                     nativeProperty = properties[i + 1];
                     return(true);
                 }
             }
             lastPath = path;
             int length = path.LastIndexOf(StopChar);
             if (length > 0)
             {
                 path = path.Substring(0, length);
             }
         }
     }
     return(false);
 }
        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();
            }
        }
 private static bool EqualContents(RuntimeNativeSerializedProperty x, RuntimeNativeSerializedProperty y)
 {
     return(x.RuntimeSerializedObject == y.RuntimeSerializedObject && x.PropertyPath == y.PropertyPath);
 }
 public RuntimeSerializedProperty(RuntimeNativeSerializedProperty nativeProperty)
 {
     this.nativeProperty = nativeProperty;
 }