UNT0028 Use non-allocating physics APIs
June 17, 2022 ยท View on GitHub
Non-allocating versions of Physics query APIs have been introduced. You can replace RaycastAll calls with RaycastNonAlloc, SphereCastAll calls with SphereCastNonAlloc, and so on.
Examples of patterns that are flagged by this analyzer
using UnityEngine;
class Camera : MonoBehaviour
{
void Update() {
var result = Physics.RaycastAll(Vector3.zero, Vector3.zero);
// ...
result = Physics.RaycastAll(Vector3.zero, Vector3.zero);
}
}
Solution
Instead of allocating a new array for each call, you can reuse a pre-allocated array to store the results. This will improve performance, especially for frequent calls.
No automatic code fix is available for this diagnostic.