Let's create a more complete generic list class to store some fictional inventory items with the following steps:
- Create a new C# script in the Scripts folder, name it InventoryList, and update its code to the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 1
public class InventoryList<T>
{
// 2
public InventoryList()
{
Debug.Log("Generic list initalized...");
}
}
- Create a new instance of InventoryList in GameBehavior:
public class GameBehavior : MonoBehaviour, IManager
{
// ... No changes needed ...
void Start()
{
Initialize();
// 3
InventoryList<string> inventoryList = new
InventoryList<string>();
}
// ... No changes to Initialize or OnGUI ...
}
Let's break down the code:
- Declares a new generic class named InventoryList with a T type parameter...