Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Displaying a progress bar using unityGUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by xemplifly, Apr 21, 2008.

  1. xemplifly

    xemplifly

    Joined:
    Nov 23, 2007
    Posts:
    27
    I'm curious how I would display a download progress bar to the user while my app is fetching an ogg sound file using unityGUI.

    Basically I am placing triggers in the world to allow the user to play a narration based on where he/she is.

    I've figured out how to display a unityGUI image to alert the user that there is a sound-byte available to play, require pressing a key to initialize the download, and finally play the file but I've not been able to display said loading bar.

    And for extra points, is there a reason why when i require pressing a key to begin downloading the ogg file it starts to play for a second and then starts to play again?
     
  2. mjjw

    mjjw

    Joined:
    Nov 12, 2005
    Posts:
    59
    For a progress bar you could simply use two GUI.DrawTextures e.g.

    Code (csharp):
    1.  
    2. var progressBackground : Texture;
    3. var progressForground : Texture;
    4.  
    5. function DrawProgress(location : Vector2, size : Vector2, progress : float)
    6. {
    7.     GUI.DrawTexture(Rect(location.x, location.y, size.x, size.y), progressBackground);
    8.     GUI.DrawTexture(Rect(location.x, location.y, size.x * progress, size.y), progressForground);
    9. }
    10.  
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    You can also just create a GUI.Box, set it's texture to your image, and set it's width as needed.

    -Jeremy
     
  4. xemplifly

    xemplifly

    Joined:
    Nov 23, 2007
    Posts:
    27
    This script is not rendering the textures. I think it's because i'm calling DrawProgress() from a function other than OnGUI directly. Here's a snipit of my code.

    Code (csharp):
    1.  
    2. function OnGUI () {
    3.     GUI.color.a = guiAlpha;
    4.     var imageWidth = NarrationPromptImg.width;
    5.     var imageHeight = NarrationPromptImg.height;
    6.    
    7.     var pLeft   = (Screen.width/2)  - (imageWidth/2);
    8.     var pTop    = (Screen.height/2) - (imageHeight/2);
    9.     var pRight  = pLeft + imageWidth;
    10.     var pBottom = pTop  + imageHeight;
    11.        
    12.     GUI.Label(Rect(pLeft,pTop,pRight,pBottom),NarrationPromptImg);
    13.     //GUI.Button(Rect(pLeft,pTop,pRight,pBottom),promptImage);
    14.        
    15.     if (Input.GetButton("Fire1")  isNarrationPrompt){
    16.         OnTriggerExit();
    17.         startAudio();
    18.     }
    19. }
    20.  
    21. function startAudio () {
    22.    
    23.     if(audio.isPlaying){
    24.         audio.Stop();
    25.     }
    26.    
    27.     // Start downloading
    28.     var download = new WWW (location+AudioFileName);
    29.    
    30.     // Show progress bar.
    31.     while(!download.isDone){
    32.         Debug.Log(download.progress);
    33.         DrawProgress(Vector2(0,0),Vector2(200,20),download.progress);
    34.         yield;
    35.     }
    36.        
    37.     // Wait for download to finish
    38.     //yield download;
    39.    
    40.     // Create ogg vorbis file
    41.     var clip : AudioClip = download.oggVorbis;
    42.     // Play it
    43.     if (clip != null){
    44.         audio.clip = clip;
    45.         audio.Play();
    46.     // Handle error
    47.     }else{
    48.         Debug.Log("Incorrect link? = ("+location+AudioFileName+")");
    49.     }
    50. }
    51.  
    52. function DrawProgress(location : Vector2, size : Vector2, progress : float)
    53. {
    54.     GUI.DrawTexture(Rect(location.x, location.y, size.x, size.y), progressBackground);
    55.     GUI.DrawTexture(Rect(location.x, location.y, size.x * progress, size.y), progressForground);
    56. }
    57.  
     
  5. mjjw

    mjjw

    Joined:
    Nov 12, 2005
    Posts:
    59
    Instad of calling DrawProgress from your audio function set a variable e.g.

    (very rough code - you'll have to make it work properly).
    Code (csharp):
    1.  
    2. public var drawProgress : boolean = false;
    3. public var progressAmount : float = 0.0;
    4.  
    5. function startAudio () {
    6. drawProgress = true;
    7. progressAmount = 0.1;
    8. }
    9.  
    10. function OnGUI()
    11. {
    12. if (drawProgress)
    13. {
    14.   DrawProgressBar(progressAmount);
    15. }
    16. }
    17.