Hello!
I am trying to set the data of an audioclip using a ReadOnlySpan because the documentation for audioClip.SetData() reccomended this as it leads to better performance.
Documentation for Unity6 (6000):
“For best performance, the Span version is preferred as it does not require allocating managed memory.”
I tried doing it exactly as the documentation did it in the example code, but the problem is that the ReadOnlySpan override of the SetData method is not accepted as a valid override. The only one showing up is the one using a normal float array, and I naturally get the “Cannot convert Unity.Collections.NativeArray to float[ ]”. I also tried using a ReadOnlySpan of floats directly, and this naturally doesn’t work either.
Any ideas to why I can’t acces the ReadOnlySpan version of the method? I tried searching it up but I couldn’t find any leads.
Here is the code I tried to replicate taken from the example in the documentation:
public class Example : MonoBehaviour
{
// Read all the samples from the clip and halve the gain
void Start()
{
AudioSource audioSource = GetComponent<AudioSource>();
var numSamples = audioSource.clip.samples * audioSource.clip.channels;
var samples = new NativeArray<float>(numSamples, Allocator.Temp);
audioSource.clip.GetData(samples, 0);
for (int i = 0; i < samples.Length; ++i)
{
samples[i] = samples[i] * 0.5f;
}
audioSource.clip.SetData(samples, 0);
}
}
(This is my first time asking a question, please correct me if I posted at the wrong place or should do it differently, Thank You :))