Exemple #1
0
        protected ReflectedProperty(ReflectedProperty parent, string name, Type declaredType, object value)
        {
            this.parent        = parent;
            this.name          = name;
            this.originalValue = value;
            this.actualValue   = value;
            this.declaredType  = declaredType;
            this.actualType    = value == null ? declaredType : value.GetType();

            this.label           = StringUtil.SplitAndTitlize(name);
            this.children        = new List <ReflectedProperty>(4);
            this.guiContent      = new GUIContent(label);
            this.changedChildren = new List <ReflectedProperty>();
            this.isHidden        = false;
            this.isDirty         = false;
            if (parent != null)
            {
                fieldInfo = parent.Type.GetField(name, BindFlags);
                object[] attrs = fieldInfo?.GetCustomAttributes(false);
                attributes = new List <Attribute>();
                if (attrs != null)
                {
                    for (int i = 0; i < attrs.Length; i++)
                    {
                        attributes.Add((Attribute)attrs[i]);
                    }
                }
                isHidden   = HasAttribute <HideInInspector>();
                isExpanded = HasAttribute <DefaultExpanded>();
            }
            this.drawer = EditorReflector.CreateReflectedPropertyDrawer(this);
        }
 protected virtual void SetElementCount(int size, bool shouldChange = true)
 {
     if (size > children.Count)
     {
         Type elementType = GetElementType();
         //because we know this will be null or a primitive,
         //it is safe to pass the same element to every new index
         //todo maybe make a real instance when we can
         object element = EditorReflector.GetDefaultForType(elementType);
         for (int i = children.Count; i < size; i++)
         {
             children.Add(CreateChild(this, string.Empty, elementType, element));
         }
         if (shouldChange)
         {
             SetChanged(true);
         }
     }
     else if (size < children.Count)
     {
         while (children.Count > size)
         {
             DestroyChild(children.RemoveAndReturnAtIndex(children.Count - 1));
         }
         if (shouldChange)
         {
             SetChanged(true);
         }
     }
 }
        protected virtual void ResizeActualValue()
        {
            IList list = actualValue as IList;

            if (children.Count == 0 || (list != null && list.Count == children.Count))
            {
                return;
            }
            list = list ?? EditorReflector.MakeInstance <IList>(declaredType);
            Debug.Assert(list != null, nameof(list) + " != null");
            if (list.Count > children.Count)
            {
                while (list.Count > children.Count)
                {
                    list.RemoveAt(list.Count - 1);
                }
            }
            else if (list.Count < children.Count)
            {
                while (list.Count < children.Count)
                {
                    list.Add(EditorReflector.GetDefaultForType(declaredType.GetGenericArguments()[0]));
                }
            }
            actualValue = list;
        }
Exemple #4
0
 protected object CreateValue(Type type)
 {
     if (type.IsArray)
     {
         return(Array.CreateInstance(type.GetElementType(), 0));
     }
     return(EditorReflector.MakeInstance(type));
 }
 public void InsertElement(int index)
 {
     if (index >= 0 && index <= ChildCount)
     {
         Type              elementType  = GetElementType();
         object            defaultValue = EditorReflector.GetDefaultForType(declaredType);
         ReflectedProperty child        = CreateChild(this, string.Empty, elementType, defaultValue);
         if (index == ChildCount)
         {
             AddElement(child);
             return;
         }
         children.Insert(index, child);
         SetChanged(true);
     }
 }
Exemple #6
0
        protected virtual void SetValue(object value)
        {
            Type previousType = actualType;

            if (value == null)
            {
                actualType  = declaredType;
                actualValue = EditorReflector.GetDefaultForType(declaredType);
            }
            else
            {
                actualType  = value.GetType();
                actualValue = value;
            }
            if (actualType != previousType)
            {
                this.drawer = EditorReflector.CreateReflectedPropertyDrawer(this);
            }
        }
        private void CreateChildren()
        {
            if (actualValue == null)
            {
                return;
            }

            DestroyChildren();

            circularReference = CheckCircularReference(this);

            UnityEngine.Debug.Assert(circularReference == null, "circularReference == null");

            FieldInfo[] fieldInfos = EditorReflector.GetFields(actualType);
            for (int i = 0; i < fieldInfos.Length; i++)
            {
                FieldInfo fi = fieldInfos[i];
                if (ShouldReflectProperty(fi))
                {
                    children.Add(CreateChild(this, fi.Name, fi.FieldType, fi.GetValue(actualValue)));
                }
            }
        }
        private void UpdateChildren()
        {
            FieldInfo[] fieldInfos = EditorReflector.GetFields(actualType);

            childSet = childSet ?? new List <ReflectedProperty>(8);

            for (int i = 0; i < children.Count; i++)
            {
                childSet.Add(children[i]);
            }

            for (int i = 0; i < fieldInfos.Length; i++)
            {
                FieldInfo info = fieldInfos[i];
                if (ShouldReflectProperty(info))
                {
                    ReflectedProperty child = children.Find(info, (c, fInfo) => c.name == fInfo.Name);
                    if (child != null)
                    {
                        UpdateChildIntenal(child, info.GetValue(actualValue));
                        childSet.Remove(child);
                    }
                    else
                    {
                        children.Add(CreateChild(this, info.Name, info.FieldType, info.GetValue(actualValue)));
                    }
                }
            }

            for (int i = 0; i < childSet.Count; i++)
            {
                children.Remove(childSet[i]);
            }

            childSet.Clear();
            UnityEngine.Debug.Assert(isDirty == false && changedChildren.Count == 0);
        }
 private static bool ShouldReflectProperty(FieldInfo fi)
 {
     return(!fi.IsNotSerialized && (fi.IsPublic || EditorReflector.HasAttribute(fi, typeof(SerializeField))));
 }
Exemple #10
0
 public void OnAfterDeserialize()
 {
     original = EditorReflector.FindDelegateWithSignature(signature);
     SetFromDelegate(original);
 }
Exemple #11
0
 public AbstractMethodPointer(MethodInfo info)
 {
     signature = MethodPointerUtils.CreateSignature(info);
     original  = EditorReflector.FindDelegateWithSignature(signature);
     SetFromDelegate(original);
 }
Exemple #12
0
 protected void UpdateDrawer()
 {
     drawer.OnDestroy();
     drawer = EditorReflector.CreateReflectedPropertyDrawer(this);
 }