Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ThaiCat · Jan 11, 2017 at 10:54 AM · c#unity 5multiplayerload scene

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bsozay · Jan 11, 2017 at 07:28 PM 0
Share

I have also the same problem. If someone knows the answer, i will be so glad.

avatar image ThaiCat bsozay · Jan 14, 2017 at 09:56 PM 0
Share

did you tried it similar problem?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrboembastic · Mar 30, 2018 at 11:17 PM 0
Share

Thanks for this! The bug workaround solved my problem.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

310 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges