我一直在尝试使用WWW类在运行时从WEB下载和加载纹理或其他模型。 该应用程序在Unity Editor上运行良好,但是当我在Android设备上部署它时,它不会加载/下载纹理。 请有人指导我。 我在脚本中使用了以下代码。
void Start()
{
StartCoroutine(loadMovie());
}
IEnumerator loadTexture()
{
WWW www = new WWW("http://images.earthcam.com/ec_metros/ourcams/fridays.jpg");
yield return www;
// Debug.Log(www.movie.duration + " <--- wwww");
if (www.error != null)
{
Debug.Log("Error: Can't load texture! - " + www.error);
yield break;
}
else
{
Texture2D texture = www.texture as Texture2D;
Debug.Log("Texture loaded");
Debug.Log(www.texture);
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
}
}
答案 0 :(得分:0)
首先,删除Debug.Log(www.texture);从代码中,然后重新尝试您的代码。
您有IEnumerator loadTexture()
但是您正在呼叫StartCoroutine(loadMovie());
。这意味着您可能正在调用另一个函数,或者可能是您问题中的类型。
void Start()
{
StartCoroutine(loadTexture());
}
IEnumerator loadTexture()
{
WWW www = new WWW("http://images.earthcam.com/ec_metros/ourcams/fridays.jpg");
//Wait for download to finish
while(!www.isDone){
Debug.Log("Progress: "+www.progress);
yield return null;
}
if ((www==null) || (www.error != null))
{
Debug.Log("Error: Can't load texture! - " + www.error);
yield break;
}
gameObject.GetComponent<Renderer>().material.mainTexture = www.texture;
}
未编译但应该有效。
修改强>: 从您发送的文件中,将Minimum API Level更改为4.1。