简体   繁体   English

从 Unity 的打开文件面板中选择文件后,UnityWebRequest 未完成

[英]UnityWebRequest is not completed after selecting a file from Open File Panel in Unity

string path;
AudioSource audio = GetComponent<AudioSource>();

path = EditorUtility.OpenFilePanel("Audio Files", "", "wav");
if (path != null)
{
    using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, AudioType.WAV))
        if (www.result == UnityWebRequest.Result.ConnectionError)
    {
        Debug.LogError(www.error);
    }
    else
        {
        //Debug.Log(www.url);
        audio.clip = DownloadHandlerAudioClip.GetContent(www);
        audio.Play();
            yield return www.SendWebRequest();
        }
}

after executing this block of code, on Play mode, A file picker runs successfully, however after selecting an audio file, the console throws the InvalidOperationException: Cannot get content from an unfinished UnityWebRequest object which caused by this line执行此代码块后,在播放模式下,文件选择器成功运行,但是在选择音频文件后,控制台抛出 InvalidOperationException: Cannot get content from an unfinished UnityWebRequest object 这是由该行引起的

audio.clip = DownloadHandlerAudioClip.GetContent(www);

my assumption is that I somehow missing a step in between getting the audio file path from the file picker and stream the actual audio clip using the path.我的假设是,我在从文件选择器获取音频文件路径和 stream 使用该路径的实际音频剪辑之间以某种方式错过了一步。

Debug.Log(www.url) will successfully print the file URI scheme. Debug.Log(www.url) 将成功打印文件 URI 方案。

In general watch out with your using .. you should wrap it in { } for readability and for preventing unexpected behavior一般来说,注意你的using .. 你应该将它包装在{ }中以提高可读性并防止意外行为

Then for some reason you do然后由于某种原因你这样做

yield return www.SendWebRequest();

after trying to access the results.... this line should be the first after the using line.. before trying to check whether the request was sent correctly and trying to access the download content尝试访问结果之后.. 此行应该是using行之后的第一行.. 在尝试检查请求是否正确发送并尝试访问下载内容之前

using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + path, AudioType.WAV))
{
    yield return www.SendWebRequest();

    if (www.result == UnityWebRequest.Result.ConnectionError)
    {
        Debug.LogError(www.error);
    }
    else
    {
        audio.clip = DownloadHandlerAudioClip.GetContent(www);
        audio.Play();           
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下载完成后打开文件 - Open file after download completed 从文件对话框中选择文件后,如何上传文件? - How to upload file just after selecting the file from the file dialog? 使用UnityWebRequest和php获取上传文件的链接 - Get the link of the uploaded file with UnityWebRequest and php 从统一C#将json字符串发布到webAPI; HttpClient还是UnityWebRequest? - POST json string to webAPI from unity C#; HttpClient or UnityWebRequest? 无法使用Unity中的UnityWebRequest从HTTP响应中获取数据 - Cannot get data from HTTP Response using UnityWebRequest in Unity 通过按顺序选择单个文件打开多个文件 - 从多个文件生成文件名 - Open multiple files by selecting single file in a sequence - Generate file name from multiple files c#在打开文件对话框中选择图像后获取图像目录路径 - c# get Image directory path after selecting image on open file dialog Unity WebGL UnityWebRequest数据处理 - Unity WebGL UnityWebRequest Data Handling 该操作无法完成,因为该文件已在 IIS 工作进程中打开 - The action can't be completed because the file is open in IIS Worker Process 在 HoloLens 2 上的 Unity 应用程序中打开文件浏览器 - Open File Browser in Unity App On HoloLens 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM