Giter VIP home page Giter VIP logo

unity-audioloader's Introduction

Unity Audio Loader

npm openupm License Stand With Ukraine

Async audio loader with two caching layers for Unity.

Image Loader

Features

  • ✔️ Async loading from Web or Local AudioLoader.LoadAudioClip(audioURL);
  • ✔️ Memory and Disk caching - tries to load from memory first, then from disk
  • ✔️ Dedicated thread for disk operations
  • ✔️ Avoids loading same audio multiple times simultaneously, a task waits for completion the first and just returns loaded audio if at least one cache layer activated
  • ✔️ Auto set to AudioSource AudioLoader.SetAudioSource(audioURL, audioSource);
  • ✔️ Debug level for logging AudioLoader.settings.debugLevel = DebugLevel.Error;

Usage

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.

Sample - Loading audio file, set to AudioSource

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);
    }
}

Sample - Loading audio into multiple Audio components

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

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.

Setup Cache

  • 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 Cache

// Override Memory cache for specific audio
AudioLoader.SaveToMemoryCache(url, audioClip);

// Take from Memory cache for specific audio file if exists
AudioLoader.LoadFromMemoryCache(url);

Does Cache contain audio

// 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 Cache

// 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);

Installation

openupm add extensions.unity.audioloader

unity-audioloader's People

Contributors

ivanmurzak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

aboutunity

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.