我試圖在發生超時時處置WWW對象。我使用下面的代碼:如果在Unity3d中發生超時,請處理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類已被釋放。 TestScript + c__Iterator2.MoveNext()
我不知道我在做什麼錯在這裏。
有人可以幫我嗎?
爲什麼這會降低投票率?這是使用IDisposables的正確方法。 – arichards