AudioKitWithResKitInit.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using UnityEngine;
  3. namespace QFramework
  4. {
  5. public class AudioKitWithResKitInit : MonoBehaviour
  6. {
  7. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  8. public static void Init()
  9. {
  10. AudioKit.Config.AudioLoaderPool = new ResKitAudioLoaderPool();
  11. }
  12. }
  13. public class ResKitAudioLoaderPool : AbstractAudioLoaderPool
  14. {
  15. public class ResKitAudioLoader : IAudioLoader
  16. {
  17. Lazy<ResLoader> mResLoader = new Lazy<ResLoader>(()=>ResLoader.Allocate());
  18. public AudioClip Clip => mClip;
  19. private AudioClip mClip;
  20. public AudioClip LoadClip(AudioSearchKeys audioSearchKeys)
  21. {
  22. mClip = mResLoader.Value.LoadSync<AudioClip>(audioSearchKeys.AssetName);
  23. return mClip;
  24. }
  25. public void LoadClipAsync(AudioSearchKeys audioSearchKeys, Action<bool, AudioClip> onLoad)
  26. {
  27. mResLoader.Value.Add2Load<AudioClip>(audioSearchKeys.AssetName,(b, res) =>
  28. {
  29. mClip = res.Asset as AudioClip;
  30. onLoad(b,res.Asset as AudioClip);
  31. });
  32. mResLoader.Value.LoadAsync();
  33. }
  34. public void Unload()
  35. {
  36. mClip = null;
  37. mResLoader.Value.Recycle2Cache();
  38. }
  39. }
  40. protected override IAudioLoader CreateLoader()
  41. {
  42. return new ResKitAudioLoader();
  43. }
  44. }
  45. }