Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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:
    233
    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???