NutArrayUtils and NutListUtils

UTILITIES

Append

  • Append(ref T[] array, T item)

Adds an item to the end of the array.

Note

ArrayUtils-only content. Equivalent to List.Add.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    string[] array =
    {
        "Text 0", "Text 1", "Text 2",
        "Text 3", "Text 4", "Text 5"
    };

    void MyMethod()
    {
        // Array is set to
        // [ "Text 0", "Text 1", "Text 2", "Text 3", "Text 4", "Text 5", "Text 6" ]
        NutArrayUtils.Append(ref array, "Text 6");
    }

}

Insert and RemoveAt

  • Insert(ref T[] array, T item, int index)
  • RemoveAt(ref T[] array, int index)

Insert adds an item in the specified index, and moves all items in the array one index lower, expanding the array by one. RemoveAt removes the item at the specified index, moves all lower items in the array one index up, and shrinks the array.

Note

ArrayUtils-only content. Equivalent to List<T>.Insert() and List<T>.RemoveAt() for arrays, respectively.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    string[] array =
    {
        "Text 0", "Text 1", "Text 2",
        "Text 3", "Text 4", "Text 5"
    };

    void MyMethod()
    {
        // Array is set to
        // [ "Text 0", "Text 1", "Text 2", "Inserted Text", "Text 3", "Text 4", "Text 5" ]
        NutArrayUtils.Insert(ref array, "Inserted Text", 3);

        // Array is set to
        // [ "Text 0", "Text 1", "Text 2", "Text 3", "Text 4", "Text 5" ]
        NutArrayUtils.RemoveAt(ref array, 3);
    }
}

MoveUp and MoveDown

  • MoveUp(ref T[]/List<T> array, int index)
  • MoveDown(ref T[]/List<T> array, int index)

MoveUp swaps the item at index with the one an index above. If index is 0, the item will be swapped with the last entry in the array. MoveDown swaps the item at index with the one an index below. If index is array.Length - 1 (the last index), the item will be swapped with the first entry in the array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    string[] array =
    {
        "Text 0", "Text 1", "Text 2",
        "Text 3", "Text 4", "Text 5"
    };

    List<string> list = new List<string>()
    {
        "Text 0", "Text 1", "Text 2",
        "Text 3", "Text 4", "Text 5"
    };

    void MyMethod()
    {
        // Array is set to
        // [ "Text 0", "Text 2", "Text 1", "Text 3", "Text 4", "Text 5" ]
        NutArrayUtils.MoveUp(ref array, 2);

        // List is set to
        // [ "Text 0", "Text 1", "Text 3", "Text 2", "Text 4", "Text 5" ]
        NutListUtils.MoveDown(ref list, 2);
    }
}

OffsetUp and OffsetDown

  • OffsetUp(ref T[]/List<T> array, int steps)
  • OffsetDown(ref T[]/List<T> array, int steps)

OffsetUp moves all values up on the array as many times as specified. OffsetDown moves all items down on the array as many times as specified.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    string[] array =
    {
        "Text 0", "Text 1", "Text 2",
        "Text 3", "Text 4", "Text 5"
    };

    List<string> list = new List<string>()
    {
        "Text 0", "Text 1", "Text 2",
        "Text 3", "Text 4", "Text 5"
    };

    void MyMethod()
    {
        // Array is set to
        // [ "Text 1", "Text 2", "Text 3", "Text 4", "Text 5", "Text 0" ]
        NutArrayUtils.OffsetUp(ref array);

        // List is set to
        // [ "Text 4", "Text 5", "Text 0", "Text 1", "Text 2", "Text 3" ]
        NutListUtils.OffsetDown(ref list, 2);
    }
}

GET HELP

Join this Discord server to get help from the community, suggest new features, and vote on future updates!

See also