Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading Screen in Unity free

Discussion in 'Scripting' started by RingOfStorms, May 21, 2012.

  1. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    So I have Unity Free and have been exploring loading screens. It seems like you need to have the load two levels functions, which are pro only. But I did see a function called isLoadingLevel.

    So I wrote up this simple script that would simply draw a texture over the screen during hte loading time.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LoadingScreen : MonoBehaviour {
    6. private bool loading = true;
    7.  
    8. public Texture loadingTexture;
    9.  
    10.     void Update () {
    11.         if(Application.isLoadingLevel)
    12.             loading = true;
    13.         else
    14.             loading = false;
    15.     }
    16.    
    17.     void OnGUI () {
    18.         if(loading)
    19.             GUI.DrawTexture (new Rect(0,0,Screen.width,Screen.height), loadingTexture, ScaleMode.StretchToFill);
    20.     }
    21. }
    22.  
    23.  
    However, it doesn't seem to work. To test it I put a crap load of high poly models in one scene and had the camera face them. My computer even lagged in the editor, but when I press play I never get a load screen. Do I need more polys for it to even need to use the load screen or will the script just never work?
     
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Pressing play in the editor doesn't 'load' a scene.
     
  3. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    NVM I solved it using what this guy said:

    Just put it in your main menu screen and it will never go away, always loading before each level.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LoadingScreen : MonoBehaviour {
    6. private bool loading = true;
    7.  
    8. public Texture loadingTexture;
    9.  
    10.     void Awake () {
    11.         DontDestroyOnLoad(gameObject);
    12.     }
    13.  
    14.     void Update () {
    15.         if(Application.isLoadingLevel)
    16.             loading = true;
    17.         else
    18.             loading = false;
    19.     }
    20.    
    21.     void OnGUI () {
    22.         if(loading)
    23.             GUI.DrawTexture (new Rect(0,0,Screen.width,Screen.height), loadingTexture, ScaleMode.StretchToFill);
    24.     }
    25. }
    26.  
     
    Skylander17 likes this.
  4. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    And yes it does, it still has to lead a level each time. Now that I fixed my script it shows the level loading screen whihc is using the "Applicaiton.isLoadingLevel" <- which works in the editor.
     
  5. charlesvi

    charlesvi

    Joined:
    Jul 4, 2012
    Posts:
    8
    First off the script works and is cool so thanks for that. However It doesnt seem to work when I am establishing a network connection and then loading a level. To be specific

    Code (csharp):
    1.     void OnConnectedToServer()
    2.     {
    3.         Application.LoadLevel(levelToLoad);
    4.     }
    and it starts to fail. I'm honestly stumped any insight?
     
  6. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    What doesn't work, how does it fail? You need to be specific otherwise nobody will have a clue what could be wrong.
     
  7. charlesvi

    charlesvi

    Joined:
    Jul 4, 2012
    Posts:
    8
    Well it fails by simply not happening. I'm not sure what part of it does not work but When the above posted code runs I load the correct level it visibly takes time to load however the loading screen script does not trigger and no load screen is displayed.
     
  8. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Did you remember this part?:
    Code (csharp):
    1. DontDestroyOnLoad(gameObject);
    Its probably the most important part.
     
  9. charlesvi

    charlesvi

    Joined:
    Jul 4, 2012
    Posts:
    8
    Here is what I have done in regards to this. The load screen script and the script that is responsible for initiating the load.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class LoadingScreen : MonoBehaviour {
    7.  
    8. private bool loading = true;
    9.  
    10.  
    11.  
    12. public Texture loadingTexture;
    13.  
    14.  
    15.  
    16.     void Awake () {
    17.  
    18.         DontDestroyOnLoad(gameObject);
    19.        
    20.     }
    21.  
    22.  
    23.  
    24.     void Update () {
    25.  
    26.         if(Application.isLoadingLevel)
    27.  
    28.             loading = true;
    29.  
    30.         else
    31.  
    32.             loading = false;
    33.  
    34.     }
    35.  
    36.    
    37.  
    38.     void OnGUI () {
    39.  
    40.         if(loading)
    41.  
    42.             GUI.DrawTexture (new Rect(0,0,Screen.width,Screen.height), loadingTexture, ScaleMode.StretchToFill);
    43.  
    44.     }
    45.  
    46. }
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. /// <summary>
    6. /// Main menu GUI.
    7. ///
    8. /// This Script will be responsible for allowing our player to chose his gamemode.
    9. /// FindOpponent, Settings, Help, Exit. are the currently planned functions.
    10. /// This Menu Will be loaded immediatly after the loginscreen on start up.
    11. ///
    12. /// </summary>
    13.  
    14. public class MainMenuGUI : MonoBehaviour {
    15.    
    16.    
    17.     //Varibles Start --------------------------------------
    18.    
    19.     //These Varibles Will Define the button Layout of our GUI
    20.     public float buttonLeft = 10;
    21.     public float buttonTop = 300;
    22.     public float buttonSpace = 10;
    23.     public float buttonWidth = 400;
    24.     public float buttonHeight = 180;
    25.    
    26.     //This is used in the scale script to make the UI work for all resolutions.
    27.     float native_width = 1920.0f;
    28.     float native_height = 1080.0f;
    29.    
    30.     Rect fullScreen = new Rect(0,0,1920,1080);
    31.     public Texture2D backgroundImage;
    32.    
    33.    
    34.     GUIStyle menuStyle = new GUIStyle();
    35.  
    36.    
    37.    
    38.     //Varibles End ---------------------------------------
    39.     // Use this for initialization
    40.     void Start ()
    41.     {
    42.         menuStyle.fontSize = 20;
    43.  
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void Update () {
    48.    
    49.     }
    50.    
    51.     void OnGUI()
    52.     {
    53.         //set up scaling
    54.         float rx = Screen.width / native_width;
    55.         float ry = Screen.height / native_height;
    56.         Vector3 scale = new Vector3(rx, ry, 1);
    57.         GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, scale);
    58.    
    59.    
    60.         //now create your GUI normally, as if you were in your native resolution
    61.         //The GUI.matrix will scale everything automatically.
    62.        
    63.         GUI.DrawTexture(fullScreen, backgroundImage);
    64.        
    65.         if(GUI.Button(new Rect(buttonLeft, buttonTop, buttonWidth, buttonHeight), "Find Opponent"))
    66.         {
    67.             GameObject networkManager = GameObject.Find("NetworkManager");
    68.        
    69.             FindOpponent findOpponent = networkManager.GetComponent<FindOpponent>();
    70.            
    71.             findOpponent.iWantToPlay = true;
    72.            
    73.         }
    74.         if(GUI.Button(new Rect(buttonLeft, buttonTop+ (buttonSpace + buttonHeight), buttonWidth, buttonHeight), "Settings"))
    75.         {
    76.             print ("Do settings Stuff");
    77.         }
    78.         if(GUI.Button(new Rect (buttonLeft,  buttonTop+ (buttonSpace + buttonHeight)*2, buttonWidth, buttonHeight), "Learn to Play"))
    79.         {
    80.            
    81.         }
    82.         if(GUI.Button(new Rect(buttonLeft, buttonTop+ (buttonSpace + buttonHeight)*3, buttonWidth, buttonHeight), "Quit"))
    83.         {
    84.             Application.Quit();
    85.         }
    86.     }
    87. }
    88.  
     
  10. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    I don't see where you're calling Application.LoadLevel there. The problem is likely that you're relying on getting an Update() call while Application.isLoadingLevel is set, which probably won't happen in Free - it would only be the case if you were doing a Pro-only async load. Instead, just set the 'loading' variable directly, from the script that calls Application.LoadLevel - or make your loading screen call Application.LoadLevel itself for better encapsulation.
     
  11. charlesvi

    charlesvi

    Joined:
    Jul 4, 2012
    Posts:
    8
    I'm sorry your right. This script has the load part in it. my bad. I am just confused mostly because it worked right out of the box when I had it load from the main menu (before it set iWantToPlay to true) now it does not.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. /// <summary>
    4. /// Find opponent.
    5. /// This script will attempt to automaticly pair the player with another player so that they may play a match.
    6. /// </summary>
    7. public class FindOpponent : MonoBehaviour
    8. {
    9.     //Varibles Start ---------------------------------------------------------------------------------------
    10.     public string GUID = "";
    11.     public int msPort = 23466;
    12.     public string msIP = "127.0.0.1";
    13.     public string levelToLoad ="";
    14.    
    15.     //Toggle me when the find opponent button is pressed.
    16.     public bool iWantToPlay = false;
    17.     //Varibles End -----------------------------------------------------------------------------------------
    18.     // Use this for initialization
    19.     void Awake()
    20.     {
    21.  
    22.     }
    23.  
    24.    
    25.     void Start ()
    26.     {
    27.        
    28.  
    29.        
    30.    
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update ()
    35.     {
    36.         if(iWantToPlay == true)
    37.         {
    38.             Debug.Log ("It knows i want to play is true and its trying to connect.");
    39.            
    40.             MasterServer.port = msPort;
    41.             MasterServer.ipAddress = msIP;
    42.             Network.natFacilitatorIP = "127.0.0.1";
    43.             Network.natFacilitatorPort = 10001;
    44.        
    45.            
    46.             MasterServer.RequestHostList(GUID);
    47.             HostData[] data = MasterServer.PollHostList();
    48.             // Go through all the hosts in the host list to find a game with a open slot. then we will connect to the game.
    49.             foreach(HostData element in data)
    50.             {  
    51.                 //If server is not full. Connect!
    52.                 if(element.playerLimit != element.connectedPlayers)
    53.                 {
    54.                     Network.Connect(element);
    55.                     iWantToPlay = false;
    56.                     break;
    57.                 }
    58.             }
    59.             //todo
    60.             //If they dont find anyone at this point We are probally going to have to do something to make this loop?
    61.         }
    62.     }
    63.    
    64.     void OnConnected()
    65.     {
    66.        
    67.     }
    68.     void OnConnectedToServer()
    69.     {
    70.         Application.LoadLevel(levelToLoad);
    71.     }
    72. }
    73.  
     
  12. charlesvi

    charlesvi

    Joined:
    Jul 4, 2012
    Posts:
    8
    Thanks that fixed it. I guess I was just lucky to get a update before Load with the former script. right now I'm just setting the varible to loading and that works fine. If I wanted to encapsulate it like you suggested what would I do instead? Set a varible to true (iAmReadyToLoad) that would trigger the script to run like the loading varible and just have it load the level I want from inside?
     
  13. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    For encapsulation, I'd have a level-loader script permanently in the scene, that's usually idle. It can handle displaying the UI etc, and it should provide a public LoadLevel() method which you can call instead of calling Applicaiton.LoadLevel. Internally, this method would set the loading flag and call Application.LoadLevel itself. It's a small amount of encapsulation, but it keeps the setting of the loading flag together with the call to Application.LoadLevel, so you can't accidentally forget to do it, and it simplifies the calling code, which is always a good thing.
     
  14. pottyscotty

    pottyscotty

    Joined:
    Nov 22, 2013
    Posts:
    5
    Hi ring, your script works perfectly but where do I change the texture? :)
     
  15. RajaSekaran

    RajaSekaran

    Joined:
    Apr 4, 2014
    Posts:
    1
    hi ring,
    script work awesome. but on the loading time screen will be still hang. If any idea
     
  16. Skylander17

    Skylander17

    Joined:
    Jul 16, 2014
    Posts:
    4