Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. 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:
    59
    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,254
    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:
    59
    Thanks. I profile in the build never in the editor as I know it gives completely different results in the editor.