How to access texture/models from cloud/WEB at run time in android app using Unity 3D?

瘦欲@ 提交于 2019-12-11 11:22:56

问题


I have been trying to download and load the texture or other models from a WEB at run time by using WWW class. The app works well on Unity Editor but when I deploy it on android device it doesn't load/download the texture. Please somebody guide me. I have used the following code in script.

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;
}
}

回答1:


First of all, remove Debug.Log(www.texture); from the code, then re-try your code.

You have IEnumerator loadTexture() but you are calling StartCoroutine(loadMovie()); . This means that you are likely calling another function or maybe that's a type in your question.

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;
}

Not compiled but should work.

EDIT: From the file you sent, change the Minimum API Level to 4.1.



来源:https://stackoverflow.com/questions/36339575/how-to-access-texture-models-from-cloud-web-at-run-time-in-android-app-using-uni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!