Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Non allocating, lazy loaded, Unity Physics.OverlapSphere
/// <summary>
/// Collider cache for non allocating physics calls. Lazy loaded.
/// </summary>
/// <example>
/// Usage
/// <code>
/// float radius = 0.2f;
/// int colliderCount = Physics.OverlapSphereNonAlloc(transform.position, radius: radius, results: ColliderCache);
/// for (int i = 0; i<colliderCount; i++) {
/// ...
/// }
/// </code>
/// </example>
protected Collider[] ColliderCache {
get {
if (colliderCache == null) {
colliderCache = new Collider[colliderCacheSize];
}
return colliderCache;
}
}
protected int colliderCacheSize = 32;
private Collider[] colliderCache;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment