Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug PhysicsRaycaster alloc GC every frame

Discussion in 'UGUI & TextMesh Pro' started by ununion, Mar 24, 2023.

  1. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    this is the PhysicsRaycaster source code
    Code (CSharp):
    1.  
    2. if (hitCount != 0)
    3.             {
    4.                 if (hitCount > 1)
    5.                     System.Array.Sort(m_Hits, 0, hitCount, RaycastHitComparer.instance);
    6.  
    7.                 for (int b = 0, bmax = hitCount; b < bmax; ++b)
    8.                 {
    9.                     var result = new RaycastResult
    10.                     {
    11.                         gameObject = m_Hits[b].collider.gameObject,
    12.                         module = this,
    13.                         distance = m_Hits[b].distance,
    14.                         worldPosition = m_Hits[b].point,
    15.                         worldNormal = m_Hits[b].normal,
    16.                         screenPosition = eventData.position,
    17.                         displayIndex = displayIndex,
    18.                         index = resultAppendList.Count,
    19.                         sortingLayer = 0,
    20.                         sortingOrder = 0
    21.                     };
    22.                     resultAppendList.Add(result);
    23.                 }
    24.             }
    this is the source code,as you can see Array.Sort create GC every frame,is there any way to solve this???