Search Unity

How to make a loading scene with canvas and slider

Discussion in 'Scripting' started by Deleted User, Jun 15, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hi all,

    im trying to make a loading scene, actually when the player click on play button on main menu screen, im showing a panel with a slider and want this slider behave like a progress bar. anyone has a simple tutorial or code to make this? thanks
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You'll have to use LoadLevelAsync to load it. LoadLevelAsync returns an AsyncOperation, which will have a progress variable you can keep an eye on over the next few seconds as it loads, and set your slider's percentage to.

    It'd probably look like this:
    Code (csharp):
    1. private AsyncOperation loadingOp = null;
    2. public void OnButtonPressed() {
    3. loadingOp = Application.LoadLevelAsync("YourLevel");
    4. }
    5.  
    6. public Slider slider;
    7. void Update() {
    8. if (loadingOp != null) {
    9. slider.gameObject.SetActive(true);
    10. slider.value = loadingOp.progress;
    11. }
    12. else {
    13. slider.gameObject.SetActive(false);
    14. }
    15. }
    One important note is that this won't appear to work when playing in the Editor. It's only in the build that your assets will be compiled such that asynchronous loading can happen; in the Editor LoadLevelAsync just behaves like LoadLevel.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    awsome! thanks so much! ill try that