CoroutineSystem
The CoroutineSystem provides a single Unity coroutine host that lets non-MonoBehaviour code run coroutines. It is implemented by the CoroutineHost class in the SaffronLibrary.Prime.Systems.Coroutine namespace.
Purpose in SaffronLibrary
Unity only runs coroutines on live MonoBehaviour instances. Architectural services such as flow controllers, signal-driven systems, and coordinators are intended to stay plain C# classes that are not tied to scene lifecycles. CoroutineHost acts as the Unity-side adapter for these services. It gives them a stable coroutine execution context without requiring them to inherit from MonoBehaviour.
Key responsibilities
- Provide a coroutine execution context.
CoroutineHostis aMonoBehaviourthat exposes aRunmethod. The method starts the suppliedIEnumeratoras a Unity coroutine. - Provide a single shared instance. The static
Instanceproperty returns oneCoroutineHostfor the application session. It creates the host on first access, so no scene setup is required. - Persist across scene loads. The host GameObject is marked
DontDestroyOnLoad, so it is not removed when scenes change.
Who should use it
- Developers writing infrastructure, flow, or service code that needs to run coroutines without being a
MonoBehaviour.
The XML documentation states that CoroutineHost is not gameplay-facing. Gameplay entities should not call it, and it should not replace proper Update logic or be used to bypass flow or signal control.
Dependencies
This system doesn't depend on any other Saffron Library system.