- Home /
combining littles sprites into one big sprite ?
Working on creating a 2d lvl procedurally i instantiate a lot of sprite of size 32*32. And i was wondering if it's possible to combine all the sprites next to each other (wich are the same into one) ? if it's relevant to save ressources or if it's unecessary ?
Answer by jorjdboss · Jan 18, 2014 at 11:31 AM
Yes if you have hundreds of sprites which all have different materials(and/or textures) then the performance hit will be big on mobile.
Unity does Dynamic batching for you where it sends batches of meshes to render. It collects several meshes until it reaches about 900 verts in total and batch it to 1 draw call. So it would batch 225 sprites in 1 draw call, on condition they use the same material.
If you're using Unity 4.3 or above, you can import a sprite sheet from texture packer and make unity recognise it as as sprite sheet by selecting 'multiple' in the import settings. The instantiated sprites are automatically batched at runtime.
Also make sure you don't use Instantiate too frequently, consider using an object recycler if possible.
Ups i didn't specify a lot of things. The targeted platform is PC, and i instantiate only on the first load to create the lvl (terraria like)So i'll have thousand and thousand sprites of 32*32. And i'm using the spriteSheet. Is there a limit of drawcalls we can have ? What is the best number to keep the drawcall under ?
$$anonymous$$ost modern PCs, even with integrated graphics can handle thousands of draw calls depending on what other workload the gpu handles, such as deferred lighting, texture filtering and antialiasing. So if you don't do much of the latter, then your draw call limit can be high.
Take a look at this forum thread . Its a bit old but will give you a clearer picture of PC graphics optimisation.
If you want to target low end PCs like netbooks, like I said in my answer, batch the draw calls as much as you can.
Check out this $$anonymous$$esh$$anonymous$$erger script. $$anonymous$$ight be helpful to you. Apart from that, like I said in my answer, use spritesheets to reduce the number of materials used and hence the draw calls.
Thanks for the answer. I was wondering is there a relation between drawcall and frame per seconds ? Because when i increase the number of object in my scene ( actually chunk wich contains the sprite tiles, so i'll have hundreds thousand sprites in my scene), the drawcall stay pretty low but my FPS decrease drastically, i end up getting 2 FPS. Is that due to the number of objects in the scene, even if they are not render by the camera ? How can you prevent this ?
I have the same issue. If you had found a solution to issue you've mentioned can you help me out on how to solve the issue. Huge number of sprites are created when a user gives a input. The sprites are dynamic sprite. So when the count increases the batch remains low but the fps reduces from 200 to 10. Any kind of help would be appreciated.
Do you have a script attached to each sprite with any update loops? Even if you have say, 50 sprites and each of them doing expensive code every frame, then its gonna run slow. There are a multitude of other reasons why the FPS is low. If you have the pro version(or the trial) you may want to run the profiler and identify whats causing the bogdown.
I don't have the pro version. And i don't have script attach to the sprites. I just create a class DataSprite for each sprites at the beginning to access them and tell them what i want, that's it. All the codes are launched at the begining to create the world, the chunks and the sprites. But no more code is running after that for the moment. (if i didn't made an error, but i don't think so)