{"id":75,"date":"2021-10-24T07:49:38","date_gmt":"2021-10-24T07:49:38","guid":{"rendered":"https:\/\/docs.sisus.co\/inity\/?p=75"},"modified":"2025-12-28T14:01:12","modified_gmt":"2025-12-28T14:01:12","slug":"wrapper","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/wrappers\/wrapper\/","title":{"rendered":"1. Wrappers"},"content":{"rendered":"<p>Wrappers can be used to attach plain old C# objects to a GameObjects, have them receive callbacks during Unity events like Update and OnDestroy, and to start coroutines.<\/p>\n<h2>Creating a Wrapper<\/h2>\n<p>Let\u2019s say you had a plain old C# class called Player:<\/p>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">using<\/span> System<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">using<\/span> UnityEngine<span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\"><span style=\"color: #808030;\">[<\/span>Serializable<span style=\"color: #808030;\">]<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">class<\/span> <span style=\"color: #005fd2;\">Player<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #808030;\">[<\/span>SerializeField<span style=\"color: #808030;\">]<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">private<\/span> Id id<span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">    <span style=\"color: #808030;\">[<\/span>SerializeField<span style=\"color: #808030;\">,<\/span> Range<span style=\"color: #808030;\">(<\/span>0f<span style=\"color: #808030;\">,<\/span> 10f<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">]<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">private<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span> speed<span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #005fd2;\">Player<\/span><span style=\"color: #808030;\">(<\/span>Id id<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span> speed<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">this<\/span><span style=\"color: #808030;\">.<\/span>id <span style=\"color: #808030;\">=<\/span> id<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">this<\/span><span style=\"color: #808030;\">.<\/span>speed <span style=\"color: #808030;\">=<\/span> speed<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">}<\/span><\/span><\/pre>\n<p>The easiest way to create a Wrapper component for the class is to select the script, open its context menu in the Inspector and select <strong>Generate Wrapper<\/strong>.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-341\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/generate-wrapper.png\" alt=\"\" width=\"523\" height=\"148\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/generate-wrapper.png 523w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/generate-wrapper-300x85.png 300w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/p>\n<p>Or you could also create it manually by defining a class that inherits from Wrapper&lt;Player&gt;.<\/p>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800080;\"><span style=\"color: #808030;\">[<\/span><\/span>AddComponentMenu<span style=\"color: #808030;\">(<\/span>\"Wrapper\/Player\"<span style=\"color: #808030;\">)]<\/span>\r\n<span style=\"color: #800000; font-weight: bold;\">class<\/span> PlayerComponent <span style=\"color: #800080;\">:<\/span> Wrapper<span style=\"color: #808030;\">&lt;<\/span>Player<span style=\"color: #808030;\">&gt; { }<\/span><\/span><\/pre>\n<p>The AddComponentMenu attribute is optional; it just makes the PlayerComponent appear as simply &#8220;Player&#8221; in the Inspector and the Add Component menu.<\/p>\n<p>If the wrapped class has the [Serializable] attribute, then an instance of it will be automatically created for any wrappers that exist as part of scene or prefab assets, and the objects serializable fields will be visible in the Inspector.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-372\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/Player.png\" alt=\"\" width=\"402\" height=\"114\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/Player.png 402w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/Player-300x85.png 300w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/p>\n<h2>Initializing a Wrapper<\/h2>\n<h3>[Serializable] Attribute<\/h3>\n<p>As stated before, if the wrapped class has the [Serializable] attribute, then an instance will be automatically created for all wrappers that exist as part of scene or prefab assets.<\/p>\n<h3>Provide Instance Via Constructor<\/h3>\n<p>If the wrapped object is not serializable, and doesn&#8217;t depend any external Object references or <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/services\/\">services<\/a>, then you can initialize the instance right in the wrapper&#8217;s parameterless constructor, and pass it to the base constructor which accepts the instance as an argument.<\/p>\n<pre class=\"code_syntax\"><span class=\"line_wrapper\">[AddComponentMenu(\"Wrapper\/Player\")]<\/span>\r\n<span class=\"line_wrapper\">sealed class PlayerComponent : Wrapper&lt;Player&gt;<\/span>\r\n<span class=\"line_wrapper\">{<\/span>\r\n<span class=\"line_wrapper\">    public PlayerComponent() : base(new Player(Id.NewId(), 0f)) { }<\/span>\r\n<span class=\"line_wrapper\">}<\/span><\/pre>\n<h3>Pass Instance During Instantiation<\/h3>\n<p>As wrappers implement <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;TWrapped&gt;<\/a>, it means that you can also pass an instance to the component when <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/monobehaviour-t\/\">creating an instance using Instantiate or AddComponent<\/a>.<\/p>\n<p>For example, an instance of Player could be attached to a game object like this:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\">Player player <span style=\"color: #808030;\">=<\/span> <span style=\"color: #800000;\"><b>new<\/b><\/span> Player<span style=\"color: #808030;\">(<\/span>id<span style=\"color: #808030;\">,<\/span> 0f<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span style=\"color: #000000;\">gameObject<span style=\"color: #808030;\">.<\/span>AddComponent<span style=\"color: #808030;\">&lt;<\/span>PlayerComponent<span style=\"color: #808030;\">,<\/span> Player<span style=\"color: #808030;\">&gt;(<\/span>player<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span><\/pre>\n<h3>Wrapper Initializer<\/h3>\n<p>If the wrapped object depends on <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/services\/\">services<\/a>, or you want to assign some arguments using the Inspector, you can generate an <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/initializer\/\">Initializer<\/a> for the wrapper component.<\/p>\n<p>The initializer should derive from WrapperInitializer&lt;T&#8230;&gt;, with its generic arguments of being the types of the wrapper component, the wrapped object, and all the objects the wrapped object needs to be provided to it.<\/p>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">class<\/span> <span style=\"color: #005fd2;\">PlayerInitializer<\/span> <span style=\"color: #808030;\">:<\/span> WrapperInitializer<span style=\"color: #808030;\">&lt;<\/span>PlayerComponent<span style=\"color: #808030;\">,<\/span> Player<span style=\"color: #808030;\">,<\/span> Id<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span><span style=\"color: #808030;\">&gt;<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">private<\/span> <span style=\"color: #800000; font-weight: bold;\">class<\/span> <span style=\"color: #005fd2;\">Init<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">public<\/span> Id Id<span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">        <span style=\"color: #808030;\">[<\/span>Range<span style=\"color: #808030;\">(<\/span>0f<span style=\"color: #808030;\">,<\/span> 10f<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">]<\/span> <span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span> Speed<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">}<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">protected<\/span> <span style=\"color: #800000; font-weight: bold;\">override<\/span> Player <span style=\"color: #005fd2;\">CreateWrappedObject<\/span><span style=\"color: #808030;\">(<\/span>Id id<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span> speed<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        Debug<span style=\"color: #808030;\">.<\/span>Assert<span style=\"color: #808030;\">(<\/span>id <span style=\"color: #808030;\">!<\/span><span style=\"color: #808030;\">=<\/span> Id<span style=\"color: #808030;\">.<\/span>Empty<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">        Debug<span style=\"color: #808030;\">.<\/span>Assert<span style=\"color: #808030;\">(<\/span>speed <span style=\"color: #808030;\">&gt;<\/span> 0f<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">return<\/span> <span style=\"color: #800000; font-weight: bold;\">new<\/span> Player<span style=\"color: #808030;\">(<\/span>id<span style=\"color: #808030;\">,<\/span> speed<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">}<\/span><\/span><\/pre>\n<div>You can optionally also define a nested class named &#8220;<em>Init<\/em>&#8221; inside the Initializer\u00a0 class, containing fields whose types match those of the arguments that the client accepts. If you do this, you can then attach property attributes to its fields, and their property drawers will be used when drawing the matching Init arguments in the Inspector.<\/div>\n<div><\/div>\n<div>When you use a Wrapper Initializer, it will take care of serializing all the arguments that will be passed to the client. This means that the client class itself does not necessarily need to be serializable.<\/div>\n<div>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">class<\/span> <span style=\"color: #005fd2;\">Player<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">public<\/span> Id Id <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000; font-weight: bold;\">get<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span> Speed <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000; font-weight: bold;\">get<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800080;\">}<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">    <span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #005fd2;\">Player<\/span><span style=\"color: #808030;\">(<\/span>Id id<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000; font-weight: bold;\">float<\/span> speed<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        Id <span style=\"color: #808030;\">=<\/span> id<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">        Speed <span style=\"color: #808030;\">=<\/span> speed<span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">    <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">}<\/span><\/span><\/pre>\n<\/div>\n<h2><img loading=\"lazy\" class=\"alignnone size-full wp-image-371\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/PlayerInitializer.png\" alt=\"\" width=\"504\" height=\"105\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/PlayerInitializer.png 504w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/PlayerInitializer-300x63.png 300w\" sizes=\"(max-width: 504px) 100vw, 504px\" \/><\/h2>\n<h2 class=\"western\">Unity Events<\/h2>\n<p>Wrapped objects can receive callbacks during select Unity events from their wrapper by implementing one of the following interfaces:<\/p>\n<ol>\n<li><b>IAwake <\/b>&#8211; Receive callback during the MonoBehaviour.Awake event.<\/li>\n<li><b>I<\/b><b>O<\/b><b>nEnable<\/b> &#8211; Receive callback during the MonoBehaviour.OnEnable event.<\/li>\n<li><b>I<\/b><b>S<\/b><b>tart<\/b> &#8211; Receive callback during the MonoBehaviour.Start event.<\/li>\n<li><b>I<\/b><b>U<\/b><b>pdate<\/b> &#8211; Receive callback during the MonoBehaviour.Update event.<\/li>\n<li><b>I<\/b><b>F<\/b><b>ixedUpdate<\/b> &#8211; Receive callback during the MonoBehaviour.FixedUpdate event.<\/li>\n<li><b>I<\/b><b>L<\/b><b>ateUpdate<\/b> &#8211; Receive callback during the MonoBehaviour.LateUpdate event.<\/li>\n<li><b>I<\/b><b>O<\/b><b>nDisable<\/b> &#8211; Receive callback during the MonoBehaviour.OnDisable event.<\/li>\n<li><b>IOnDestroy<\/b> &#8211; Receive callback during the MonoBehaviour.OnDestroy event.<\/li>\n<\/ol>\n<p>For example, to receive callbacks during the Update event the Player class would need to be modified like this:<\/p>\n<pre class=\"western\"><span style=\"color: #000000;\"><span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>class<\/b><\/span> Player <span style=\"color: #808030;\">:<\/span> IUpdate<\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #000000;\">        <span style=\"color: #800000;\"><b>public<\/b><\/span> <span style=\"color: #800000;\"><b>void<\/b><\/span> Update<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\"><b>float<\/b><\/span> deltaTime<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span style=\"color: #000000;\">        <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span style=\"color: #000000;\">                <span style=\"color: #696969;\">\/\/ Do something every frame<\/span><\/span>\r\n<span style=\"color: #000000;\">        <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n<h2 class=\"western\">Coroutines<\/h2>\n<p>Wrapped objects can also start and stop coroutines in their wrapper.<\/p>\n<p>To gain this ability the wrapped object has to implement the ICoroutines interface.<\/p>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">class<\/span> <span style=\"color: #005fd2;\">Player<\/span> <span style=\"color: #808030;\">:<\/span> ICoroutines<\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">public<\/span> ICoroutineRunner CoroutineRunner <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000; font-weight: bold;\">get<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800000; font-weight: bold;\">set<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">}<\/span><\/span><\/pre>\n<p>The wrapper component gets automatically assigned to the CoroutineRunner property during its initialization phase.<\/p>\n<p>You can start a coroutine from the wrapped object using CoroutineRunner.StartCoroutine.<\/p>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">class<\/span> <span style=\"color: #005fd2;\">Player<\/span> <span style=\"color: #808030;\">:<\/span> ICoroutines<\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">public<\/span> ICoroutineRunner CoroutineRunner <span style=\"color: #800080;\">{<\/span> <span style=\"color: #800000; font-weight: bold;\">get<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800000; font-weight: bold;\">set<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800080;\">}<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">        <span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">void<\/span> <span style=\"color: #005fd2;\">SayDelayed<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">string<\/span> message<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">                CoroutineRunner<span style=\"color: #808030;\">.<\/span>StartCoroutine<span style=\"color: #808030;\">(<\/span>SayDelayedCoroutine<span style=\"color: #808030;\">(<\/span>message<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800080;\">}<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">        IEnumerator <span style=\"color: #005fd2;\">SayDelayedCoroutine<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">string<\/span> message<span style=\"color: #808030;\">)<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">                <span style=\"color: #800000; font-weight: bold;\">yield<\/span> <span style=\"color: #800000; font-weight: bold;\">return<\/span> <span style=\"color: #800000; font-weight: bold;\">new<\/span> WaitForSeconds<span style=\"color: #808030;\">(<\/span>1f<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n\r\n<span class=\"line_wrapper\">                Debug<span style=\"color: #808030;\">.<\/span>Log<span style=\"color: #808030;\">(<\/span>message<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\">        <span style=\"color: #800080;\">}<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">}<\/span><\/span><\/pre>\n<p>The started coroutine is tied to the lifetime of the GameObject just like it would be if you started the coroutine directly within a MonoBehaviour.<\/p>\n<p>You can stop a coroutine that is running on the wrapper using CoroutineRunner.StopCoroutine or stop all coroutines that are running on it using CoroutineRunner.StopAllCoroutines.<\/p>\n<pre class=\"code_syntax\" style=\"color: #000000; background: #ffffff;\"><span class=\"line_wrapper\"><span style=\"color: #800000; font-weight: bold;\">public<\/span> <span style=\"color: #800000; font-weight: bold;\">void<\/span> <span style=\"color: #005fd2;\">OnDisable<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">)<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">{<\/span><\/span>\r\n<span class=\"line_wrapper\">        CoroutineRunner<span style=\"color: #808030;\">.<\/span>StopAllCoroutines<span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/span>\r\n<span class=\"line_wrapper\"><span style=\"color: #800080;\">}<\/span><\/span><\/pre>\n<h2 class=\"western\">Wrapped Objects vs MonoBehaviour&lt;T&#8230;&gt;<\/h2>\n<p>Wrapping plain old C# objects using wrapper components is an alternative to deriving from the <code>MonoBehaviour&lt;T...&gt;<\/code> base classes. Both approaches make it easy to receive objects they depend on from the outside, and make it easy to create unit tests for the objects. You&#8217;ll probably mostly want to use one or the other in your project consistently, rather than mixing and matching them at random.<\/p>\n<h3 class=\"western\">Wrapped Object Pros<\/h3>\n<p>Unit testing wrapped objects can be even simpler and efficient, because you usually don&#8217;t even need to initialize any GameObjects or components when testing them, and can instead just test the plain old C# objects directly.<\/p>\n<p>Your object won&#8217;t have any private unity event methods, that can&#8217;t be executed during Edit Mode unit tests without resorting to reflection. You can always easily execute any of their their lifetime events via the interfaces that they implement.<\/p>\n<p>Additionally the pattern that wrapped objects use to handle coroutines makes it easy to swap the coroutine runner class during unit tests, making it possible to even unit tests coroutines, for example with the help of the EditorCoroutineRunner class.<\/p>\n<p>An additional nice little benefit with using wrapped objects is that you can assign dependencies to read-only fields and properties in the constructor, which makes it possible to make your classes immutable. Having your wrapped objects be stateless can make your code less error-prone and makes it completely thread safe as well.<\/p>\n<p>If you care about keeping your code as decoupled from the Unity framework as possible, so that it could be easier to port it to other platforms, then wrapped objects could also be able to help you with this.<\/p>\n<h3 class=\"western\">Wrapped Object Cons<\/h3>\n<p>Using wrapped objects instead of MonoBehaviour&lt;T&#8230;&gt; can introduce some additional boilerplate and complexity. In addition to creating your plain old C# object, you&#8217;ll always also need to generate a wrapper for it, resulting in one additional class (albeit a really simple one).<\/p>\n<p>There&#8217;s also more of a learning cure compared to just using MonoBehaviour&lt;T&#8230;&gt;, because you&#8217;ll need to learn to use interfaces like IUpdate and <span class=\"line_wrapper\">ICoroutines when you need to make use of functionality that is already built into components.<\/span><\/p>\n<p>Also, because constructor injection by its very nature doesn&#8217;t support circular references (A depends on B, and B depends on A), you can encounter runtime exceptions if you ever try to make two wrapped objects receive references to each other during their initialization.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wrappers can be used to attach plain old C# objects to a GameObjects, have them receive callbacks during Unity events like Update and OnDestroy, and to start coroutines. Creating a Wrapper Let\u2019s say you had a plain old C# class called Player: using System; using UnityEngine; [Serializable] public class Player { [SerializeField] private Id id; ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/wrappers\/wrapper\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/75"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":26,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":1089,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/75\/revisions\/1089"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}