Skip to content

Instantly share code, notes, and snippets.

@robertwahler
Created January 22, 2019 16:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertwahler/884339ea639784275fbba1a611c22499 to your computer and use it in GitHub Desktop.
Save robertwahler/884339ea639784275fbba1a611c22499 to your computer and use it in GitHub Desktop.
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