gpt4 book ai didi

c# - Unity3d超时处理WWW

转载 作者:行者123 更新时间:2023-11-30 20:55:22 26 4
gpt4 key购买 nike

如果发生超时,我正在尝试处理 WWW 对象。我正在使用以下代码:

WWW localWWW;

void Start ()
{
stattTime = Time.time;

nextChange = Time.time + rotationSpeed;

StartCoroutine ("DownloadFile");

}

bool isStopped = false;
bool isDownloadStarted = false;
// Update is called once per frame
void Update ()
{ //2.0f as to simulate timeout
if (Time.time > stattTime + 2.0f && !isStopped) {
isStopped = true;
isDownloadStarted = false;
Debug.Log ("Downloading stopped");
StopCoroutine ("DownloadFile");
localWWW.Dispose ();

}
if (isDownloadStarted) {

}

if (Time.time > nextChange && isDownloadStarted) {
Debug.Log ("Current Progress: " + localWWW.progress);
nextChange = Time.time + rotationSpeed;
}
}

IEnumerator DownloadFile ()
{
isDownloadStarted = true;
GetWWW ();
Debug.Log ("Download started");
yield return (localWWW==null?null:localWWW);
Debug.Log ("Downlaod complete");
if (localWWW != null) {
if (string.IsNullOrEmpty (localWWW.error)) {
Debug.Log (localWWW.data);
}
}
}

public void GetWWW ()
{
localWWW = new WWW (@"http://www.sample.com");
}

但是我遇到了异常:

NullReferenceException: WWW class has already been disposed. TestScript+c__Iterator2.MoveNext ()

我不确定我在这里做错了什么。

有人可以帮我吗?

最佳答案

localWWW 永远不应为 null,因为 GetWWW 总是返回一个新实例。

以下代码片段虽然丑陋,但应该可以帮助您入门。

float elapsedTime = 0.0f;
float waitTime = 2.5f;
bool isDownloading = false;
WWW theWWW = null;
void Update () {
elapsedTime += Time.deltaTime;
if(elapsedTime >= waitTime && isDownloading){
StopCoroutine("Download");
theWWW.Dispose();
}
}

IEnumerator Download(string url){
elapsedTime = 0.0f;
isDownloading = true;

theWWW = new WWW(url);
yield return theWWW;

Debug.Log("Download finished");
}

关于c# - Unity3d超时处理WWW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18353821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com