Callbacks

3_ScriptableList

  1. OnItemAdded / OnItemRemoved

Useful if you want to do different things depending on if an item is added or removed. To see an example of this, click on the VfxSpawner.cs.

Here we want to display a different VFX depending on if a player has been spawned or destroyed. The player argument is useful, as we can get its position and spawn the VFX there.

 public void Awake()
{
    scriptableListPlayer.OnItemRemoved += OnPlayerDestroyed;
    scriptableListPlayer.OnItemAdded += OnPlayerSpawned;
}

public void OnDestroy()
{
    scriptableListPlayer.OnItemRemoved -= OnPlayerDestroyed;
    scriptableListPlayer.OnItemAdded -= OnPlayerSpawned;
}

private void OnPlayerSpawned(Player player)
{
    Instantiate(_spawnVFXPrefab, player.transform.position, Quaternion.identity, transform);
}

private void OnPlayerDestroyed(Player player)
{
    Instantiate(_destroyVFXPrefab, player.transform.position, Quaternion.identity, transform);
}
  1. OnItemCountChanged

This event is useful if you only care about when the number of elements in the list is changed.

The example for this one is the ListCount.cs

Scene Hierarchy: UICanvas/Bottom/ListCount

void Awake()
{
    scriptableListPlayer.OnItemCountChanged += UpdateText;
}

private void OnDestroy()
{
    scriptableListPlayer.OnItemCountChanged -= UpdateText;
}

private void UpdateText()
{
    _text.text = $"Count : {scriptableListPlayer.Count}";
}

We simply update the text with the number of elements in the list.

  1. OnItemsAdded / OnItemsRemoved

This event is useful if you don’t want the OnItemAdded/OnItemRemoved event triggered every time you add/remove an item. This will be called once after a range of items have been added/removed.

Last updated