Game.UI.Widgets.ListAdapter
Assembly:
Game
Namespace:
Game.UI.Widgets
Type:
public class ListAdapter
Base:
ListAdapterBase
Summary:
Adapter implementation for editing IList instances in the UI editor system. This class provides concrete behaviour for inserting, deleting and clearing elements of a runtime IList via the base ListAdapterBase
Fields
- (None declared in this class)
This class does not declare private fields itself; it relies on fields defined in the base class ListAdapterBase(for example: accessor, generator, elementType, length, level).
Properties
public Type listType { get; set; }
Specifies the concrete IList type to create when the underlying list value is null and an insertion is requested. Typical value is typeof(List) for lists of T. If not set, CreateInstance (from the base) must be able to construct a usable IList instance.
Constructors
public ListAdapter()
Implicit parameterless constructor. Instances are typically created via the static factory FromListor by the editor generation system; no special constructor logic is defined in this class.
Methods
public override void InsertElement(int index)
Inserts a new default element at the specified index. Behavior:- Asserts that index >= 0 and index <= length (uses UNITY_ASSERTIONS).
- Obtains the IList via base.accessor.GetTypedValue().
- If the list is null, creates a new list instance of type listType via ListAdapterBase
.CreateInstance(listType) and assigns it back through the accessor. -
Creates a new element instance using the base CreateInstance for the configured elementType and inserts it into the list at index.
-
public override void DeleteElement(int index)
Removes the element at the given index from the underlying IList (calls RemoveAt on the IList returned by base.accessor.GetTypedValue()). -
public override void Clear()
Clears the underlying IList if it is not null (calls Clear()). -
public static ListAdapter FromList<T>([NotNull] List<T> list, IEditorGenerator generator)
Factory helper that creates a ListAdapter bound to an existing List. It sets: - accessor to a DelegateAccessor
that returns the provided list, - generator to the provided IEditorGenerator,
- elementType to typeof(T),
- listType to typeof(List
), - level to 0.
This is a convenient way to wrap a concrete List
for editing via the ListAdapter.
Usage Example
// Create a list and an IEditorGenerator (generator must be provided by the editor system)
List<MyItem> myItems = new List<MyItem>();
IEditorGenerator generator = /* obtain or implement generator */;
// Create a ListAdapter for the list
ListAdapter adapter = ListAdapter.FromList(myItems, generator);
// Insert a new element at index 0 (will create a default instance of MyItem)
adapter.InsertElement(0);
// Delete the element we just added
adapter.DeleteElement(0);
// Clear the list
adapter.Clear();
Notes and implementation details:
- Index checks use Unity assertions (UNITY_ASSERTIONS define present in source).
- List and element instances are created via ListAdapterBase