Search Unity

Loader image load help

Discussion in 'Scripting' started by gringofxs, Jun 5, 2018.

  1. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    How can i put a simple loader to show the image download progress. Heres the script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LoadWWW : MonoBehaviour {
    7.  
    8.     public RawImage img;
    9.     public string url = "http://nfx3d.000webhostapp.com/ii.png";
    10.     void Start (){
    11.         StartCoroutine (LoadImageToUnity ());
    12.     }
    13.     public IEnumerator LoadImageToUnity()
    14.     {
    15.         WWW W = new WWW (url);
    16.         yield return W;
    17.         Texture2D te = W.texture;
    18.         img.texture = te;
    19.     }}
    20.  
    21.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Instead of just yielding on your www object with one yield statement, you can make a busy loop like so:

    Code (csharp):
    1. while( !W.isDone)
    2. {
    3.  fractionComplete = W.progress;
    4.  yield return null;
    5. }
    And then use that fractionComplete (a float from 0 to 1) to display a loading bar, etc.

    Actually, come to think of it you might just be able to access the W.progress value outside of your downloading coroutine but I usually like to marshal it out like the above.

    Plus, technically the WWW object implements the IDisposable interface so you should always use it within a using() block, or be sure to .Dispose it after completion. An example of a using() block can be found at the main docs page:

    https://docs.unity3d.com/ScriptReference/WWW.html
     
  3. gringofxs

    gringofxs

    Joined:
    Oct 14, 2012
    Posts:
    240
    1. Can u help me write the script.