-
Notifications
You must be signed in to change notification settings - Fork 36
Lowlevel & Resource Management
genar edited this page Sep 25, 2023
·
3 revisions
To reduce the GC pressure the low-level extension has some classes and data structures that make use of native memory. The best part is that this is fast and blittable and can be natively included in any component.
Supported data structures:
UnsafeArray<T>
UnsafeList<T>
UnsafeStack<T>
UnsafeQueue<T>
UnsafeJaggedArray<T>
Additional data structures:
JaggedArray<T>
SparseJaggedArray<T>
// Create (Using automatically disposes)
using var array = new UnsafeArray<int>(8);
array[0] = 0;
array[1] = 1;
// Acess
ref var intRef = ref array[0];
// Iterate
foreach(ref var item in array){
...
}
// Create (Using automatically disposes)
using var list = new UnsafeList<int>(8);
list.Add(1);
list.Add(2);
// Acess
ref var intRef = ref list[0];
// Iterate
foreach(ref var item in list){
...
}
Thanks to the Resource Manager, you can access and use a managed resource from anywhere using a unique ID. This saves memory and is blittable, so it can also be used in unsafe code.
// Create resources jagged array and create a sprite handle
var resources = new Resources<Sprite>(IntPtr.Size, capacity: 64);
var handle = resources.Add(new Sprite());
var sprite = resources.Get(in handle);
// Use handle wherever you want to access the sprite
public struct SpriteComponent{
public Handle<Sprite> handle;
}