Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

CustomCollider2D GC generation

Discussion in '2D Experimental Preview' started by Geneworm, Oct 30, 2021.

  1. Geneworm

    Geneworm

    Joined:
    Jun 26, 2017
    Posts:
    41
    I'm trying out the new CustomCollider2D to generate a collider around a procedural marching squares mesh, the method I'm using is to add a small circle collider to each vertex on the edge:

    upload_2021-10-30_11-43-18.png

    This works quite well for what I need however the GC allocation is quite high, what I'm doing at the moment is clearing the PhysicsShapeGroup2d at the start of the processing and then populating it at the end.

    I image I could optimise this by keeping the circle colliders that have already been generated and only adding the new ones where necessary however I'm here to ask whether it's possible to change the centre position on one of the existing circle colliders to a different spot, rather than deleting them and adding a new one. I just can't find any API that would let me do this
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    The shape group is GC free but it you expand its capacity (which you can set initially) then it'll reallocate obviously. It's just a list behind the scenes. Clearing leaves the capacity intact. I don't know what the GC is you're seeing but if you're increasing beyond capacity then it's likely that.

    Yes it's possible to do this for all shapes. As the docs state for the Circle shape, it's comprised of a single vertex (centroid) and a radius so, like other shapes, use SetShapeVertex() passing the shape-index, the vertex-index (always zero for Circle) and the actual vertex (centroid). Likewise you can change its radius using SetShapeRadius() too.
     
  3. Geneworm

    Geneworm

    Joined:
    Jun 26, 2017
    Posts:
    41
    Thanks for getting back to me, the GC I'm seeing appears to be from string formatting, I'm not sure if I'm doing something wrong:

    upload_2021-10-30_17-46-44.png
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    There's no string manipulation in the shape group, so that is very odd.

    Actually there will be but only if it's throwing exceptions when it's formatting the exception message string.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Actually, I'll need to check to ensure that the damn exception helpers are not executing that even if an exception isn't processed. In which case this is a bug that needs to be fixed ASAP.

    Would you mind reporting this as a bug in the meantime and posting me the case number?
     
  6. Geneworm

    Geneworm

    Joined:
    Jun 26, 2017
    Posts:
    41
    Sure I'll do that now
     
  7. Geneworm

    Geneworm

    Joined:
    Jun 26, 2017
    Posts:
    41
    Case number 1377116
     
    MelvMay likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Thank you for that! I'll take that case number and get back to you ASAP.

    Sorry for this issue, we'll get it sorted.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    This has now been fixed and has gone to QA for testing. I cannot give you exact dates when it'll land but you can use the public tracker link here to follow its progress into 2022.1.x and 2021.2.x

    It's unfortunate that this wasn't spotted and reported during the whole Beta cycle so thank you for reporting this and sorry for the issue it's caused.
     
    NotaNaN likes this.
  10. Geneworm

    Geneworm

    Joined:
    Jun 26, 2017
    Posts:
    41
    No problem, now I know it's a bug and a fix will be implemented eventually I can continue to use the custom collider and just work around it. Thanks for your help.
     
    NotaNaN and MelvMay like this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    The fixes have passed review and QA so are now in the release streams waiting to land. When they do, they'll be in the subsequent releases. The link above will state which ones when they land.

    Before Fix:
    Physics_beforeGCFix.png

    After Fix:
    Physics_afterGCFIx.png
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    A quick update:

    The fixes have now landed in the yet-to-be-released 2021.2.3f1 and 2022.1.0a15. Eventually, prior to release, this will be reflected in the public case tracker here.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    2021.2.3f1 is available for download so I'm hoping you no longer see this issue unity that version or later.
     
  14. Geneworm

    Geneworm

    Joined:
    Jun 26, 2017
    Posts:
    41
    Thanks! I'll download that version and check it out.
     
    MelvMay likes this.