{"id":15475,"date":"2018-03-20T14:05:00","date_gmt":"2018-03-20T14:05:00","guid":{"rendered":"http:\/\/www.gamasutra.com\/view\/news\/315643"},"modified":"2018-03-20T14:05:00","modified_gmt":"2018-03-20T14:05:00","slug":"blog-working-with-pixel-art-sprites-in-unity","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2018\/03\/20\/blog-working-with-pixel-art-sprites-in-unity\/","title":{"rendered":"Blog: Working with pixel art sprites in Unity"},"content":{"rendered":"<p><strong><em><small>The following blog post, unless otherwise noted, was written by a member of Gamasutra\u2019s community.<br \/>The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.<\/small><\/em><\/strong><\/p>\n<hr \/>\n<p>Welcome to a series of blog posts that will reveal my workflow for importing, animating, and placing 2D sprites in Unity while using the actual Unity editor as little as possible. I\u2019ve taught myself how to navigate Unity\u2019s API by the process of actually making a game, and I figured my long hours of scrambling in the dark could benefit other developers.<\/p>\n<p>Before we can begin animating, we need art to animate with. Welcome to the surprisingly complicated world of simply importing your art assets into Unity<\/p>\n<h2>Part 1. Importing Assets for 2D Pixel Sprite Games<\/h2>\n<p>Unity was not meant for 2D games. Especially not pixel art sprite and tile-based 2D games. The headache of importing a sprite sheet and slicing it up, especially coming from other <a href=\"https:\/\/haxeflixel.com\/documentation\/flxsprite\/\">more straightforward frameworks<\/a>, was incredibly disheartening. Imagine importing a sprite sheet into your project only to discover that you had to manually change half-a-dozen attributes each and every time? Or that, instead of slicing up a sprite sheet by simply defining a couple parameters, you had to manually slice up each frame? Well guess what, you don\u2019t have to imagine it, that\u2019s just how Unity works.<\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/www.sickgamedev.win\/wp-content\/uploads\/2018\/03\/blog-working-with-pixel-art-sprites-in-unity.png\" \/><\/p>\n<p>I desperately needed a solution that would return this process to the completely trivial category that it belongs in. And then I learned that my salvation lay in editor scripts.<\/p>\n<p>Certain editor scripts execute automatically every time a certain Unity function is performed. Importing assets is one of those functions. To make an editor script, you must first simply create a new script in a directory called \u201cEditor\u201d somewhere inside the \u201cAssets\u201d directory of your project. This should be a class that extends AssetPostprocessor. The code listed in the OnPreprocessTexture and OnPostprocessTexture methods will change the behavior of how your assets are imported into your project.<\/p>\n<p>So instead of manually editing all of these properties every time you import a sprite:<\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/www.sickgamedev.win\/wp-content\/uploads\/2018\/03\/blog-working-with-pixel-art-sprites-in-unity-1.png\" \/><\/p>\n<p>You could just edit this in script, once:<\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/www.sickgamedev.win\/wp-content\/uploads\/2018\/03\/blog-working-with-pixel-art-sprites-in-unity-2.png\" \/><\/p>\n<p>Now I don\u2019t have to manually change filterMode to Point on every single sprite I import! Score. (FilterMode.Point keeps your texture art sharp, which is ideal for pixel art. Unity assumes you were importing a large texture to be used in a high-verisimilitude 3D game, in which case, a bilinear\/trilinear filter might be better.) However, these properties will probably change from project to project, so don\u2019t assume the code I\u2019ve posted here is canon.<\/p>\n<p>Next we\u2019ll use the OnPostprocessTexture() method to slice up our script for us by iterating through the texture and storing the slices in an array on the metadata of the asset (you can <a href=\"https:\/\/github.com\/dithyrambs\/SpriteProcessorScript\">see the code on GitHub<\/a> if you want the details). The problem, however, was in deciding how to determine the dimensions of the sprites that needs to be sliced, and which of the imported assets should even be sprites in the first place. I didn\u2019t want to have to repeatedly determine these properties in some editor window pop-up every single time I imported a sprite (that would defeat the entire purpose of this exercise). Eventually I settled on an auto-magical solution. Simply drag your asset into a folder named after an integer somewhere in your project\u2019s \u201cResources\u201d directory (eg. a directory named \u201c16\u201d), and the editor script will assume you want that asset sliced up as a sprite sheet with 16 by 16 pixel sprite dimensions.<\/p>\n<p>I mean just look at this brilliant, sliced sprite:<\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/www.sickgamedev.win\/wp-content\/uploads\/2018\/03\/blog-working-with-pixel-art-sprites-in-unity-3.png\" \/><\/p>\n<p>In general auto-magic solutions make me slightly nervous. I decided to add a little pop-up window telling the user that the sprite was sliced, so the user was aware that something just happened.<\/p>\n<p><img decoding=\"async\" alt=\"image\" src=\"https:\/\/www.sickgamedev.win\/wp-content\/uploads\/2018\/03\/blog-working-with-pixel-art-sprites-in-unity-4.png\" \/><\/p>\n<p>And that\u2019s about it! If you think this script could be useful for your project <a href=\"https:\/\/github.com\/dithyrambs\/SpriteProcessorScript\">feel free to grab it on GitHub<\/a>.<\/p>\n<p>In my next post, I\u2019ll share some of the things I learned defining and playing sprite animations in script rather than via the Unity editor. Until then, be good!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following blog post, unless otherwise noted, was written by a member of Gamasutra\u2019s community.The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company. Welcome to a series of blog posts that will reveal my workflow for importing, animating, and placing 2D sprites in Unity while using the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":15476,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-15475","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/15475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=15475"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/15475\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/15476"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=15475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=15475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=15475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}