Unity AudioSource 无法动态加载播放 mP3文件 Github上面提供了一个办法 …Github 例子很简单看一下就会了 将NAudio 文件放到Plugins 下 2.最重要的 NAudioPlayer.cs 脚本
3 编写自己的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class LoadMp3 : MonoBehaviour
{
private string mMusicURL;
string AudioClipName = "少年.mp3";
void Start()
{
mMusicURL = Application.dataPath + "/Audio/" + AudioClipName;
StartCoroutine(loadMusic(mMusicURL));
}
IEnumerator loadMusic(string URL)
{
UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(URL, AudioType.OGGVORBIS);
AsyncOperation async = request.SendWebRequest();
while (!request.isDone)
{
Debug.Log("Music " + async.progress);
yield return null;
}
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(request.error);
}
else
{
AudioSource source = gameObject.AddComponent<AudioSource>();
AudioClip clip = NAudioPlayer.FromMp3Data(request.downloadHandler.data);
clip.name = AudioClipName;
source.clip = clip;
source.Play();
}
}
}
|