Exemplo n.º 1
0
        // does this work right?
        public void TrimExcess()
        {
            if (this.Count >= this.Capacity * 0.9)
            {
                return;
            }

            int newCapacity = FlaiMath.Max(4, this.Count);

            Array.Resize(ref _items, newCapacity); // not 100% if works, but I think it does
        }
        public static bool GetDrawFunction(Type type, MemberReference memberReference, ShowInInspectorAttribute attribute, out DrawFunction drawFunction)
        {
            if (_drawFunctions.TryGetValue(type, out drawFunction))
            {
                return(true);
            }

            DrawFunctionGenerator generator;

            if (_drawFunctionGenerators.TryGetValue(type, out generator))
            {
                drawFunction = generator(memberReference, attribute);
                return(true);
            }

            if (type.IsEnum)
            {
                drawFunction = (n, v, o) => EditorGUILayout.EnumPopup(n, (Enum)v, o);
                return(true);
            }
            else if (typeof(UnityObject).IsAssignableFrom(type))
            {
                drawFunction = (n, v, o) => EditorGUILayout.ObjectField(n, (UnityObject)v, type, true, o);
                return(true);
            }
            else if (type.IsArray)
            {
                Type         elementType = type.GetElementType();
                DrawFunction elementDrawFunction;
                if (InternalPropertyDrawer.GetDrawFunction(elementType, out elementDrawFunction))
                {
                    drawFunction = (label, value, parameters) =>
                    {
                        const int IndentationAmount = 20;
                        Array     array             = (Array)value;

                        // draw the name of the array
                        EditorGUILayout.LabelField(label, EditorStyles.boldLabel);

                        // draw the length (and '+' & '-' buttons)
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Space(IndentationAmount);
                        int newLength = EditorGUILayout.IntField("Length", array.Length);
                        if (GUILayout.Button("+", GUILayout.Width(24)))
                        {
                            newLength++;
                        }
                        else if (GUILayout.Button("-", GUILayout.Width(24)))
                        {
                            newLength = FlaiMath.Max(0, newLength - 1);
                        }
                        EditorGUILayout.EndHorizontal();

                        // if the size has been changed, then update it
                        if (newLength != array.Length && newLength > 0)
                        {
                            Array newArray = (Array)Activator.CreateInstance(array.GetType(), newLength);
                            if (array.Length > 0)
                            {
                                for (int i = 0; i < newArray.Length; i++)
                                {
                                    var elementValue = (i >= array.Length) ? array.GetValue(array.Length - 1) : array.GetValue(i);
                                    newArray.SetValue(elementValue, i);
                                }
                            }

                            array = newArray;
                        }

                        // draw all the elements
                        for (int i = 0; i < array.Length; i++)
                        {
                            EditorGUILayout.BeginHorizontal();
                            GUILayout.Space(IndentationAmount);
                            array.SetValue(elementDrawFunction("Element " + i, array.GetValue(i), null), i);
                            EditorGUILayout.EndHorizontal();
                        }

                        return(array);
                    };

                    return(true);
                }
            }

            return(false);
        }