using System; using System.Collections.Generic; public class ItemEventArgs : EventArgs { public int ItemIndex { get; set; } } public class ItemList { private Listitems = new List (); public event EventHandler ItemAdded; public void AddItem(string item) { items.Add(item); OnItemAdded(new ItemEventArgs{ ItemIndex = items.Count - 1 }); } protected virtual void OnItemAdded(ItemEventArgs e) { ItemAdded?.Invoke(this, e); } } public class Program { static void Main(string[] args) { ItemList itemList = new ItemList(); itemList.ItemAdded += (sender, e) => Console.WriteLine($"Item added at index {e.ItemIndex}"); itemList.AddItem("Item 1"); itemList.AddItem("Item 2"); itemList.AddItem("Item 3"); } }
using UnityEngine; using System.Collections.Generic; public class ItemList : MonoBehaviour { private ListIn this example, we create a class called `ItemList` that has a list of `GameObject`s and an event called `ItemRemoved`. The `RemoveItem` method removes an item from the list, raises the `ItemRemoved` event, and then destroys the `GameObject`. We then subscribe to the `ItemRemoved` event and log the name of the removed item whenever an item is removed. In the `Update` method, we simulate removing an item from the list by pressing the space key. Package/Library: This concept of ItemEvent is not specific to any package or library, it is simply a way of implementing events in C# for tracking changes to a list of items.items = new List (); public event System.Action ItemRemoved; public void RemoveItem(GameObject item) { if (items.Contains(item)) { items.Remove(item); ItemRemoved?.Invoke(item); Destroy(item); } } } public class Example : MonoBehaviour { private ItemList itemList; void Start() { itemList = GetComponent (); itemList.ItemRemoved += (item) => Debug.Log($"Item {item.name} removed"); } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { itemList.RemoveItem(itemList.gameObject.transform.GetChild(0).gameObject); } } }