Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Collisions and garbage

Discussion in 'Physics' started by Robdon, Jun 13, 2018.

  1. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    75
    Haven't looked at this for a while, but I believe that now collisions can be detected without garbage?

    I knew there was a problem with garbage if you wanted Contacts, and there is now a new command GetContracts() that you can pass a predefined array, and that all looks good.

    But, if I just have a simple OnCollisionEnter2D() and do a GetContacts in there, it still seems to generate garbage, specifically in the profiler as ConvertCollision2DForScript() and SendCollisionContractCallbacks().

    Am I supposed to used GetContacts in OnCollisionEnter2D or is there something I have to do to stop the 'old' garbage being allocated?

    I've also noted that even if the OnCollisionEnter2D() routine has no code in it, it still generates garbage on collisions. (I even tried to removed the Collision2D parameter, and it still did it)

    I've also tried doing my GetContacts in FixedUpdate() as a test, and this seemed to remove all garbage, but now obviously its doing the check every physics tick rather than a callback.

    Is that the correct way to use GetContacts now, in FixedUpdate to get zero garbage or is there something else?

    Using 2018.1.0f2
     
  2. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    2,683
    Did you profile a build or the editor? If you profiled the game running in the editor, I recommend to profile a build on the target hardware instead.

    The memory allocation and performance characteristics between a build and editor are often significant, like shown here. Many GCAlloc's that occur in the editor are completely gone in a build.
     
  3. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    75
    Thanks. I profile in the build never in the editor as I know it gives completely different results in the editor.
     
  4. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    25
    Hello Robdon, do you still have GCAllocs on your targeted hardware? I still have a lot of GC.Alloc even if I use GetContacts inside oncollisionenter2d:
    upload_2018-9-2_8-1-45.png
     
  5. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    75
    Yes, still getting them. I'm not sure if it's been fixed so that Unity shouldn't allocate for collisions, or if it hasn't been changed or fixed yet. I couldn't find any info about it, and no on 'official' has replied.
     
  6. samifruit514

    samifruit514

    Joined:
    Mar 27, 2015
    Posts:
    25
    I just discovered that we can define void OnCollisionXXX() without Collision2D argument (which is not documented anywhere!!). I was expecting that it would not alloc mem for Collision2D, but it seems that it does still alloc. Maybe you would be luckier if you try
     
  7. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    75
    Yeah, I knew about that and had tried it before. It didn't help.
     
  8. DrDrimixar

    DrDrimixar

    Joined:
    Aug 13, 2014
    Posts:
    5
    Bump, I have the same problem with it even on Unity 2018.2.14f1.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,504