Los eventos permiten que un sistema avise que ocurrió algo sin conocer directamente quién va a reaccionar.
using System;
public class PlayerHealth : MonoBehaviour
{
public event Action<int> OnHealthChanged;
private int vida = 100;
public void RecibirDaño(int daño)
{
vida -= daño;
OnHealthChanged?.Invoke(vida);
}
}
void OnEnable()
{
playerHealth.OnHealthChanged += ActualizarUI;
}
void OnDisable()
{
playerHealth.OnHealthChanged -= ActualizarUI;
}
PlayerHealth no necesita conocer directamente al HUD.
PlayerHealth
↓ evento
HUD
PlayerHealth
↓ mismo evento
Audio
PlayerHealth
↓ mismo evento
Effects
event Action
event Action<T>
?.Invoke(...)