TileSelector

Note that the most up-to-date version of this script can be found on my blog: http://cjcurrie.net/blog/2010/10/tileset-selector

Author: CJCurrie

Description
This is a helper class that makes it a lot easier to pick an individual image from a massive tileset, and apply that image to a sprite. An editor script allows you to pick out an image of any rectangular size from a given tileset and apply it as the mainTexture of Renderer. The point of this script is to avoid having to take a large source tileset and chop it up via Photoshop into a hundred smaller, more obnoxious files.



Usage
The TileSet comprises three scripts. TileManagerEditor and TileSelectorEditor are Editor scripts and must be placed in the Editor folder of your project hierarchy. TileManager is the base script that kicks off the magic, and is a component attached to the sprite you want to texture.

Step 1
Create a GameObject (frequently a plane) and attach a material (frequently with a Transparent Cutout shader) to it.

Step 2
Attach this script to the GameObject: Note that this is an empty class.

Step 3
Place these two scripts in the Editor folder:

and

Once the script compiles, set the default "Selection Box" reference image by clicking on the script and dragging the image to the exposed variable. I used a simple one I made in Photoshop: Note that it will be stretched over your selection.

Step 4
Drag your main tileset from your hierarchy to the "Current Tileset" variable exposed in the Tile Manager component of your GameObject from Step 1. Set the "Tile Size" variable exposed directly below it. This is the square root size in pixels of each individual tile. For example, a value of "16" indicates that each tile in your tileset is 16 pixels by 16 pixels (16x16).

Step 5
Click "Open Tile Selector". An EditorWindow will pop up with your tileset. Use the Zoom buttons to get closer or further from your tileset. Click and drag to select the area you want on your sprite. Click "Set Sprite" to apply the cropped image. You're done!

Step N
To skip all the setup, just duplicate the sprite you made above, or work from a prefab, and the process becomes much faster.