Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading Unity3D:Don';在下载大型三维模型文件之前,请不要继续编写代码_Multithreading_Unity3d_Networking_Download_Coroutine - Fatal编程技术网

Multithreading Unity3D:Don';在下载大型三维模型文件之前,请不要继续编写代码

Multithreading Unity3D:Don';在下载大型三维模型文件之前,请不要继续编写代码,multithreading,unity3d,networking,download,coroutine,Multithreading,Unity3d,Networking,Download,Coroutine,点击一个按钮,我需要从服务器加载大约10个3D模型 public GameObject DownloadFile(String url) { GameObject obj = null; Debug.Log("url: " + url); string path = GetFilePath(url); Debug.Log("Path: " + path); if (File.Exists(path)) {

点击一个按钮,我需要从服务器加载大约10个3D模型

public GameObject DownloadFile(String url)
{
    GameObject obj = null;
    Debug.Log("url: " + url);

    string path = GetFilePath(url);
    Debug.Log("Path: " + path);

    if (File.Exists(path))
    {
        Debug.Log("Found file locally, loading...");
        Debug.Log(path);
        obj = LoadModel(path);

        return obj;
    }
    Debug.Log(path);


    StartCoroutine(GetFileRequest(url, (UnityWebRequest req) =>
    {
        if (req.isNetworkError || req.isHttpError)
        {
            // Log any errors that may happen
            Debug.Log($"{req.error} : {req.downloadHandler.text}");
        }
        else
        {
            // Save the model into a new wrapper
            obj = LoadModel(path);

            obj.SetActive(false);

            StopAllCoroutines();

        }
    }));



    return obj;
}


string GetFilePath(string url)
{
    filePath = $"{Application.persistentDataPath}/Files/";
    string[] pieces = url.Split('/');
    string filename = pieces[pieces.Length - 1];

    return $"{filePath}{filename}";
}



IEnumerator GetFileRequest(string url, Action<UnityWebRequest> callback)
{
    using (UnityWebRequest req = UnityWebRequest.Get(url))
    {
        req.downloadHandler = new DownloadHandlerFile(GetFilePath(url));
        yield return req.SendWebRequest();
        callback(req);
    }
}

GameObject LoadModel(string path)
{
    ResetWrapper();
    GameObject model = Importer.LoadFromFile(path);



    return model;
}
公共游戏对象下载文件(字符串url)
{
gameobjectobj=null;
Log(“url:+url”);
字符串路径=GetFilePath(url);
Log(“路径:“+Path”);
if(File.Exists(path))
{
Log(“在本地找到文件,正在加载…”);
Debug.Log(路径);
obj=负荷模型(路径);
返回obj;
}
Debug.Log(路径);
Start例程(GetFileRequest(url,(UnityWebRequest请求)=>
{
如果(请求isNetworkError | |请求ISHttPeror)
{
//记录可能发生的任何错误
Log($“{req.error}:{req.downloadHandler.text}”);
}
其他的
{
//将模型保存到新的包装器中
obj=负荷模型(路径);
obj.SetActive(假);
StopAllCoroutines();
}
}));
返回obj;
}
字符串GetFilePath(字符串url)
{
filePath=$“{Application.persistentDataPath}/Files/”;
string[]pieces=url.Split('/');
字符串文件名=个数[pieces.Length-1];
返回$“{filePath}{filename}”;
}
IEnumerator GetFileRequest(字符串url,操作回调)
{
使用(UnityWebRequest req=UnityWebRequest.Get(url))
{
req.downloadHandler=新的DownloadHandlerFile(GetFilePath(url));
产生返回请求SendWebRequest();
回调(req);
}
}
游戏对象加载模型(字符串路径)
{
ResetWrapper();
游戏对象模型=Importer.LoadFromFile(路径);
收益模型;
}

这是我现在的代码。我使用URL作为参数从脚本调用DownloadFile函数。下载函数返回一个空obj,因为下载一个大文件需要时间。因此它返回obj而不被下载。在下载完成之前,我怎么才能让它返回呢?

如果你愿意按照你的要求去做,你会完全冻结你的应用程序。。。如果这是您想要的,您当然可以不运行协同程序,只需执行以下操作

var-request=GetFileRequest(url,(UnityWebRequest请求)=>
{
如果(请求isNetworkError | |请求ISHttPeror)
{
//记录可能发生的任何错误
Log($“{req.error}:{req.downloadHandler.text}”);
}
其他的
{
//将模型保存到新的包装器中
obj=负荷模型(路径);
obj.SetActive(假);
StopAllCoroutines();
}
}
while(request.MoveNext())
{
//冻结
}
返回obj;
但在大多数情况下,这绝对不是您想要的;)


与其给它一个返回类型,我宁愿通过回调来解决这个问题:

public void下载文件(字符串url,Action onResult)
{
gameobjectobj=null;
Log(“url:+url”);
字符串路径=GetFilePath(url);
Log(“路径:“+Path”);
if(File.Exists(path))
{
Log(“在本地找到文件,正在加载…”);
Debug.Log(路径);
obj=负荷模型(路径);
onResult?调用(obj);
}
Debug.Log(路径);
Start例程(GetFileRequest(url,(UnityWebRequest请求)=>
{
如果(请求isNetworkError | |请求ISHttPeror)
{
//记录可能发生的任何错误
LogError($“{req.error}:{req.downloadHandler.text}”);
}
其他的
{
//将模型保存到新的包装器中
obj=负荷模型(路径);
obj.SetActive(假);
StopAllCoroutines();
onResult?调用(obj);
}
}));
}
因此,不要像这样做

var result=下载文件(url);
你宁愿这样做

下载文件(url,结果=>{ //与结果对象有什么关系 });