问题
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