Skip to content

santutu/unity-dynamic-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Background

  1. Why should we pay attention to trivial matters? When using Physics.NonAlloc, determining the size of the array is usually unnecessary.
If the number of detected colliders exceeds the size of the array, the DynamicArray will automatically resize the array.
  1. When executing Physics.NonAlloc method, the number of detected colliders and the actual colliders are handled separately as an int and an array[], making it inconvenient to use.
A DynamicArray instance holding this information would be more convenient to use.
  1. For concise code.
using var result = DPhysics.OverlapSphereNonAllocFromPool(transform.position, 3);
            
foreach (var collider in result)
{
    Debug.Log(collider.name);
}

Use Case

  1. automatic resizing
DynamicArray<RaycastHit> array = new();
//just call the method, and it will automatically resize the array.
DPhysics.SphereCastNonAlloc(ray, 1, 1000, array)
  1. foreach loop
var results = DPhysics.SphereCastNonAlloc(ray, 1, 1000, array);
foreach (var hit in results)
{
    Debug.Log(hit.collider.name);
}
  1. for loop
var results = DPhysics.SphereCastNonAlloc(ray, 1, 1000, array);

for (var i = 0; i < results.Length; i++)
{
    Debug.Log(array[i].collider.name);
}
  1. passing the detected collider array as a parameter
var results = DPhysics.SphereCastNonAlloc(ray, 1, 1000, array);
Log(results);

The detected colliders and their count can be accessed from anywhere using dynamicarray

  1. from pool of dynamic array
using var array = DynamicArray<Collider>.Get();
            
var results = DPhysics.OverlapSphereNonAlloc(transform.position, 1, array);

foreach (var collider in results)
{
    Debug.Log(collider.name);
}
            
  1. shorthand
using var result = DPhysics.OverlapSphereNonAllocFromPool(transform.position, 3);
            
foreach (var collider in result)
{
    Debug.Log(collider.name);
}

Git install URL

https://github.com/santutu/unity-dynamic-array.git?path=/Assets/Santutu/Core/DynamicArray

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors