如果我打了一个WWW电话,它需要几秒钟的回复,不会游戏冻结? 不,游戏不会freez试试这个代码,你会看到日志“corou”后会立即显示图片下载。
public class WWWDemo : MonoBehaviour {
bool b = true;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update()
{
if (b) {
StartCoroutine(ExampleCoroutine());
Debug.LogWarning("after corou");
}
}
IEnumerator ExampleCoroutine()
{
b = false;
Debug.Log("Time : " + DateTime.Now);
string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
WWW www = new WWW(url);
yield return www;
Debug.Log("Time : " + DateTime.Now);
Renderer render = gameObject.GetComponent<Renderer>();
render.material.mainTexture = www.texture;
Debug.Log("Time : " + DateTime.Now);
}
}
但下一行的执行将产生回报WWW的语句将等待unitl图片下载后明确停止,而统一其余的事件将执行异步。
请分别考虑以下示例。开始事件执行它将等待,但更新将连续运行。
IEnumerator Start() {
Debug.Log("Time : " + DateTime.Now);
string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
WWW www = new WWW(url);
yield return www;
Debug.Log("Time : " + DateTime.Now);
Renderer render = gameObject.GetComponent<Renderer>();
render.material.mainTexture = www.texture;
Debug.Log("Time : " + DateTime.Now);
}
void Update()
{
Debug.Log("update is running while start is waiting for downloading");
}
从这个例子中,我猜测'yield return www;'行异步等待,直到下载页面。该例中的 –
它只是等待www请求结束(等待发生在“yield return www;”行)。 你也可以使用这个新的webrequest更多的控制:http://docs.unity3d.com/ScriptReference/Experimental.Networking.UnityWebRequest.html – mgear
它是如何知道它是完整的,如果返回是IEnumerator ...没有什么以“测试”完成 – Elenesski