I've been trying to migrate SoundReplacement from using WWW to UnityWebRequest. The editor reports that it's outdated and there are comments in the code about migrating. I'll include the relevant portion of what I have so far. The problem is that while it works when I load replacement music, the game hard locks when it tries to load a WAV replacement (which causes streaming to be false). I'm pretty sure that it's the "WaitForSeconds(0)" call but if I don't add a way to make the code wait, unityWebRequestAudioClip might be equal to null when I assign it to the out variable.
Second question is where can I find the code that creates a dungeon automap's models? Especially the part where textures are applied to them?
Code: Select all
private static AudioClip unityWebRequestAudioClip;
private static bool TryImportAudioClip(string name, string extension, bool streaming, out AudioClip audioClip)
{
// Seek from loose files
string filePath = Path.Combine(soundPath, name + extension);
if (File.Exists(filePath))
{
AudioType audioType = AudioType.UNKNOWN;
if (extension.Equals("ogg", System.StringComparison.OrdinalIgnoreCase))
audioType = AudioType.OGGVORBIS;
else if (extension.Equals("wav", System.StringComparison.OrdinalIgnoreCase))
audioType = AudioType.WAV;
DaggerfallUnity.Instance.StartCoroutine(UseUnityWebRequest(filePath, streaming, audioType));
while (unityWebRequestAudioClip == null)
new WaitForSeconds(0);
audioClip = unityWebRequestAudioClip;
unityWebRequestAudioClip = null;
Debug.LogErrorFormat("audioClip = " + audioClip + ", unityWebRequestAudioClip = " + unityWebRequestAudioClip);
return true;
}
}
private static IEnumerator UseUnityWebRequest(string filePath, bool streaming, AudioType audioType)
{
using (UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip("file://" + filePath, audioType))
{
((DownloadHandlerAudioClip)unityWebRequest.downloadHandler).streamAudio = true;
unityWebRequest.SendWebRequest();
if (streaming)
{
while (unityWebRequest.downloadProgress < 0.001)
yield return new WaitForSeconds(0);
}
else
yield return unityWebRequest.isDone;
if (unityWebRequest.isNetworkError)
Debug.LogErrorFormat("System error encountered while trying to load audioclip '{0}': {1}", filePath, unityWebRequest.error);
else if (unityWebRequest.isHttpError)
Debug.LogErrorFormat("Network error encountered while trying to load audioclip '{0}': {1}", filePath, unityWebRequest.responseCode);
else
{
unityWebRequestAudioClip = ((DownloadHandlerAudioClip)unityWebRequest.downloadHandler).audioClip;
Debug.LogWarningFormat("Web request done: " + unityWebRequestAudioClip + ", " + filePath);
}
}
}