using UnityEngine;
using UnityEngine.Events;
namespace LobbyRelaySample
{
///
/// MonoBehaviour that will automatically handle setting up to observe something. It also exposes an event so some other component can effectively observe it as well.
///
public abstract class ObserverBehaviour : MonoBehaviour where T : Observed
{
public T observed { get; set; }
public UnityEvent OnObservedUpdated;
protected virtual void UpdateObserver(T obs)
{
observed = obs;
OnObservedUpdated?.Invoke(observed);
}
public void BeginObserving(T target)
{
if (target == null)
{
Debug.LogError($"Needs a Target of type {typeof(T)} to begin observing.", gameObject);
return;
}
UpdateObserver(target);
observed.onChanged += UpdateObserver;
}
public void EndObserving()
{
if (observed == null)
return;
if (observed.onChanged != null)
observed.onChanged -= UpdateObserver;
observed = null;
}
void Awake()
{
if (observed == null)
return;
BeginObserving(observed);
}
void OnDestroy()
{
if (observed == null)
return;
EndObserving();
}
}
}