- Home /
Assign multiple sprites to a renderer, or assign multiple renderers to single object
If I want to make a tilemap, with sprite Assets consisting of multiple tiles, I need to be able to adequately sort each tile sprite depending on its position on the isometric grid (thus "cutting" the sprite Asset in tile units).
To achieve this, a simple solution would be to create one gameobject per tiled sprite, and track its transform.position. Problem is the scene could rapidly get crammed with gameobjects.
Thus my idea was to get a single gameobject (the sprite Asset) to render multiple sprites (one sprite per tile occupied). To my knowledge, it is impossible to either :
get a single gameobject to use two different sprite renderers each assigned with rendering a different sprite (tile).
get a single sprite renderer to render multiple sprites at the same time, each being assigned a different sorting order.
Is there a way out of this plight? Or is having a plethora of gameobjects in a scene not really an issue performance-wise?
Answer by supernat · Jan 29, 2014 at 07:16 AM
Having tons of game objects will always be an issue performance-wise and death if you're doing mobile. You will want to create a single quad that has a vertex at every tile corner, and just set the texture coordinate for that vertex to the texture coordinate of that tile for that corner. I'm not sure what you mean by sprite rendered as sprite is a rather generic term. Are you using a particular sprite system (like 2DTK?)
My suggestion is that while it's a good exercise in learning, it'd be way more efficient to purchase a plugin from the Asset Store to do this for you. Otherwise, you'll want to create a dynamic vertex buffer at runtime and set the texture coordinates for each tile in the buffer. Then as you move through the world, you would load up new quads in front of you and unload old ones that are out of sight.
What I should have said sooner is that this project is actually based on the isometric view. What I have coded so far is a SpriteAsset importer that creates prefabs based on the data stored in a xml file this way :
Cut asset in tile width sized sprites
Draw each tile-sized sprite with correct offset (start with the top left most one, end with the bottom right most one)
Group all these sprites in a single gameobject
Sort each sprite order based on its position in the isometric grid (bottom-right is higher, top-left is lower) granting me the ability to have assets more than one unit tall to overlay parts of each other depending on their position.
This method gives me incredible ease in the editor when it comes to building a level, as I can freely adjust a sprite asset position, flip it... The idea was actually to get something similar to the Shadowrun Editor (video here).
The idea of a single mesh is way better performace-wise but would not, in my humble opinion, offer such ease-of-use. Nonetheless, I think I'll use this technique when it comes to making the ground of the level (representing the vast majority of tiles in the level, thus reducing the gameobjects count ALOT).
I apologize if I don't make much sense, but (1) I am new to all this coding poetry, and (2) English ain't my first language.
Sorry, I assumed you were just creating a single layered ground map from tiles. You may have a nice balance to have the ground layer be a single mesh (or large mesh chunks that are dynamically loaded/unloaded) and to use objects for detail sprites like mountains, characters, buildings, etc.
Your answer
Follow this Question
Related Questions
How to correctly use SpriteRenderer with multiple sprites (if possible) 0 Answers
How to truncate sprite without changing the scale of the sprite? 1 Answer
Need help with size of Tiles! 2 Answers
How do you load a single sprite from a multiple sprite sheet with script? 1 Answer
Sprite Mask Render Issue 1 Answer