{"id":580,"date":"2017-01-16T14:12:44","date_gmt":"2017-01-16T13:12:44","guid":{"rendered":"http:\/\/davikingcode.com\/blog\/?p=580"},"modified":"2017-01-16T14:23:10","modified_gmt":"2017-01-16T13:23:10","slug":"unity-sprite-packing","status":"publish","type":"post","link":"https:\/\/davikingcode.com\/blog\/unity-sprite-packing\/","title":{"rendered":"Unity &#8211; sprite packing"},"content":{"rendered":"<p>Best practices with Unity have always been an hidden gem. Most precisely concerning the <em>Resources<\/em> folder. Hopefully things are changing with Unity&#8217;s <a href=\"https:\/\/unity3d.com\/fr\/learn\/tutorials\/topics\/best-practices\" target=\"_blank\">best practices guide<\/a>! And things are simple concerning the Resources folder: just <a target=\"_blank\" href=\"https:\/\/unity3d.com\/fr\/learn\/tutorials\/topics\/best-practices\/resources-folder\">don&#8217;t use it<\/a>!<\/p>\n<p>Managing 2D Sprites in Unity is simple if you have a static scene: just put every sprite needed on your screen and you&#8217;re almost done. Obviously you should package them in Spritesheets. The easy way to do it is via Unity&#8217; <a href=\"https:\/\/docs.unity3d.com\/Manual\/SpritePacker.html\" target=\"_blank\">Sprite Packer<\/a> tool. But what to do if you need to load Sprites at runtime? How to access a Sprite if it&#8217;s not linked directly on a GameObject or a Prefab and without using the Resources folder?<\/p>\n<p>Create a <a href=\"https:\/\/docs.unity3d.com\/Manual\/class-ScriptableObject.html\" target=\"_blank\">ScriptableObject<\/a>! They are the best way to store informations within Unity. Here is an Editor Script for generating ScriptableObjects with a list of Sprites for each <em>Packing Tag<\/em> mentioned:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing UnityEngine;\r\n#if UNITY_EDITOR\r\nusing UnityEditor;\r\n#endif\r\nusing System;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\n\r\n[Serializable]\r\npublic class SpritesData:ScriptableObject {\r\n\r\n\t[SerializeField]\r\n\tpublic Sprite[] sprites;\r\n\r\n\tstatic public string assetDirPath = &quot;RuntimeSprites\/SpritesData\/&quot;;\r\n\tstatic public string assetPath = &quot;Assets\/RuntimeSprites\/SpritesData\/{0}.asset&quot;;\r\n\r\n\t#if UNITY_EDITOR\r\n\t[MenuItem(&quot;Data\/Create Sprites Data&quot;)]\r\n\tstatic void CreateSpritesData() {\r\n\t\tstring dirPath = Application.dataPath + &quot;\/&quot; +  assetDirPath;\r\n\r\n\t\tif (!System.IO.Directory.Exists(dirPath))\r\n\t\t\tSystem.IO.Directory.CreateDirectory(@dirPath);\r\n\r\n\t\tSpritesData data = ScriptableObject.CreateInstance&lt;SpritesData&gt;();\r\n\r\n\t\tList&lt;Sprite&gt; spritesList = new List&lt;Sprite&gt;();\r\n\r\n\t\tstring[] anims = AssetDatabase.FindAssets(&quot;t:Sprite&quot;, new string[] {&quot;Assets\/Sprites&quot;});\r\n\r\n\t\tstring currentTag = &quot;&quot;;\r\n\r\n\t\tforeach (string anim in anims) {\r\n\r\n\t\t\tstring path = AssetDatabase.GUIDToAssetPath(anim);\r\n\r\n\t\t\tTextureImporter ti = TextureImporter.GetAtPath(path) as TextureImporter;\r\n\r\n\t\t\tif (ti.spritePackingTag != currentTag) {\r\n\r\n\t\t\t\tif (data &amp;&amp; currentTag != &quot;&quot;) {\r\n\r\n\t\t\t\t\tdata.sprites = spritesList.ToArray();\r\n\t\t\t\t\tAssetDatabase.CreateAsset(data, string.Format(assetPath, currentTag));\r\n\t\t\t\t}\r\n\r\n\t\t\t\tdata = ScriptableObject.CreateInstance&lt;SpritesData&gt;();\r\n\t\t\t\tspritesList = new List&lt;Sprite&gt;();\r\n\r\n\t\t\t\tcurrentTag = ti.spritePackingTag;\r\n\t\t\t}\r\n\r\n\t\t\tSprite sprite = AssetDatabase.LoadAssetAtPath&lt;Sprite&gt;(path);\r\n\r\n\t\t\tspritesList.Add(sprite);\r\n\t\t}\r\n\r\n\t\tdata.sprites = spritesList.ToArray();\r\n\t\tAssetDatabase.CreateAsset(data, string.Format(assetPath, currentTag));\r\n\t}\r\n\t#endif\r\n}\r\n<\/pre>\n<p>Now that you have all your SpritesData you just have to create a <em>MonoBehaviour<\/em> with a <em>public SpritesData[] spritesDatas;<\/em> property, then you will have access to all your sprites! The good things is while a sprite isn&#8217;t displayed on Screen it&#8217;s not in memory.<\/p>\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Best practices with Unity have always been an hidden gem. Most precisely concerning the Resources folder. Hopefully things are changing with Unity&#8217;s best practices guide! And things are simple concerning the Resources folder: just don&#8217;t use it! Managing 2D Sprites in Unity is simple if you have a static scene: just put every sprite needed &hellip; <a href=\"https:\/\/davikingcode.com\/blog\/unity-sprite-packing\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unity &#8211; sprite packing<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_discordance_state":"","_discordance_checked":true},"categories":[15],"tags":[5],"_links":{"self":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/580"}],"collection":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/comments?post=580"}],"version-history":[{"count":8,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/580\/revisions"}],"predecessor-version":[{"id":598,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/posts\/580\/revisions\/598"}],"wp:attachment":[{"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/media?parent=580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/categories?post=580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davikingcode.com\/blog\/wp-json\/wp\/v2\/tags?post=580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}