AudioAsset¶
Uniasset.Audio.AudioAsset is the high-level audio wrapper for loading audio, reading PCM frames sequentially, seeking arbitrarily, and converting the result to a Unity AudioClip.
Definition¶
Read-only properties¶
| Property | Type | Description |
|---|---|---|
SampleRate |
int |
Sample rate, for example 44100 |
SampleCount |
long |
Total sample count |
ChannelCount |
int |
Number of channels |
FrameCount |
long |
Total frame count |
For multi-channel audio:
Loading¶
Load¶
Loads audio from a file path.
| Parameter | Type | Default | Description |
|---|---|---|---|
path |
string |
— | Audio file path |
sampleFormat |
SampleFormat |
Float |
Decoded sample format |
Load(Span<byte>)¶
Loads audio from a byte buffer.
| Parameter | Type | Default | Description |
|---|---|---|---|
data |
Span<byte> |
— | Encoded audio bytes |
sampleFormat |
SampleFormat |
Float |
Decoded sample format |
LoadIO¶
Loads audio from a custom stream.
If stream is null, the method throws ArgumentNullException.
Note
AudioAsset currently has no LoadAsync(...) overloads and no LoadIO(Stream) convenience overload.
Reading and seeking¶
Tell¶
Returns the current read position in frames.
Seek¶
Seeks to the specified frame index.
| Parameter | Type | Description |
|---|---|---|
position |
long |
Target frame index |
Read<T>¶
Reads PCM frames into the destination buffer.
| Parameter | Type | Description |
|---|---|---|
buffer |
Span<T> |
Destination buffer |
frameCount |
int |
Number of frames to read |
The generic type T must match the selected SampleFormat:
| SampleFormat | T |
|---|---|
Float |
float |
Int16 |
short |
float[] buffer = new float[1024 * audio.ChannelCount];
int framesRead = audio.Read<float>(buffer, 1024);
The return value is the number of frames actually read.
SeekUnsafe¶
Seeks to the specified frame position.
Thread safety
This method is not thread-safe. Use it only when the asset is accessed from a single thread. Use Seek in concurrent scenarios.
| Parameter | Type | Description |
|---|---|---|
position |
long |
Target frame index |
It behaves like Seek but skips internal synchronization for performance.
ReadUnsafe<T>¶
Reads PCM frames into the destination buffer.
Thread safety
This method is not thread-safe. Use it only when the asset is accessed from a single thread. Use Read in concurrent scenarios.
| Parameter | Type | Description |
|---|---|---|
buffer |
Span<T> |
Destination buffer |
frameCount |
int |
Number of frames to read |
The generic type T must match the selected SampleFormat:
| SampleFormat | T |
|---|---|
Float |
float |
Int16 |
short |
It behaves like Read<T> but skips internal synchronization for performance.
float[] buffer = new float[1024 * audio.ChannelCount];
int framesRead = audio.ReadUnsafe<float>(buffer, 1024);
The return value is the number of frames actually read.
Conversion¶
ToAudioClip¶
Converts the audio into a Unity AudioClip.
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
string |
"created_from_uniasset" |
AudioClip name |
stream |
bool |
true |
Whether to create a streaming clip |
Streaming (stream = true):
- Audio data is pulled on demand during playback
- Lower memory usage, suitable for long clips
Fully loaded (stream = false):
- Unity pulls the complete audio data during clip creation
- Better for short sound effects or repeated playback
AudioClip bgm = audio.ToAudioClip("BGM", stream: true);
AudioClip sfx = audio.ToAudioClip("Explosion", stream: false);
Lifetime¶
Unload¶
Unloads the currently loaded audio data. You can call Load again afterward.
Dispose¶
Releases the underlying native resources. The instance should not be used afterward.
Examples¶
Load audio and play it¶
using Uniasset.Audio;
using var audio = new AudioAsset();
audio.Load("background.mp3");
AudioClip clip = audio.ToAudioClip("BGM");
audioSource.clip = clip;
audioSource.Play();