Skip to content

Game.UI.Widgets.ArrayAdapter

Assembly:
Assembly-CSharp.dll

Namespace:
Game.UI.Widgets

Type:
public class ArrayAdapter

Base:
ListAdapterBase

Summary:
ArrayAdapter is a ListAdapter implementation specialised for handling managed arrays (System.Array) as the underlying collection. It provides insertion, deletion and clearing operations by creating new arrays and copying elements (System.Array.Copy) because managed arrays in .NET are fixed-size. The adapter relies on members inherited from ListAdapterBase (such as accessor and elementType) to read and write the typed array. Assertions (Unity.Assertions.Assert) are used to validate index bounds on insertion; note that assertions are enabled at the top of the file via #define UNITY_ASSERTIONS.


Fields

  • (none declared in this class)
    This class does not declare any private or public fields. It operates on inherited members from ListAdapterBase (for example, accessor, elementType and length as exposed/used by the base class).

Properties

  • (none declared in this class)
    No public or private properties are declared on ArrayAdapter itself. It uses properties/fields provided by the base class ListAdapterBase (such as the accessor used to GetTypedValue/SetTypedValue, and the elementType used when creating instances).

Constructors

  • public ArrayAdapter()
    Default parameterless constructor. No special construction logic is implemented in this class; initialization is expected to be handled by the base class or the UI system that constructs and configures the adapter.

Methods

  • public override void InsertElement(int index)
    Inserts a newly created element at the specified index. Behavior:
  • Asserts index >= 0 and index <= base.length (Unity.Assertions.Assert).
  • Retrieves the current typed array via base.accessor.GetTypedValue().
  • Creates a new element instance using ListAdapterBase.CreateInstance(base.elementType).
  • Allocates a new array of elementType with length base.length + 1.
  • Places the new element at the requested index.
  • If the previous array (typedValue) is non-null, copies elements before and after the insertion index into the new array using Array.Copy.
  • Writes the new array back with base.accessor.SetTypedValue(array). Notes and edge cases:
  • The method supports the case where the current typed array is null (it simply creates a single-element array).
  • Assertions are used for bounds checking; in builds where UNITY_ASSERTIONS are disabled, invalid indices may cause runtime exceptions from Array.Copy or other operations.

  • public override void DeleteElement(int index)
    Removes the element at the specified index. Behavior:

  • Retrieves the current typed array via base.accessor.GetTypedValue().
  • Allocates a new array with length typedValue.Length - 1.
  • Copies elements before the index and after the index (skipping the removed element) into the new array using Array.Copy.
  • Writes the new array back with base.accessor.SetTypedValue(array). Notes and edge cases:
  • This method assumes the current array is non-null and that index is within bounds; if not, Array.Copy or array length access will throw an exception.
  • No explicit assertion is present here in the class code, so callers should ensure index validity or the adapter should be used in contexts where the base ensures valid indices.

  • public override void Clear()
    Clears the array by setting an empty array of the appropriate elementType (length 0) via base.accessor.SetTypedValue(Array.CreateInstance(base.elementType, 0)). This replaces any existing array with a zero-length array of the same element type.

Performance and complexity: - All operations that modify the array allocate a new array and copy elements, therefore InsertElement and DeleteElement are O(n) in array length. This is typical for fixed-size arrays.

Safety and usage notes: - The adapter uses ListAdapterBase.CreateInstance(base.elementType) to create a default instance for inserted items; the behavior of CreateInstance depends on the base implementation (it may create a default value or attempt more complex initialization). - Assertions at the top (#define UNITY_ASSERTIONS) enable the index checks in InsertElement — in release builds without assertions, invalid indices can cause exceptions. - Because arrays are replaced wholesale on mutation, any external references to the previous array will not observe changes; code should read the array via the accessor after modifications.

Usage Example

// Example usage inside a derived UI adapter or widget lifecycle method.
// Assumes this code runs in a context where 'accessor' and 'elementType' (inherited fields)
// are already set up by the UI system or base class.

protected override void OnCreate()
{
    base.OnCreate();

    // Initialize the underlying array (example uses int as element type)
    // Note: accessor is a protected member provided by the base class.
    accessor.SetTypedValue(Array.CreateInstance(elementType, 3)); // create array of length 3

    // Insert a default instance at index 1
    InsertElement(1);

    // Delete element at index 0
    DeleteElement(0);

    // Clear the array
    Clear();
}

If you need an example that shows creating and wiring an ArrayAdapter from scratch (outside of the UI framework), tell me which base-class members (accessor, elementType) are available to you or provide the ListAdapterBase definition and I can produce a concrete, compiling example.