Unity Tip: ArrayPool for Easier Non Alloc Physics Queries
January 29 2018
Here's a quick tip. In the upcoming Unity 2018.1 beta, they've recently added support for .NET Standard 2.0 profile. This includes System.Buffers.ArrayPool
class. ArrayPool acts as a managed pool of strongly typed arrays. This class can be particularly useful when working with the *NonAlloc Unity Physics queries, as they require an array as an input.
Before, you likely had code like the following:
const int kCacheSize = 256;
Collider[] colldierCache; // Member variable to the script as a cache
void Awake() {
colliderCache = new Collider[kCacheSize];
}
void CheckSphere(...) {
int colliderCount = Physics.OverlapSphereNonAlloc(..., colldierCache, ...);
for (int i = 0; i < colliderCount; i++) {
...
// Process colldier here
...
}
}
The overhead of needing to manage a member variable as a cache can be quite cumbersome, especially when it's an operation as common as raycasting. The previously shown code can be rewritten like so:
using System.Buffers;
void CheckSphere(...) {
const int kCacheSize = 256;
var arrayPool = ArrayPool<Collider>.Shared;
Collider[] colliderCache = arrayPool.Rent(kCacheSize);
int colliderCount = Physics.OverlapSphereNonAlloc(..., colldierCache, ...);
for (int i = 0; i < colliderCount; i++) {
...
// Process colldier here
...
}
arrayPool.Return(colliderCache);
}
Some things to consider: