{"id":78,"date":"2021-10-24T07:53:00","date_gmt":"2021-10-24T07:53:00","guid":{"rendered":"https:\/\/docs.sisus.co\/inity\/?p=78"},"modified":"2025-07-14T19:37:08","modified_gmt":"2025-07-14T19:37:08","slug":"scriptablewrapper","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/wrappers\/scriptablewrapper\/","title":{"rendered":"2. ScriptableWrapper"},"content":{"rendered":"<p>The ScriptableWrapper class is a ScriptableObject that can act as a simple wrapper for a plain old class object.<\/p>\n<p>It makes it easy to take a plain old class object and serialize it as an asset in the project.<\/p>\n<p>Let\u2019s say you had a plain old class called Settings:<\/p>\n<pre class=\"western\">public class Settings\r\n{\r\n    public float MoveSpeed = 10f;\r\n    public float JumpHeight = 4f;\r\n}\r\n<\/pre>\n<p>To create a scriptable wrapper for the Settings class, create a class that inherits from ScriptableWrapper&lt;Settings&gt;.<\/p>\n<pre class=\"western\">public class SettingsAsset : ScriptableWrapper&lt;Settings&gt; { }<\/pre>\n<p>Beyond being able to wrap a plain old class object, this class functions just like a normal ScriptableObject, so you can decorate it with the <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/CreateAssetMenuAttribute.html\">CreateAssetMenu attribute<\/a> to make it easy to create an instance of the asset from Unity&#8217;s create menu.<\/p>\n<pre class=\"western\">[CreateAssetMenu]\r\npublic class SettingsAsset : ScriptableWrapper&lt;Settings&gt; { }<\/pre>\n<p>You can access the plain old class wrapped by a ScriptableWrapper through the WrappedObject property.<\/p>\n<pre class=\"western\">var settingsAsset = Resources.Load&lt;SettingsAsset&gt;(\"Settings\");\r\nSettings settings = settingsAsset.WrappedObject;<\/pre>\n<p>This scriptable wrapper implements IInitializable&lt;Settings&gt;, which means that a new instance can be initialized with the Settings object passed as an argument using the custom Instantiate or Create.Instance methods that come with Init(args).<\/p>\n<p>You can create create a new instance of the SettingsAsset with a Settings object wrapped inside of it using Create.Instance:<\/p>\n<pre class=\"western\">public void SaveSettings(Settings settings)\r\n{\r\n    var settingsAsset = Create.Instance&lt;SettingsAsset&gt;(settings);\r\n    AssetDatabase.CreateAsset(settingsAsset, \"Resources\/Settings.asset\");\r\n}<\/pre>\n<p>You can clone an existing Settings asset while injecting a new Settings object inside of it using settingsAsset.Instantiate(settings);<\/p>\n<pre class=\"western\">public SettingsAsset InstantiateSettingsAsset(Settings overrideSettings = null)\r\n{\r\n    SettingsAsset settingsAsset = Resources.Load&lt;SettingsAsset&gt;(\"Settings\");\r\n\r\n    if(overrideSettings != null)\r\n    {\r\n        return settingsAsset.Instantiate(overrideSettings);\r\n    }\r\n\r\n    return Object.Instantiate(settingsAsset);\r\n}<\/pre>\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><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>IAwake <\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">&#8211; Receive callback during the <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">ScriptableObject<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">.<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">Awake event.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>I<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>O<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>nEnable<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> &#8211; <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">Receive callback during the <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">ScriptableObject<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">.OnEnable<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> event.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>I<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>U<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>pdate<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> &#8211;<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> Receive callback during the Update event.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>I<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>F<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>ixedUpdate<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> &#8211; <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">Receive callback during the FixedUpdate<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> event.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>I<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>L<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>ateUpdate<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> &#8211; <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">Receive callback during the LateUpdate<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> event.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>I<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>O<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>nDisable<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> &#8211; <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">Receive callback during the <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">ScriptableObject<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">.OnDisable<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> event.<\/span><\/span><\/span><\/li>\n<li><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"><b>IOnDestroy<\/b><\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> &#8211; <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">Receive callback during the <\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">ScriptableObject<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\">.OnDestroy<\/span><\/span><\/span><span style=\"color: #000000;\"><span style=\"font-family: Liberation Serif, serif;\"><span style=\"font-size: medium;\"> event.<\/span><\/span><\/span><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>The ScriptableWrapper class is a ScriptableObject that can act as a simple wrapper for a plain old class object. It makes it easy to take a plain old class object and serialize it as an asset in the project. Let\u2019s say you had a plain old class called Settings: public class Settings { public float ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/wrappers\/scriptablewrapper\/\" 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\/78"}],"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=78"}],"version-history":[{"count":8,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":613,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/78\/revisions\/613"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}