I often find myself in need of a simple 3d quad that displays a texture from a sprite sheet. Unfortunately if you use an actual Unity Sprite you are going run into weird batching and drawcall issues in 3d. On top of that Sprites have very specialized shaders.

first a word of caution

The context here is important. This post describes a script that will take a Material and customize it for a sprite in a sprite sheet. If you do this often you will end up creating many materials and will run into rampant inefficiencies. This script is just a tool to help you out when you are experimenting, not writing production code/assets.

but I just want to use the spritesheet as a pseudo atlas

What I usually wind up doing is creating a quad mesh, creating a new material and adjusting the materials UV scaling and offset so that the sprite is drawn to the quad.

This is very hard to get right without calculating the UV's. And doing this every time is tedious. So let's write a script.

first, how do I use it?

Like any other editor script, download this code and add it to any folder called Editor in your Assets directory. I've namespaced it so that 1. you know where to find it and 2. it doesn't interfere with your own classes.

Once it is in your project just do what the gif demonstrates

  1. Right click on a material in a inspector
  2. Choose Set with sprite
  3. Click the big red button (an unfortunate Unity limitation).
  4. Choose the sprite

voila

The script will have updated the "_MainTex" property for that material to be the texture of the chosen sprite. It will also calculate the Material.mainTextureOffset and Material.mainTextureScale automatically.

limitations

  • It only works on the "_MainTexture" field in the material/shader. The script could be easily updated to change any texture with aditional UI.
  • It probably only works well will with quads that Unit Square aligned UV's ([0,0] to [1,1]).
  • The dumb and ugly red button. If you can remove this, I would love to know how you did it! Fork the gist and tweet me!

twitter cross-post