From bc3301a6c3571bc9e59407653d919b176bc39b53 Mon Sep 17 00:00:00 2001 From: neuecc Date: Sat, 17 Feb 2024 23:58:06 +0900 Subject: [PATCH] RM --- README.md | 4 ++-- .../Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs | 10 ++++++++++ src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6538a48d..58d8561e 100644 --- a/README.md +++ b/README.md @@ -1143,7 +1143,7 @@ In addition to the above, the following `ObserveOn`/`SubscribeOn` methods have b * ObserveOnMainThread * SubscribeOnMainThread -When using `AddTo(Component / GameObject)` in Unity, it attaches a special component called ObservableDestroyTrigger, which monitors for destruction. Unity has a characteristic where components that have never been activated do not fire OnDestroy, and the destroyCancellationToken does not get canceled. ObservableDestroyTrigger is designed to monitor for destruction and reliably issue OnDestroy regardless of the active state. It would be wise to use destroyCancellationToken effectively if needed. +When using `AddTo(Component / GameObject)` in Unity, it attaches a special component called ObservableDestroyTrigger if gameObject is not active yet, which monitors for destruction. Unity has a characteristic where components that have never been activated do not fire OnDestroy, and the destroyCancellationToken does not get canceled. ObservableDestroyTrigger is designed to monitor for destruction and reliably issue OnDestroy regardless of the active state. It would be wise to use destroyCancellationToken effectively if needed. ```csharp // simple pattern @@ -1156,7 +1156,7 @@ var d = Disposable.CreateBuilder(); Observable.EveryUpdate().Subscribe().AddTo(ref d); Observable.EveryUpdate().Subscribe().AddTo(ref d); Observable.EveryUpdate().Subscribe().AddTo(ref d); -d.Build().AddTo(destroyCancellationToken or this); // Build and Register +d.RegisterTo(this.destroyCancellationToken); // Build and Register ``` You open tracker window in `Window -> Observable Tracker`. It enables watch `ObservableTracker` list in editor window. diff --git a/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs b/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs index 20622517..e93ed0ed 100644 --- a/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs +++ b/src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs @@ -58,6 +58,16 @@ public static T AddTo(this T disposable, Component gameObjectComponent) return disposable; } +#if UNITY_2022_2_OR_NEWER + if (gameObjectComponent.gameObject.activeInHierarchy && gameObjectComponent is MonoBehaviour mb) + { + // gameObject is Awaked, no need to use ObservableDestroyTrigger + disposable.RegisterTo(mb.destroyCancellationToken); + return disposable; + } +#endif + + // Add ObservableDestroyTrigger return AddTo(disposable, gameObjectComponent.gameObject); } } diff --git a/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs b/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs index d1164b53..851276c6 100644 --- a/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs +++ b/src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs @@ -88,6 +88,15 @@ void Start() //subject.OnNext(10); //subject.OnNext(20); //subject.OnNext(30); + + + var d = Disposable.CreateBuilder(); + Observable.EveryUpdate().Subscribe().AddTo(ref d); + Observable.EveryUpdate().Subscribe().AddTo(ref d); + Observable.EveryUpdate().Subscribe().AddTo(ref d); + + + }