private bool CanInitializeNewObject()
        {
#if UNITY_EDITOR || !NETFX_CORE
            if (BoundVariableType.IsAbstract || BoundVariableType.IsInterface)
#else
            if (BoundVariableType.GetTypeInfo().IsAbstract || BoundVariableType.GetTypeInfo().IsInterface)
#endif
            { return(false); }

            if (typeof(ScriptableObject).IsAssignableFrom(BoundVariableType))
            {
                return(true);
            }

            if (typeof(UnityEngine.Object).IsAssignableFrom(BoundVariableType))
            {
                return(false);
            }

            if (BoundVariableType.IsArray)
            {
                return(false);
            }

#if UNITY_EDITOR || !NETFX_CORE
            if (BoundVariableType.IsGenericType && BoundVariableType.GetGenericTypeDefinition() == typeof(List <>))
#else
            if (BoundVariableType.GetTypeInfo().IsGenericType&& BoundVariableType.GetGenericTypeDefinition() == typeof(List <>))
#endif
            { return(false); }

            return(true);
        }
Example #2
0
        protected override void OnBound(MemberInfo variable)
        {
            base.OnBound(variable);

            isArray     = BoundVariableType.IsArray;
            elementType = isArray ? BoundVariableType.GetElementType() : BoundVariableType.GetGenericArguments()[0];
        }
Example #3
0
        private bool CanInitializeNewObject()
        {
            if (BoundVariableType.IsAbstract || BoundVariableType.IsInterface)
            {
                return(false);
            }

            if (typeof(ScriptableObject).IsAssignableFrom(BoundVariableType))
            {
                return(true);
            }

            if (typeof(UnityEngine.Object).IsAssignableFrom(BoundVariableType))
            {
                return(false);
            }

            if (BoundVariableType.IsArray)
            {
                return(false);
            }

            if (BoundVariableType.IsGenericType && BoundVariableType.GetGenericTypeDefinition() == typeof(List <>))
            {
                return(false);
            }

            return(true);
        }
        private void InitializeObject()
        {
            if (CanInitializeNewObject())
            {
                Value = BoundVariableType.Instantiate();

                RegenerateElements();
                IsExpanded = true;
            }
        }
Example #5
0
        private void OnSizeChange(ChangeEvent <string> input)
        {
            if (int.TryParse(input.newValue, out int size))
            {
                if (size > 100)
                {
                    UnityEngine.Debug.LogWarning("输入的数组长度过大");
                    sizeInput.value = input.previousValue;
                    return;
                }

                if (size != Length && size >= 0)
                {
                    int currLength = Length;
                    if (IsArray)
                    {
                        Array array    = (Array)Value;
                        Array newArray = Array.CreateInstance(BoundVariableType.GetElementType(), size);
                        if (size > currLength)
                        {
                            if (array != null)
                            {
                                Array.ConstrainedCopy(array, 0, newArray, 0, currLength);
                            }

                            for (int i = size - currLength; i < currLength; i++)
                            {
                                object v = Activator.CreateInstance(BoundVariableType);
                                newArray.SetValue(v, i);
                            }
                        }
                        else
                        {
                            Array.ConstrainedCopy(array, 0, newArray, 0, size);
                        }

                        Value = newArray;
                    }
                    else
                    {
                        IList list = (IList)Value;

                        int differLength = size - currLength;
                        if (differLength > 0)
                        {
                            if (list == null)
                            {
                                list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(BoundVariableType.GetGenericArguments()[0]));
                            }

                            for (int i = 0; i < differLength; i++)
                            {
                                list.Add(default);
Example #6
0
        private void RefreshValue()
        {
            try
            {
                m_value = getter();
            }
            catch
            {
#if UNITY_EDITOR || !NETFX_CORE
                if (BoundVariableType.IsValueType)
#else
                if (BoundVariableType.GetTypeInfo().IsValueType)
#endif
                { m_value = Activator.CreateInstance(BoundVariableType); }
                else
                {
                    m_value = null;
                }
            }
        }
        private void GenerateExposedMethodButtons()
        {
            if (Inspector.ShowRemoveComponentButton && typeof(Component).IsAssignableFrom(BoundVariableType) && !typeof(Transform).IsAssignableFrom(BoundVariableType))
            {
                CreateExposedMethodButton(GameObjectField.removeComponentMethod, () => this, (value) => { });
            }

            ExposedMethod[] methods = BoundVariableType.GetExposedMethods();
            if (methods != null)
            {
                bool isInitialized = Value != null && !Value.Equals(null);
                for (int i = 0; i < methods.Length; i++)
                {
                    ExposedMethod method = methods[i];
                    if ((isInitialized && method.VisibleWhenInitialized) || (!isInitialized && method.VisibleWhenUninitialized))
                    {
                        CreateExposedMethodButton(method, () => Value, (value) => Value = value);
                    }
                }
            }
        }
Example #8
0
        private void GenerateMethods()
        {
            ExposedMethod[] methods = BoundVariableType.GetExposedMethods();
            if (methods != null)
            {
                bool isInitialized = Value != null && !Value.Equals(null);
                for (int i = 0; i < methods.Length; i++)
                {
                    ExposedMethod method = methods[i];
                    if ((isInitialized && method.VisibleWhenInitialized) || (!isInitialized && method.VisibleWhenUninitialized))
                    {
                        ExposedMethodField methodDrawer = (ExposedMethodField)Inspector.CreateDrawerForType(typeof(ExposedMethod), drawArea, Depth + 1, false);
                        if (methodDrawer != null)
                        {
                            methodDrawer.BindTo(typeof(ExposedMethod), string.Empty, () => Value, (value) => Value = value);
                            methodDrawer.SetBoundMethod(method);

                            exposedMethods.Add(methodDrawer);
                        }
                    }
                }
            }
        }
Example #9
0
        private object GetTemplateElement(object value)
        {
            Array array = null;
            IList list  = null;

            if (isArray)
            {
                array = (Array)value;
            }
            else
            {
                list = (IList)value;
            }

            object template    = null;
            Type   elementType = isArray ? BoundVariableType.GetElementType() : BoundVariableType.GetGenericArguments()[0];

#if UNITY_EDITOR || !NETFX_CORE
            if (elementType.IsValueType)
#else
            if (elementType.GetTypeInfo().IsValueType)
#endif
            {
                if (isArray && array != null && array.Length > 0)
                {
                    template = array.GetValue(array.Length - 1);
                }
                else if (!isArray && list != null && list.Count > 0)
                {
                    template = list[list.Count - 1];
                }
                else
                {
                    template = Activator.CreateInstance(elementType);
                }
            }
            else if (typeof(Object).IsAssignableFrom(elementType))
            {
                if (isArray && array != null && array.Length > 0)
                {
                    template = array.GetValue(array.Length - 1);
                }
                else if (!isArray && list != null && list.Count > 0)
                {
                    template = list[list.Count - 1];
                }
                else
                {
                    template = null;
                }
            }
            else if (elementType.IsArray)
            {
                template = Array.CreateInstance(elementType, 0);
            }
#if UNITY_EDITOR || !NETFX_CORE
            else if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(List <>))
#else
            else if (elementType.GetTypeInfo().IsGenericType&& elementType.GetGenericTypeDefinition() == typeof(List <>))
#endif
            { template = Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType)); }
            else
            {
                template = elementType.Instantiate();
            }

            return(template);
        }
Example #10
0
        private bool OnSizeChanged(BoundInputField source, string input)
        {
            int value;

            if (int.TryParse(input, NumberStyles.Integer, RuntimeInspectorUtils.numberFormat, out value) && value >= 0)
            {
                int currLength = Length;
                if (currLength != value)
                {
                    if (isArray)
                    {
                        Array array    = (Array)Value;
                        Array newArray = Array.CreateInstance(BoundVariableType.GetElementType(), value);
                        if (value > currLength)
                        {
                            if (array != null)
                            {
                                Array.ConstrainedCopy(array, 0, newArray, 0, currLength);
                            }

                            for (int i = currLength; i < value; i++)
                            {
                                object template = GetTemplateElement(array);
                                if (template != null)
                                {
                                    newArray.SetValue(template, i);
                                }
                            }
                        }
                        else
                        {
                            Array.ConstrainedCopy(array, 0, newArray, 0, value);
                        }

                        Value = newArray;
                    }
                    else
                    {
                        IList list        = (IList)Value;
                        int   deltaLength = value - currLength;
                        if (deltaLength > 0)
                        {
                            if (list == null)
                            {
                                list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(BoundVariableType.GetGenericArguments()[0]));
                            }

                            for (int i = 0; i < deltaLength; i++)
                            {
                                list.Add(GetTemplateElement(list));
                            }
                        }
                        else
                        {
                            for (int i = 0; i > deltaLength; i--)
                            {
                                list.RemoveAt(list.Count - 1);
                            }
                        }

                        Value = list;
                    }

                    Inspector.RefreshDelayed();
                }

                return(true);
            }

            return(false);
        }
Example #11
0
 protected override void OnBound()
 {
     base.OnBound();
     elementType     = IsArray ? BoundVariableType.GetElementType() : BoundVariableType.GetGenericArguments()[0];
     sizeInput.value = Length.ToString();
 }