{"id":562,"date":"2016-10-23T20:17:45","date_gmt":"2016-10-23T20:17:45","guid":{"rendered":"http:\/\/magiteker.com\/?p=562"},"modified":"2016-12-11T22:21:08","modified_gmt":"2016-12-11T22:21:08","slug":"unity-design-patterns","status":"publish","type":"post","link":"https:\/\/magiteker.com\/index.php\/2016\/10\/23\/unity-design-patterns\/","title":{"rendered":"Unity Design Patterns: Scriptable Objects"},"content":{"rendered":"<p style=\"text-align: justify;\">An interesting use of Scriptable Objects within Unity as demonstrated at Unite 2016.<\/p>\n<p style=\"text-align: center;\"><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/VBA1QCoEAX4\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\">video<\/iframe><\/p>\n<h2 class=\"post-title\" style=\"text-align: center;\">Data Scriptable Object<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing UnityEngine;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\n\r\n&#x5B;CreateAssetMenu]\r\npublic class DataScriptable : ScriptableObject {\r\n\r\n    public static string SavedSettingsPath\r\n    {\r\n        get\r\n        {\r\n            return System.IO.Path.Combine(Application.persistentDataPath, &quot;settings.json&quot;);\r\n        }\r\n    }\r\n\r\n    private static DataScriptable _instance;\r\n    public static DataScriptable Instance\r\n    {\r\n        get\r\n        {\r\n            if (!_instance)\r\n                _instance = Resources.FindObjectsOfTypeAll&lt;DataScriptable&gt;().FirstOrDefault();\r\n#if UNITY_EDITOR\r\n            if (!_instance)\r\n                InitializeFromDefault(UnityEditor.AssetDatabase.LoadAssetAtPath&lt;DataScriptable&gt;(&quot;Assets\/Test game settings.asset&quot;));\r\n#endif\r\n            return _instance;\r\n        }\r\n    }\r\n\r\n    \/\/\/\r\n\r\n\r\n&lt;summary&gt;\r\n    \/\/\/ Retrieves data from JSON format.\r\n    \/\/\/ &lt;\/summary&gt;\r\n\r\n\r\n\r\n    \/\/\/ &lt;param name=&quot;path&quot;&gt;&lt;\/param&gt;\r\n    public static void LoadFromJSON(string path)\r\n    {\r\n        if (!_instance)\r\n            DestroyImmediate(_instance);\r\n        JsonUtility.FromJsonOverwrite(System.IO.File.ReadAllText(path), _instance);\r\n        _instance.hideFlags = HideFlags.HideAndDontSave;\r\n    }\r\n\r\n    \/\/\/\r\n\r\n\r\n&lt;summary&gt;\r\n    \/\/\/ Object instance constructor.\r\n    \/\/\/ &lt;\/summary&gt;\r\n\r\n\r\n\r\n    \/\/\/ &lt;param name=&quot;settings&quot;&gt;&lt;\/param&gt;\r\n    public static void InitializeFromDefault(DataScriptable settings)\r\n    {\r\n        if (_instance) DestroyImmediate(_instance);\r\n        _instance = Instantiate(settings);\r\n        _instance.hideFlags = HideFlags.HideAndDontSave;\r\n    }\r\n\r\n    \/\/\/\r\n\r\n\r\n&lt;summary&gt;\r\n    \/\/\/ Store data to disk in JSON.\r\n    \/\/\/ &lt;\/summary&gt;\r\n\r\n\r\n\r\n    \/\/\/ &lt;param name=&quot;path&quot;&gt;&lt;\/param&gt;\r\n    public void SaveToJSON(string path)\r\n    {\r\n        Debug.LogFormat(&quot;Saving game settings to {0}&quot;, path);\r\n        System.IO.File.WriteAllText(path, JsonUtility.ToJson(this, true));\r\n    }\r\n\r\n    public static bool FileExists()\r\n    {\r\n        return System.IO.File.Exists(SavedSettingsPath);\r\n    }\r\n}\r\n\r\n<\/pre>\n<h2 class=\"post-title\" style=\"text-align: center;\">Data Scriptable Manager<\/h2>\n<p>&nbsp;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class DataScriptableManager : MonoBehaviour {\r\n\r\n    public DataScriptable dataTemplate;\r\n\r\n    \/\/ Use this for initialization\r\n\tvoid Start () {\r\n\r\n        if (DataScriptable.FileExists())\r\n            DataScriptable.LoadFromJSON(DataScriptable.SavedSettingsPath);\r\n        else\r\n            DataScriptable.InitializeFromDefault(dataTemplate);\r\n\t}\r\n\t\r\n    public void Play()\r\n    {\r\n        DataScriptable.Instance.SaveToJSON(DataScriptable.SavedSettingsPath);\r\n        InstanceScriptable.CreateFromSettings(DataScriptable.Instance);\r\n\r\n    }\r\n}\r\n\r\n\r\n<\/pre>\n<hr \/>\n<h2 class=\"post-title\" style=\"text-align: center;\">Instanced Scriptable Object<\/h2>\n<p>&nbsp;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Runtime.Remoting.Messaging;\r\nusing UnityEngine;\r\nusing UnityEngine.Assertions;\r\n\r\n&#x5B;CreateAssetMenu]\r\npublic class InstanceScriptable : ScriptableObject {\r\n\r\n    public static InstanceScriptable _instance;\r\n    public static InstanceScriptable Instance\r\n    {\r\n        get\r\n        {\r\n            if (!_instance)\r\n                _instance = Resources.FindObjectsOfTypeAll&lt;InstanceScriptable&gt;().FirstOrDefault();\r\n\r\n#if UNITY_EDITOR\r\n            if (!_instance)\r\n                CreateFromSettings(DataScriptable.Instance);\r\n#endif\r\n\r\n            return _instance;\r\n        }\r\n    }\r\n\r\n\r\n    \/\/\/\r\n\r\n\r\n&lt;summary&gt;\r\n    \/\/\/ Object instance constructor.\r\n    \/\/\/ &lt;\/summary&gt;\r\n\r\n\r\n\r\n    \/\/\/ &lt;param name=&quot;settings&quot;&gt;&lt;\/param&gt;\r\n    public static void CreateFromSettings(DataScriptable settings)\r\n    {\r\n        Assert.IsNotNull(settings);\r\n\r\n        _instance = CreateInstance&lt;InstanceScriptable&gt;();\r\n        _instance.hideFlags = HideFlags.HideAndDontSave;\r\n\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An interesting use of Scriptable Objects within Unity as demonstrated at Unite 2016. video Data Scriptable Object using System; using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; &#x5B;CreateAssetMenu] public class DataScriptable : ScriptableObject { public static string SavedSettingsPath { get { return System.IO.Path.Combine(Application.persistentDataPath, &quot;settings.json&quot;); } } private static DataScriptable _instance; public static DataScriptable Instance { [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":570,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[21,3,4,5],"tags":[],"class_list":["post-562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-design_patterns","category-tools","category-tutorials"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/magiteker.com\/wp-content\/uploads\/2016\/10\/Unity_Pattern_Logo2.png?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6MIgq-94","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/posts\/562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/comments?post=562"}],"version-history":[{"count":20,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":738,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/posts\/562\/revisions\/738"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/media\/570"}],"wp:attachment":[{"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/media?parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/categories?post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magiteker.com\/index.php\/wp-json\/wp\/v2\/tags?post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}