Skip to content

Commit

Permalink
RM
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 17, 2024
1 parent 932b4c5 commit bc3301a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public static T AddTo<T>(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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);



}


Expand Down

0 comments on commit bc3301a

Please sign in to comment.