diff --git a/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs b/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs index 16ab8448..2fac0a3d 100644 --- a/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs +++ b/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs @@ -1,11 +1,12 @@ -using System.Threading; +using System; +using System.Threading; using UnityEngine; namespace R3 { - internal static class MonoBehaviourExtensions + public static class MonoBehaviourExtensions { - public static CancellationToken GetDestroyCancellationToken(this MonoBehaviour value) + internal static CancellationToken GetDestroyCancellationToken(this MonoBehaviour value) { // UNITY_2022_2_OR_NEWER has MonoBehavior.destroyCancellationToken #if UNITY_2022_2_OR_NEWER @@ -14,5 +15,17 @@ public static CancellationToken GetDestroyCancellationToken(this MonoBehaviour v return CancellationToken.None;; #endif } + +#if UNITY_2022_2_OR_NEWER + + public static CancellationTokenRegistration AddTo(this IDisposable disposable, MonoBehaviour value) + { + return value.destroyCancellationToken.Register(state => + { + ((IDisposable)state!).Dispose(); + }, disposable, false); + } + +#endif } } diff --git a/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs b/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs index a15318c3..1a5d265b 100644 --- a/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs +++ b/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs @@ -4,38 +4,30 @@ public class NewBehaviourScript : MonoBehaviour { - IDisposable d; - void Start() { - var a = Observable.TimerFrame(5, 100) + Observable.TimerFrame(5, 100) .TakeUntil(this.destroyCancellationToken) .Subscribe(x => { Debug.Log(Time.time); - }); - + }) + .AddTo(this); - - var b = Observable.EveryUpdate() + Observable.EveryUpdate() .Where(x => true) .Subscribe(x => { Debug.Log(Time.frameCount); - }); + }) + .AddTo(this); - var c = Observable.EveryValueChanged(this, x => x.transform, destroyCancellationToken) + Observable.EveryValueChanged(this, x => x.transform, destroyCancellationToken) .Select(x => x) .Subscribe(x => { Debug.Log(x); - }); - - d = Disposable.Combine(a, b, c); - } - - void OnDestroy() - { - d.Dispose(); + }) + .AddTo(this); } } diff --git a/src/R3/Disposable.cs b/src/R3/Disposable.cs index bc94f381..b38649ac 100644 --- a/src/R3/Disposable.cs +++ b/src/R3/Disposable.cs @@ -27,7 +27,7 @@ public static void AddTo(this IDisposable disposable, ICollection d disposables.Add(disposable); } - public static CancellationTokenRegistration RegisterTo(this IDisposable disposable, CancellationToken cancellationToken) + public static CancellationTokenRegistration AddTo(this IDisposable disposable, CancellationToken cancellationToken) { if (!cancellationToken.CanBeCanceled) throw new ArgumentException("Require CancellationToken CanBeCanceled"); @@ -468,9 +468,9 @@ public IDisposable Build() return result; } - public CancellationTokenRegistration RegisterTo(CancellationToken cancellationToken) + public CancellationTokenRegistration AddTo(CancellationToken cancellationToken) { - return Build().RegisterTo(cancellationToken); + return Build().AddTo(cancellationToken); } public void Dispose()