- Home /
Load scene in unet behind a loading screen
Currently i have a button, that calls LoadScene2() method, scene loads, loading screen works. Thats all on host.
The problem is that NetworkManager.singleton.StartHost() is called after scene fades in, and, if let's say StartHost() fails to start a server and load a scene, then i obviously get stuck on loading screen forever. The same applies to StartClient() if i put it there for clients, if there is nothing to connect to yet and such.
How do I handle it before starting a coroutine, to know if it possible to use loading screen or not? loading screen code:
public void LoadScene2()
{
StartCoroutine(LoadAsync(LoadingScreenAction.StartHost));
}
private IEnumerator LoadAsync(LoadingScreenAction action)
{
Application.backgroundLoadingPriority = ThreadPriority.BelowNormal;
WorldInit.SetStartProgress();
loadingScreen.SetActive(true);
loadingText.gameObject.SetActive(false);
progressBarSlider.gameObject.SetActive(false);
//fade in
overlayBackground.color = new Color(overlayBackground.color.r, overlayBackground.color.g, overlayBackground.color.b, 0f);
while (overlayBackground.color.a < 1f)
{
LoopFadeIn(fadeDuration, Time.deltaTime);
yield return new WaitForEndOfFrame();
}
loadingText.gameObject.SetActive(true);
progressBarSlider.gameObject.SetActive(true);
if (action == LoadingScreenAction.StartHost) { NetworkManager.singleton.StartHost(); } //NetworkManager loads the scene
float progress = 0f;
while (DoneLoading() == false) //WorldInit generates the scene
{
progress = Mathf.Abs(WorldInit.progressAsteroids / WorldInit.progressAsteroidsMax);
progressBarSlider.value = progress;
yield return new WaitForEndOfFrame();
}
loadingText.gameObject.SetActive(false);
progressBarSlider.gameObject.SetActive(false);
//fade out
while (overlayBackground.color.a > 0f)
{
LoopFadeIn(fadeDuration, -Time.deltaTime);
yield return new WaitForEndOfFrame();
}
loadingScreen.SetActive(false);
}
Any help?
I have also the same problem. If someone knows the answer, i will be so glad.
Answer by ThaiCat · Jan 19, 2017 at 03:16 PM
here is the solution:
overrride ServerChangeScene in networkManager class, make custom message to load a scene the way you want, and, the most important, to make a workaround a UNET bug.
the code:
void RegisterClientMessages2(NetworkClient client)
{
client.RegisterHandler(MsgType2.LoadingScreenMessage, OnClientLoadingScreen);
}
void OnClientLoadingScreen(NetworkMessage netMsg)
{
Debug.Log("OnClientLoadingScreen");
string sceneName = netMsg.reader.ReadString();
networkSceneName = sceneName;
if (SceneManager.GetActiveScene().name != sceneName && sceneName != "")
{
LoadingScreenManager.singleton.ActivateLoadingScreen();
LoadSceneAsync(sceneName);
}
}
void LoadSceneAsync(string newSceneName)
{
Debug.Log("LoadSceneAsync()");
loadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName);
StartCoroutine(LoadSceneAsyncCR());
}
IEnumerator LoadSceneAsyncCR()
{
Debug.Log("66666666666666666666666666666666666");
if (loadingSceneAsync == null)
yield return null;
if (!loadingSceneAsync.isDone)
yield return null;
loadingSceneAsync.allowSceneActivation = true;
loadingSceneAsync = null;
yield return new WaitForEndOfFrame(); //important! UNET bug workaround
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
FinishLoadScene2();
}
void FinishLoadScene2()
{
if (IsClientConnected() && client != null)
{
OnClientSceneChanged(client.connection);
}
if (NetworkServer.active)
{
{ Debug.Log("FinishLoadScene2 NetworkServer.SpawnObjects();"); }
NetworkServer.SpawnObjects();
OnServerSceneChanged(networkSceneName);
}
}
why the code without additional wait does not work in the base NetworkManager class method FinishLoadScene(), inside UpdateScene() is called from another class, and it seems it is called after some time since the scene is loaded. Without that time elapsed it is just not functional regardless of client scene ready state is set correctly, before or after NetworkServer.SpawnObjects() call.
Thanks for this! The bug workaround solved my problem.
Your answer
Follow this Question
Related Questions
Unity 5 multiplayer free look camera 0 Answers
UNET - Keep connection to client when changing scenes 1 Answer
what is wrong here ? 0 Answers
Re-hosting a Match in unity creates a bunch of random errors 1 Answer
Multiplayer Respawning Problems 0 Answers