- Home /
Texture loading via www.texture not loading!
Hi there,
i tried to load a image via WWW. therefor i created a IEnumerator and started loading with WWW(url).
to show loading progress, i created a progressbar with NGUI, and set a while loop in the ienum. this includes the setting of the progressbar to www.progress, and a yield that waits for the end of the frame.
problem... the www.progress always seems to be 0.0... so the image isn't loading :/ It´s working for my Audio Listener...
any ideas what can cause this problem?
The Code:
private WWW www;
[...]
void OnClick() {
Debug.Log("Button geklickt!");
//LADE AUDIOPLAYER
if(Buttonsprite.spriteName == "Image_Audiofile"){
Debug.Log("Button ist Audiobutton!");
StartCoroutine(GetAudiofile());
}
else if(Buttonsprite.spriteName == "Image_Imagefile"){
Debug.Log("Button ist Imagebutton!");
StartCoroutine(GetImagefile());
}
}
[...]
private string urlstring, cutstring;
private string[] urlarray;
IEnumerator GetImagefile(){
Viewer_Image.SetActive(true);
Viewer_Main.SetActive(false);
urlstring = ContentHolder.contents[(int)Ownfields.Arrayfield.x, (int)Ownfields.Arrayfield.y].cont_linkurl;
www = new WWW(urlstring);
if(www.error != null){
Debug.Log("Image WWW ERROR: " + www.error);
}
while(www.progress < 1){
Viewer_Image.transform.Find("StreamingBar").GetComponent<UISlider>().value = www.progress;
Viewer_Image.transform.Find("Ladestand").GetComponent<UILabel>().text = "Ladestand: " + ((int)(www.progress * 100.0f)).ToString() + "%";
Debug.Log(www.progress);
yield return new WaitForEndOfFrame();
}
Viewer_Image.transform.Find("StreamingBar").GetComponent<UISlider>().value = 1.0f;
Viewer_Image.transform.Find("Ladestand").GetComponent<UILabel>().text = "Ladestand: 100%";
ImagePlane.renderer.material.mainTexture = new Texture2D(www.texture.width,www.texture.height);
www.LoadImageIntoTexture((Texture2D)ImagePlane.renderer.material.mainTexture);
ImagePlane.transform.localScale = new Vector3(www.texture.width, www.texture.height, 1);
ImagePlane.SetActive(true);
}
Answer by scottharber · Oct 11, 2014 at 10:40 PM
I had a similar problem and just solved it.
The trick is to save your textures as PNG before you download them using WWW. Jpg doesn't download through iOS for some reason.
Answer by Andre Barbosa · Mar 24, 2014 at 04:40 PM
You should store the WWW var as a global var and update the progress bar on the Update() function. Something like the following:
private WWW www;
private int progress;
public string host;
public string uriVersion;
IEnumerator GetWWW () {
using(www = new WWW("http://" + host + "" + uriVersion)){
yield return www;
if (www.error != null){
Debug.Log("WWW download had an error:" + www.error);
}
else{
//Do whatever you want with the www var
}
}
}
// Use this for initialization
void Start () {
StartCoroutine(GetWWW());
}
void Update () {
//Store the progress as an integer value
progress = Mathf.RoundToInt(www.progress * 100f);
}
i pasted my code in the start post.. so you can see what i´ve done.
On your GetImagefile() function, www.progress is always going to be either 0 or 1.
You should check the www.progress on Update().
Your GetImagefile() function should be something like:
IEnumerator GetImagefile(){
using(www = new WWW(www = new WWW(urlstring)){
yield return www;
if(www.error != null){
Debug.Log("Image WWW ERROR: " + www.error);
}
else{
//Load Image
www.LoadImageIntoTexture((Texture2D)ImagePlane.renderer.material.mainTexture);
}
}
}
void Update(){
Viewer_Image.transform.Find("Strea$$anonymous$$gBar").GetComponent<UISlider>().value = www.progress;
Viewer_Image.transform.Find("Ladestand").GetComponent<UILabel>().text = "Ladestand: " + ((int)(www.progress * 100.0f)).ToString() + "%";
}
I haven't tested this (Some of it is your code really), but it should get you started
Same thing.. The image isn´t loading.. www.progress is always 0.000000f And so, the progress bar is always on a value of 0, and the Label is always showing "0%"
Any idea? It doesn´t matter if i use a update method or the while loop.. www2.progress always is 0
Interesting at this problem: $$anonymous$$y Audio-Player works the same way. With a while loop and the UISLider/UILabel.. And it´s working!!!!
Why not the image loader?