Observable Wrapper of event Action<T>
for .NET / Unity
Runtime/Reactive/ObservableEvent.cs
HeaderDoc
(c) 2025 Sator Imaging, Licensed under the MIT License https://github.com/sator-imaging/Unity-Fundamentals
Similar to and different from ObservableAction<T>
, this class completely
encapsulates actual event field inside to provide more elegant look & feel.
How to Use
// keep raw instance private to prevent event invocation
private ObservableEvent<int> m_myEvent = new();
// provide readonly (sub/unsub) interface to consumers
public IObservableEvent<int> MyEvent => m_myEvent;
// invoke event
m_myEvent.Invoke(310);
// consumer can only perform subscribe/unsubscribe
MyEvent.Subscribe(myAction)
.BindTo(cancellationToken); // unsubscribe when token is canceled.
.AddTo(disposables); // or, add to IDisposable collection to unsub later.
MyEvent.Unsubscribe(myAction); // ofcourse able to unsubscribe manually.
// clear all event handlers. note that event is still exist and accepts new handler
m_myEvent.Dispose();