Audio Data

In Unity, AudioClip objects are not serializable by default. Therefore, to save and load them, they need to be converted to byte arrays first. The AudioClip is converted to a WAV byte array for saving and is then converted back to an AudioClip when loading.

Saving Audio Data: To save an AudioClip, it is first converted to a byte array using the ToBytes extension method and then saved using Tarsave's Save method.

public void Save()
{
    byte[] audioData = _audioClip.ToBytes();
    bool result = _dataPersistenceService.Save("Key", audioData);
}

Loading Audio Data: During loading, the saved byte array is retrieved and converted back to an AudioClip using the ToAudioClip extension method.

public void Load()
{
    byte[] audioData = _dataPersistenceService.Load<byte[]>("Key");
    _audioClip = audioData.ToAudioClip();
}

The AudioHelper class provides methods to convert an AudioClip to a byte array and to convert a byte array back to an AudioClip. The ToBytes method converts the AudioClip to a byte array, and the ToAudioClip method reconstructs the AudioClip from the byte array.

Last updated