Async audio loader with two caching layers for Unity.
AudioLoader.LoadAudioClip(audioURL);
AudioLoader.SetAudioSource(audioURL, audioSource);
AudioLoader.settings.debugLevel = DebugLevel.Error;
In the main thread somewhere at the start of the project need to call AudioLoader.Init();
once to initialize static properties in the right thread. It is required to be made in the main thread. Then you can use AudioLoader
from any thread and at any time.
using Extensions.Unity.AudioLoader;
using Cysharp.Threading.Tasks;
public class AudioLoaderSample : MonoBehaviour
{
[SerializeField] string audioURL;
[SerializeField] AudioSource audioSource;
async void Start()
{
// Loading audio file from web, cached for quick load next time
audioSource.clip = await AudioLoader.LoadAudioClip(audioURL);
// Same loading with auto set to audio
await AudioLoader.SetAudioSource(audioURL, audioSource);
}
}
using Extensions.Unity.AudioLoader;
using Cysharp.Threading.Tasks;
public class AudioLoaderSample : MonoBehaviour
{
[SerializeField] string audioURL;
[SerializeField] AudioSource audioSource1;
[SerializeField] AudioSource audioSource2;
void Start()
{
// Loading with auto set to audio
AudioLoader.SetAudioSource(audioURL, audioSource1, audioSource2).Forget();
}
}
Cache system based on the two layers. The first layer is memory cache, and the second is disk cache. Each layer could be enabled or disabled. It could be used without caching at all. By default, both layers are enabled.
AudioLoader.settings.useMemoryCache = true;
default value is true
AudioLoader.settings.useDiskCache = true;
default value is true
Change disk cache folder:
AudioLoader.settings.diskSaveLocation = Application.persistentDataPath + "/myCustomFolder";
// Override Memory cache for specific audio
AudioLoader.SaveToMemoryCache(url, audioClip);
// Take from Memory cache for specific audio file if exists
AudioLoader.LoadFromMemoryCache(url);
// Check if any cache contains specific audio file
AudioLoader.CacheContains(url);
// Check if Memory cache contains specific audio file
AudioLoader.MemoryCacheContains(url);
// Check if Memory cache contains specific audio file
AudioLoader.DiskCacheContains(url);
// Clear memory Memory and Disk cache
AudioLoader.ClearCache();
// Clear only Memory cache for all audio files
AudioLoader.ClearMemoryCache();
// Clear only Memory cache for specific audio file
AudioLoader.ClearMemoryCache(url);
// Clear only Disk cache for all audio files
AudioLoader.ClearDiskCache();
// Clear only Disk cache for specific audio file
AudioLoader.ClearDiskCache(url);
openupm add extensions.unity.audioloader