Array and List Extensions

These extensions will shorten the code by a large chunk, make the code readable, and easier to manage.

Some extensions may seem redundant like First and Convert, which are both present under the System.Linq namespace. While that’s true, Linq treats most - if not all - collection extensions as IEnumerables, while Nut Library treats arrays and lists separately, making implementation easier to understand.

EXTENSIONS

AtOrFirst, AtOrLast, and AtOrDefault

T AtOrFirst(int index)

Returns the item at the given index or the first in the array if none is found.

T AtOrLast(int index)

Returns the first item at the given index or the last item in the array if none is found.

T AtOrDefault(int index)

Returns the provided index or the default value respective to the array object’s type.

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

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

    void MyMethod()
    {
        Debug.Log(array.AtOrFirst(5)); // Prints "Text 4"
        Debug.Log(array.AtOrFirst(10)); // Prints "Text 0"
        Debug.Log(array.AtOrLast(5)); // Prints "Text 4"
        Debug.Log(array.AtOrLast(10)); // Prints "Text 5"
        Debug.Log(array.AtOrDefault(5)); // Prints "Text 4"
        Debug.Log(array.AtOrDefault(10)); // Prints "string.Empty"
    }
}

ContainsIndex

bool ContainsIndex(int index)

Tells if the array contains a specific index.

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

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

    void MyMethod()
    {
        if (array.ContainsIndex(1)) // Returns true
            Debug.Log(array[1]); // Prints "Text 1"

        if (array.Contains(7)) // Returns false
            Debug.Log(array[7]);
    }
}

Convert

T2 Convert<T1, T2>() where T1: T2

Converts the array of type T1 to an array of type T2 via casting. T1 must be inherited from T2.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public Transform[] transforms = new Transform[0];

    void MyMethod()
    {
        // ...
        Component[] comps = transforms.Convert<Transform, Component>();
        // ...
    }
}

FillWith

  • FillWith(T obj)

Fills an array with the given value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        // ...
        int[] values = new int[10];
        values.FillWith(5);
        // ...
    }
}

FillWithRandomColorsRGB/RGBA/BW/BWA

  • FillWithRandomColorsRGB(this Color/Color32 color, byte/float a = 255/1f)

Fills the array with random colors. The colors will have the given alpha value.

  • FillWithRandomColorsRGBA(this Color/Color32 color)

Fills the array with random colors. The alpha is also randomized.

  • FillWithRandomColorsBW(this Color/Color32 color, byte/float a = 255/1f)

Fills the array with random black and white levels. The colors will have the given alpha value.

  • FillWithRandomColorsBWA(this Color/Color32 color)

Fills the array with black and white levels. The alpha is also randomized.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        Texture2D tex = new Texture2D(128, 128);
        Color[] colors = FillWithRandomColorsRGB();
        tex.SetPixels(colors);
        tex.Apply();
    }
}

First, Last, SetFirst, and SetLast

T First()

Retrieves the first element of the array (array[0]).

T Last()

Retrieves the last element of the array (array[array.Length - 1]).

SetFirst(T item)

Sets the first element of an array.

SetLast(T item)

Sets the last element of the array.

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

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

    void MyMethod()
    {
        if (transforms.First() == "Text 0")
        {
            transforms.SetFirst(transforms.Last());
        }
        else
            transforms.SetLast(transforms.First());
    }
}

GetTypes

Type[] GetTypes()

Returns an array with the types of the items stored inside of the original array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    Component[] components = new Component[0];

    void MyMethod()
    {
        // ...
        Type[] types = components.GetTypes();
        // ...
    }
}

ListItems

string ListItems()

Returns a readable string version of the array.

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

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

    void Start()
    {
        // Prints "string[]: [ Text 0, Text 1, Text 2, Text 3, Text 4, Text 5 ]"
        Debug.Log(array.ListItems());
    }
}

MakeArray

  • T[] MakeArray(this T obj, int length)

Makes an array of the given size with the provided object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public Transform[] transforms = new Transform[0];

    void MyMethod()
    {
        float[] values = 29f.MakeArray(20);
    }
}

Next, Previous, and Random (Index, Set, and References)

int NextIndex(int index)

Returns the next index relative to the provided index.

int PreviousIndex(int index)

Returns the previous index relative to the provided index.

int RandomIndex()

Returns a random index inside of the boundaries of the array.

T Next(int index)

Returns the next item relative to the provided index.

T Previous(int index)

Returns the previous item relative to the provided index.

T Random()

Returns a random item inside of the boundaries of the array.

SetNext(int index, T item)

Sets the item next to the provided array to the given item.

SetPrevious(int index, T item)

Sets the item previous to the provided array to the given item.

SetRandom(T item)

Sets a random item of the array to the given item.

Note

All Next and Previous methods loop the array, I.e. when using Next at the top of the array it returns the bottom (array[0]), and when using Previous at the bottom of the array it returns the top (array[array.Length - 1]).

 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
29
30
31
32
33
34
35
36
37
38
using NutTools;
using UnityEngine;

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

    void MyMethod()
    {
        int next_index = array.NextIndex(2); // Returns 3.
        int next_index_loop = array.NextIndex(array.Length - 1); // Returns 0.

        int prev_index = array.PreviousIndex(2); // Returns 1.
        int prev_index_loop = array.PreviousIndex(0); // Returns 5, a.k.a. array.Length.

        int random_index = array.RandomIndex(); // Returns "Random.Range(0, array.Length)".

        string next = array.Next(2); // Returns Text 3.
        string next_loop = array.Next(array.Length - 1); // Returns Text 0.

        string prev = array.PreviousIndex(2); // Returns Text 1.
        string prev_loop = array.PreviousIndex(0); // Returns Text 5.

        string random = array.Random(); // Returns array[Random.Range(0, array.Length)].

        array.SetNext(2, "Next Text"); // Sets Text 3 to Next Text.
        array.SetNext(array.Length - 1, "Next Text"); // Sets Text 0 to Next Text.

        array.SetPrevious(2, "Previous Text"); // Sets Text 1 to Previous Text.
        array.SetPrevious(0, "Previous Text"); // Sets Text 5 to Previous Text.

        array.SetRandom("RandomText"); // Sets a random item on the list to Random Text.
    }
}

ParentAll, UnparentAll, and SetActiveAll

They control parenting and the active state of the object. Here are the many variants and permutations of the above methods:

ParentAll(Transform parent)

Parents all objects from the array to another transform.

ParentAll(Transform parent, int length)

Parents all objects from the array up until the provided length - 1 to another transform.

UnparentAll()

Parents all objects to the root of the scene (I.e. no parent).

UnparentAll(int length)

Parents all objects up to the provided length - 1 to the root of the scene (I.e. no parent).

SetActiveAll(bool state)

Sets the objects’ active state.

SetActiveAll(bool state, int length)

Sets the objects’ active state up to the provided length - 1.

ParentAndSetActiveAll(Transform parent, bool state)

Sets the objects’ parent to «parent» and the active state to «state».

ParentAndSetActiveAll(Transform parent, bool state, int length)

Sets the objects’ parent to «parent» and the active state to «state» up until the provided length - 1.

UnparentAndSetActiveAll(bool state)

Sets the objects’ parent to the root of the scene and the active state to «state».

UnparentAndSetActiveAll(bool state, int length)

Sets the objects’ parent to the root of the scene and the active state to «state» up until the provided length - 1.

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

public class MyClass: MonoBehaviour
{
    public GameObject[] objs = new GameObject[0];

    void MyMethod()
    {
        objs.ParentAll(transform);
        objs.ParentAll(transform, 5);
        objs.UnparentAll();
        objs.UnparentAll(5);
        objs.SetActiveAll(false);
        objs.SetActiveAll(false, 5);
        objs.ParentAndSetActiveAll(transform, true);
        objs.ParentAndSetActiveAll(transform, true, 5);
        objs.UnparentAndSetActiveAll(false);
        objs.UnparentAndSetActiveAll(true, 5);
    }
}

GET HELP

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

See also

  • GameObject and Component Extensions
  • ParticleSystem Extensions
  • Physics Extensions
  • Texture Extensions
  • Value Extensions