- Home /
Converting single sprite into Multiple
Hi guys, currently I have a single map that I want to split into 16x16 pixels and automatically put it (the entire thing) in the scene. I've been doing it manually up until now by splicing it and using a tile editor to place them together, but I'm really dying for an automatic solution. Is there a way to do this without spending money? I still want the 16x16 tiles to be considered separate game objects. Thanks
Thank you for the response. The part I'm having trouble with is the bulk drag'n'drop of many sprites at once onto the scene, while keeping them as separate game objects. When I try to drag more than one sprite onto the scene, it only brings the first sprite selected. I'm just wondering if there's a software that will directly import the map picture I have into the scene while keeping it into separate 16x16 game objects.
Answer by Immanuel-Scholz · Nov 28, 2014 at 07:42 AM
What part makes you the trouble? The splitting of images should be quite fast if you use the auto-split function of unity (clickme and scroll down until "Automatic slicing"). 20 images should take around 5-10 minutes max..
For arranging the images into the scene, you know about the grid snapping, right? (Edit/Snap settings will edit the grid size and dragging while holding down Ctrl/Command key will snap while when you move). That makes it quite easy to manually place objects in a fixed grid. Remember that you can first place all the tiles in a grid anywhere and then move/rotate them together if they are not spot-on as a group.
Remember that many functions like changing properties (like the single/multiple sprites setting) can be executed on multi-selections. You can also bulk-drag'n'drop many sprites in one go onto the scene (and then move them around row-by-row).
So I've figured out that it's probably a good idea for me to use a single image as a map, and split that into an array of separate game objects, but I'm having a little trouble as I'm pretty new to unity.
Answer by koray1396 · Nov 28, 2014 at 04:01 PM
Check Sprite.Create
Sample would be like;
Sprite[,] TileSprites;
public int NumberOfColumns, NumberOfRows;
public Sprite SourceSprite;
void SliceImage(){
int PixelsToUnits = SourceSprite.texture.height / NumberOfRows;
TileSprites = new Sprite[NumberOfColumns, NumberOfRows];
for(int i = 0; i < NumberOfColumns; i++){
for(int y = 0; y < NumberOfRows; y++){
Rect theArea = new Rect(i * PixelsToUnits, y * PixelsToUnits, PixelsToUnits, PixelsToUnits);
TileSprites[i,y] = Sprite.Create(SourceSprite.texture, theArea, Vector2.zero, PixelsToUnits);
}
}
}
edit: the above would create square tiles, and it requires that source texture resolution is divisible by both column number and row number. You can change it accordingly to your needs.
Thank you, this is very helpful! I'm trying to find a method that lays out the sprites onto the scene through the API, but I'm not finding anything. Also, I renamed the background I want to use to "CurrentImage" but it still gives me the error 'The name CurrentImage does not exist in this context', do you know what I can do to fix this? I'm sorry, I'm really new to Unity.
Sorry about that, I just edited, cannot test it now.
So you now only need to assign values to NumberOfColumns and NumberOfRows, and assign a sprite to SourceImage in the editor.
if you want to actually use these sprites on the scene, the method would be like.
Sprite[,] TileSprites;
public int NumberOfColumns, NumberOfRows;
public Sprite SourceSprite;
void SliceImage(){
int PixelsToUnits = SourceSprite.texture.height / NumberOfRows;
TileSprites = new Sprite[NumberOfColumns, NumberOfRows];
for(int i = 0; i < NumberOfColumns; i++){
for(int y = 0; y < NumberOfRows; y++){
Rect theArea = new Rect(i * PixelsToUnits, y * PixelsToUnits, PixelsToUnits, PixelsToUnits);
TileSprites[i,y] = Sprite.Create(SourceSprite.texture, theArea, Vector2.zero, PixelsToUnits);
GameObject tile = new GameObject("Tile-"+i.ToString()+"-"+y.ToString());
SpriteRenderer spr = tile.AddComponent<SpriteRenderer>();
spr.sprite = TileSprites[i,y];
tile.transform.position = new Vector3(i,y);
}
}
}
Also forgot to mention that this creates unit tiles, where height and width are 1, if i am not wrong.
Or is there even a tiling software that includes an option for tiling an entire image ins$$anonymous$$d of piece by piece tiling?
the above will create and place gameobjects, in respect to the original texture. what else do you need?
Your answer
Follow this Question
Related Questions
tilepalette problems 0 Answers
Draw 2d map array with SetTile or Instantiate ? 0 Answers
Infinite map size: respawn you at the other end? 1 Answer
Best way to do tiling in 2D 1 Answer
Get sprite of Tile 2 Answers