Hello
I’m studying colliders and I have two questions.
What is the performance difference between using 100 BoxCollider2Ds and CompositeCollider2D together?
How much performance difference is there between creating multiple Cells using GameObject.Instantiateand creating multiple Cells using Tilemap’s SetTiles?
Well, a CompositeCollider2D takes a set of colliders and essentially turns them into either an EdgeCollider2D or PolygonCollider2D depending on the user’s preference.
That being said, the defining characteristics that would effect performance in this scenario would primarily be:
How large is the CompositeCollider2D? And how often will collision checks be performed against it, particularly while inside its AABB (Axis Aligned Bounding Box)?
How complex is the collider that is generated from CompositeCollider2D?
A complex collider will take much more time to process than a CompositeCollider2D that generates a simple, low-vertex shape.
These two qualities of a given CompositeCollider2D will greatly effect its potential performance impact.
That being said — I believe that in most cases it would be advised to use a CompositeCollider2D in this instance.
Checking against (on average) 100 BoxCollider2D AABBs vs checking against 1 CompositeCollider2D of moderate complexity (in my mind) would almost always have the CompositeCollider2D come out on top — particularly in scenarios where the AABB of the CompositeCollider2D can be checked against instead of its (potentially complex) shape.
HOWEVER.
Profile. Profile. Profile your usecase!
That is the only way you will know what is best for your particular scenario.
The difference is MASSIVE.
And in more ways than one.
First of all —
GameObjects are much heavier — in both the time it takes to instantiate them and in their memory footprint (which you will feel later when the GC decides to do its thing) — than Tiles in a Tilemap.
Instantiating hundreds of GameObjects (to presumably use them like Minecraft blocks) is undoubtedly a very bad idea… particularly if you plan to destroy them frequently… and… please tell me you are NOT targeting mobile?
And don’t even get me started about rendering.
If your game strongly hinges on these “blocks”, I would highly recommend using Tiles instead (or a custom solution if you feel like tinkering).