I’ve just upgraded from 5.5 → 5.6, which states ‘Physics2D improvements’, so I tested to see if the “Physics.Contacts” garbage allocations had gone.
Woohoo, I thought, as I could not see any, however, now I’m getting allocations on ‘Physics2D.ConvertCollision2DForScript’ (this wasn’t there before in 5.5, and I cant see anything in Google about it)
This seems to be a new one, or has Contacts just been renamed, and nothing has been fixed with garbage yet?
GC for 2D & 3D callbacks are not new although the profiler entries have changed a lot but there are two problems.
The first is that the Collision & Collision2D types are classes and are legacy issues. They cannot simply be changed to structs but there are plans to provide better callbacks. There is a small overhead of creating the Collision or Collision2D classes.
The second is more of a problem in that the “contacts” property returns an array of contact points. This is done for you even if you don’t use them. For 2D I have fixed this and it will be in a future release: x.com
Essentially “Collision2D.contacts” for 2D physics will now be evaluated lazily (only if you use them). To avoid GC altogether, you can call “Collision2D.GetContacts()” and there’s a new “Physics2D.GetContacts(Collider2D, Collider2D)” as well.
I didn’t say that it was fixed and it’s about to land either. You forgot to mention the detail of what I said which was that this work cannot continue while we’re held up with other implementations that are outside of our control. Unity is a large organization and priorities shift and the physics team and others are at the mercy of various systems dev.
I recerntly made a big push on this because I was frustrated by nothing happening. The good news is that we’ve got a meeting with the appropriate parties involved with a view to getting this finally resolved, even if it’s just for 2D/3D physics initially.
Completely transparent and the problem and intent was made clear. No promises either. We’ve been held up by major refactorings by other teams that are now coming to fruition.
Inferring that I made the post then waited for a year to book a meeting.
Ah, so when we have this ‘lazy’ fix, what we could do is still use the OnCollisionEnter2D() event, with no params, and then manually process GetContatcs() with a predefined array, and then we should get zero GC I guess?