Essential Systems / Observables
Observables
Enjoying Essential Systems?Leave a reviewLearn to use the included observables.
AObservable<T>
Observe any object. Implement
IObserver<T>on the class that should react to changes.
public class TestObservable : AObservable<TestObservable>
{
protected override void NotifyObserver(IObserver<TestObservable> observer)
{
observer.Notify(this);
}
private void Update()
{
NotifyObservers();
}
public void Test()
{
Debug.Log("Notified");
}
}
public class TestObserver : MonoBehaviour, IObserver<TestObservable>
{
public TestObservable observable;
public void Notify(TestObservable other)
{
other.Test();
}
}
Observable<T>
Observe any property for value changes.
public class Test : MonoBehaviour
{
public Observable<float> observable; // can be any type
private void Start()
{
observable.V = 1;
}
private void OnEnable()
{
observable.onValueChanged += OnChanged;
}
private void OnDisable()
{
observable.onValueChanged -= OnChanged;
}
private void OnChanged(float v)
{
Debug.Log("Value changed");
}
}
Always unsubscribe in
OnDisable()to avoid leaks.
Last updated

