Getting loading bar progress while loading using PhotonNetwork.LoadLevel

I was getting the progress before by creating an async task and getting the task.progress. This does not seem possible in PUN. I read online that I can use PhotonNetwork.LoadLevelAsync but the method is not showing and I cannot get it to work. Any thoughts? I just want to create a basic loading screen when changing scenes.

Comments

  • Hi @mcollins,

    PhotonNetwork.LoadLevelAsync(...); was first introduced in PUN version 1.90, so please make sure, that you at least use this version or a newer one (if there is one available in the future).

    If you are using a version which supports async scene loading, the function returns an AsyncOperation: AsyncOperation async = PhotonNetwork.LoadLevelAsync(...);. Having this AsyncOperation you can for example call async.progress to see its actual progress or async.isDone to check if it has finished loading the new scene.
  • mcollins
    mcollins
    edited May 2018
    Is there a certain library or class I need to inherit from? Because I tried that previously looking at one of your forum posts and the function does not exist according to my debugger. I asked the person who is working on networking on our team and he has assured me that our photon is the latest version.

    The script I'm trying to use that function in is inheriting from Photon.Monobehaviour.
  • Is there a certain library or class I need to inherit from?


    The only thing you need is using UnityEngine; because AsyncOperation is defined in that namespace. However this is added by default when creating a new script, so I guess you don't have to add it manually.

    You can also check the PUN version yourself. It can be either found in the PhotonNetwork class (variable is called versionPUN) or in the changelog which can be found in /Assets/Photon Unity Networking/.

    I'm adding a simple script here which can be added to a new scene in order to test things. You would only have to adjust which scene should actually be loaded. Please let me know, if this isn't working for you, too.
    using System.Collections;
    using UnityEngine;
    
    public class AsyncLoading : MonoBehaviour
    {
        private bool isLoading = false;
    
        public void OnGUI()
        {
            if (isLoading)
            {
                GUILayout.Label("Loading Level...");
            }
            else
            {
                if (GUILayout.Button("Load Level"))
                {
                    StartCoroutine(LoadLevelCoroutine());
                }
            }
        }
    
        public IEnumerator LoadLevelCoroutine()
        {
            isLoading = true;
    
            AsyncOperation async = PhotonNetwork.LoadLevelAsync(0);
    
            while (!async.isDone)
            {
                Debug.Log(async.progress);
    
                yield return new WaitForEndOfFrame();
            }
        }
    }
  • Thanks I will try this, how would you go about creating a loading screen for PhotonNetwork.JoinRoom()?
  • [Deleted User]
    edited May 2018
    Thanks I will try this, how would you go about creating a loading screen for PhotonNetwork.JoinRoom()?


    I honestly would not do this, because when you call PhotonNetwork.LoadLevelAsync(...); you disable the Message Queue and furthermore won't make any 'network progress', so calling PhotonNetwork.JoinRoom(...); directly before or while loading another scene may not work. Instead I would add an overlay to the screen telling the user that he currently joins a room (it's quite a fast operation). Additionally I would wait for the OnJoinedRoom() callback and start loading another scene from inside this callback.