Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- using RuntimeAudioClipLoader;
- using System.Collections.Generic;
- namespace RuntimeAudioClipLoaderDemo
- {
- public class LoadAllStreamingAssets : MonoBehaviour
- {
- static string rootFolder { get { return Application.streamingAssetsPath; } }
- static string sourceFolder { get { return Path.Combine(rootFolder, "RuntimeAudioClipLoader"); } }
- public List<string> streamingAssetPathsToLoad;
- public List<AudioClip> loadedAudioClips = new List<AudioClip>();
- void Start()
- {
- streamingAssetPathsToLoad = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories)
- .Where(f => Manager.IsSupportedFormat(f))
- .Select(f => f.Substring(rootFolder.Length + 1))
- .ToList();
- }
- Coroutine lastCoroutine;
- private void Update()
- {
- if (streamingAssetPathsToLoad.Count > 0 && lastCoroutine == null)
- {
- var path = streamingAssetPathsToLoad[0];
- streamingAssetPathsToLoad.RemoveAt(0);
- lastCoroutine = StartCoroutine(StartLoading(path));
- }
- }
- IEnumerator StartLoading(string path)
- {
- var url = "file://" + Path.Combine(rootFolder, path);
- byte[] bytes = null;
- using (var webRequest = UnityEngine.Networking.UnityWebRequest.Get(url))
- {
- webRequest.SendWebRequest();
- while (!webRequest.isDone)
- {
- yield return new WaitForEndOfFrame();
- }
- if (webRequest.isNetworkError)
- {
- // failed
- lastCoroutine = null;
- yield break;
- }
- bytes = webRequest.downloadHandler.data;
- }
- var config = new AudioLoaderConfig();
- config.DataStream = new MemoryStream(bytes);
- config.UnityAudioClipName = path;
- var audioLoader = new AudioLoader(config);
- audioLoader.StartLoading();
- while (audioLoader.LoadState == AudioDataLoadState.Loading)
- {
- yield return new WaitForEndOfFrame();
- }
- if (audioLoader.LoadState == AudioDataLoadState.Failed)
- {
- // failed
- lastCoroutine = null;
- yield break;
- }
- loadedAudioClips.Add(audioLoader.AudioClip);
- lastCoroutine = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment