protected ListFieldBase(MemberData data, object owner, bool requiresSetter) { _owner = owner; _label = new GUIContent(data.name, data.description); this.memberName = data.member.Name; var prop = data.member as PropertyInfo; if (prop != null) { _reorderable = !prop.IsDefined <NotReorderableAttribute>(true); _itemType = GetItemType(prop.PropertyType); _list = (IList)prop.GetValue(owner, null); if (_list == null || requiresSetter) { _setter = (list) => { _list = list; prop.SetValue(owner, list, null); if (_simpleItemCreator != null) { _simpleItemCreator.SetParent(_list); } }; } } else { var field = data.member as FieldInfo; if (field != null) { _reorderable = !field.IsDefined <NotReorderableAttribute>(true); _itemType = GetItemType(field.FieldType); _list = (IList)field.GetValue(owner); if (_list == null || requiresSetter) { _setter = (list) => { _list = list; field.SetValue(owner, list); if (_simpleItemCreator != null) { _simpleItemCreator.SetParent(_list); } }; } } else { throw new ArgumentException("Invalid reflected member type, only fields and properties are supported."); } } _isSimpleType = SerializationMaster.ConverterExists(_itemType) || SerializationMaster.StagerExists(_itemType); if (_list != null) { var count = _list.Count; _editorItems = new List <EditorItem>(count); if (_isSimpleType) { _simpleItemCreator = new SimpleItemConstructor(_list, _itemType); for (int i = 0; i < count; i++) { var item = _simpleItemCreator.Conctruct(i, _list[i]); _editorItems.Add(ReflectMaster.Reflect(item)); } } else { for (int i = 0; i < count; i++) { _editorItems.Add(ReflectMaster.Reflect(_list[i])); } } } else { _editorItems = new List <EditorItem>(); } }
private void AddNew(Vector2 mousePos, AIInspectorState state) { if (_isSimpleType) { if (_list == null) { _setter(CreateList()); } if (_simpleItemCreator == null) { _simpleItemCreator = new SimpleItemConstructor(_list, _itemType); } var item = Activator.CreateInstance(_itemType); DoAdd(item, state); var itemWrapper = _simpleItemCreator.Conctruct(_list.Count - 1, item); _editorItems.Add(ReflectMaster.Reflect(itemWrapper)); return; } Action <Type[]> cb = (selectedTypes) => { if (_list == null) { _setter(CreateList()); } using (state.currentAIUI.undoRedo.bulkOperation) { for (int i = 0; i < selectedTypes.Length; i++) { var selectedType = selectedTypes[i]; if (_itemType.IsGenericType && selectedType.IsGenericType) { var genArgs = _itemType.GetGenericArguments(); selectedType = selectedType.GetGenericTypeDefinition().MakeGenericType(genArgs); } var item = Activator.CreateInstance(selectedType); DoAdd(item, state); _editorItems.Add(ReflectMaster.Reflect(item)); if (Application.isPlaying) { HandleVisualizerAdd(state, item); } } state.MarkDirty(); } }; // We do not want the button click itself to count as a change.. same as above. GUI.changed = false; var screenPos = EditorGUIUtility.GUIToScreenPoint(mousePos); AIEntitySelectorWindow.Get(screenPos, _itemType, cb); }