Skip to content

Instantly share code, notes, and snippets.

@IJEMIN
Created February 18, 2020 02:55
Show Gist options
  • Save IJEMIN/f445458748d003d8f2201981776ec6d8 to your computer and use it in GitHub Desktop.
Save IJEMIN/f445458748d003d8f2201981776ec6d8 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class WebAudioImporter : MonoBehaviour
{
private MobileImporter mobileImporter;
private void Start()
{
mobileImporter = GetComponent<MobileImporter>();
mobileImporter.Loaded += OnAudioLoaded;
StartCoroutine(DownloadAndLoadAudio());
}
private IEnumerator DownloadAndLoadAudio()
{
var req = UnityWebRequest.Get("https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3");
yield return req.SendWebRequest();
var downloadedBytes = req.downloadHandler.data;
var savePath = Path.Combine(Application.temporaryCachePath, "downloadedAudio.mp3");
File.WriteAllBytes(savePath, downloadedBytes);
mobileImporter.Import(savePath);
}
private void OnAudioLoaded(AudioClip audioClip)
{
Debug.Log(audioClip.length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment