Beispiel #1
0
        private T DoListField <T>(ref Rect position, ref Rect rect, Type type, TK key, T value)
        {
            if (value == null)
            {
                value = Misc.Default <T>(type);
            }

            if (typeof(System.Collections.IList).IsAssignableFrom(type))
            {
                var list = (System.Collections.IList)value;

                Rect listCountRect = rect;
                listCountRect.width = kButtonWidth;
                listCountRect.x     = rect.xMax + 6 - kButtonWidth * 2 - kLabelWidth;

                EditorGUI.LabelField(listCountRect, list.Count.ToString());

                Type elementType = null;
                var  attri       = type.GetCustomAttribute <CollectionAttribute>(true);
                if (attri != null)
                {
                    elementType = attri.valueType;
                }
                else if (type.IsGenericType)
                {
                    elementType = type.GetGenericArguments()[0];
                }

                if (elementType != null)
                {
                    Rect buttonRect = rect;
                    buttonRect.width = kButtonWidth;
                    buttonRect.x     = rect.xMax + 2;
                    if (GUI.Button(buttonRect, new GUIContent("+", "Add item"), EditorStyles.miniButton))
                    {
                        list.Add(Misc.Default(elementType));
                        SetTKFoldout(key, true);
                    }
                }
                position.y += 17;

                if (IsTKFoldout(key))
                {
                    rect.width -= kButtonWidth * 2.0f;
                    int count = list.Count;
                    for (int i = 0; i < count; ++i)
                    {
                        rect.y = position.y;

                        var removeRect = rect;
                        removeRect.y     = rect.y;
                        removeRect.width = kButtonWidth;
                        removeRect.x     = rect.xMax + 2 + kButtonWidth;
                        if (GUI.Button(removeRect, new GUIContent("x", "Remove item"), EditorStyles.miniButtonRight))
                        {
                            list.RemoveAt(i);
                            break;
                        }
                        //rect.width -= kButtonWidth;
                        list[i]     = DoField(ref position, ref rect, list[i].GetType(), list[i]);
                        position.y += 17;
                    }
                }
            }

            return(value);
        }