Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by AxisRob · Feb 15, 2014 at 04:09 PM · networkingcrashfreezeasynclevelload

Switch Levels with Async and still display progress

I have a serious issue.

Before you read, please note. I am testing this by using a webplayer build interacting with the editor build and running both at the same time because I need to test a lot of networking code built into this thing. If that is the reason, please let me know.

The problem is that if I enable "LoadLevelAsync" at all, it just freezes and makes both the webplayer and the editor unresponsive. I don't know why. I also tried posting the async code outside of an enumerator, and still. Nothing.

 {
     public UILabel PercentageLabel;
 
     private Utilities utility;
     private PlayerSettings playerSettings;
     private AsyncOperation theGame;
     private bool CountIncremented = false;
 
     void Awake()
     {
         utility = GameObject.Find("LoadManager").GetComponent<Utilities>();
         theGrid = GameObject.Find("CharacterGrid");
         gridComponent = theGrid.GetComponent<UIGrid>();
         // Find Playersettings, set the NameLabelText, load the sprite
         playerSettings = GameObject.Find("LoadManager").GetComponent<PlayerSettings>();
 
         //StartCoroutine("LoadUpLevel");
     }
 
     void Update()
     {
         PercentageLabel.text = (((int)(theGame.progress * 100f)).ToString() + "%");
         if (theGame.isDone && !CountIncremented)
         {
             networkView.RPC("IncrementCount", RPCMode.Server);
             CountIncremented = true;
         }
         if (utility.ICanLoadTheLevel)
         {
             utility.ICanLoadTheLevel = false;
             CountIncremented = false;
             if (theGame.isDone)
             {
                 theGame.allowSceneActivation = true;
             }
         }
     }
 
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         int asyncprog = 0;
         if (stream.isWriting)
         {
             
             asyncprog = (int)(theGame.progress * 100f);
             stream.Serialize(ref Charname);
             stream.Serialize(ref isServer);
             //stream.Serialize(ref asyncprog);
         }
         else
         {
             stream.Serialize(ref asyncprog);
             //PercentageLabel.text = (asyncprog.ToString() + "%");
         }
     }
 
     [RPC]
     void IncrementCount()
     {
         //utility.PlayersFinishedLoading++;
         //if (utility.PlayersFinishedLoading >= utility.TotalPlayersInGame)
         //{
         //    utility.StartTheGame();//RPCs utility.ICanLoadTheLevel = true;
         //}
     }
 
     IEnumerator LoadUpLevel()
     {
         theGame = Application.LoadLevelAsync("MYGAMENAMEHERE");
         theGame.allowSceneActivation = false;
 
         yield return theGame;
     }
 }

Should I not be using an enumerator? Is this impossible, and if so, why?

I realize async is quite complicated, so thank you for your time.

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 MakeCodeNow · Feb 15, 2014 at 06:27 PM 0
Share

Async is always complicated, sadly. First off, it's strange that you are using a member variable (theGame) as the return of your LoadUpLevel. It seems that bad things could happen if that was called twice before the first run had finished.

What I would recommend is to keep LoadUpLevel as you have it, but make it a regular function. Then, in your update loop, just check the status of theGame and see if it's done or not. That's what I do in my async loading and it works great :)

avatar image AxisRob · Feb 16, 2014 at 12:05 PM 0
Share

But how do you get progress bar returns?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AxisRob · Feb 16, 2014 at 02:45 PM

I have the answer.

Basically, don't ever, ever do async operations in Awake. Ever.

I switched to start, and while it won't load beyond ~89%, it doesn't just freeze and die anymore

Comment
Add comment · 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
0

Answer by fholm · Feb 17, 2014 at 01:50 PM

This is the level loader I use for my FPS asset, or well an early version of it. It uses async loading, and first loads a "clear" scene which is empty, which wipes all other objects from the previous scene out, and then loads the next scene.

 using System;
 using UnityEngine;
 
 [FpsExecutionOrder(-9000)]
 public class FpsAppMapLoader : MonoBehaviour {
     FpsGameMap loadMap;
     FpsGameMode loadMode;
     AsyncOperation loadAsync;
     AsyncOperation unloadAsync;
 
     void Update () {
         if (unloadAsync == null) {
             FpsCallbacks.MapUnloadBegin();
             unloadAsync = Application.LoadLevelAsync("Clear");
 
         } else if (unloadAsync.isDone == false) {
             FpsCallbacks.MapUnloadProgress(unloadAsync.progress);
 
         } else {
             if (loadMap == null) {
                 UnloadDone();
                 Destroy(gameObject);
 
             } else {      
                 if (loadAsync == null) {
                     UnloadDone();
 
                     FpsCallbacks.MapLoadBegin(loadMap, loadMode); 
                     FpsCallbacks.MapLoadProgress(loadMap, loadMode, 0f); 
 
                     loadAsync = Application.LoadLevelAsync(loadMap.SceneId);
 
                 } else if (loadAsync.isDone == false) {
                     FpsCallbacks.MapLoadProgress(loadMap, loadMode, loadAsync.progress);
 
                 } else {
                     GC.Collect();
 
                     FpsCallbacks.MapLoadProgress(loadMap, loadMode, 1f);
                     FpsCallbacks.MapLoadDone(loadMap, loadMode);
 
                     Destroy(gameObject);
                 }
             }
         }
     }
 
     void UnloadDone () {
         FpsCallbacks.MapUnloadProgress(1f);
         FpsCallbacks.MapUnloadDone();
 
         GC.Collect();
     }
 
     void Start () {
         DontDestroyOnLoad(gameObject);
     }
 
     public static void LoadMap (FpsGameMap map, FpsGameMode mode) {
         var loader = FpsUtils.CreateObject<FpsAppMapLoader>();
         loader.loadMap = map;
         loader.loadMode = mode;
 
         FpsLog.Game("Loading map '{0}' ({1})", map.Name, mode.Name);
     }
 }
 
Comment
Add comment · 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

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

21 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

Related Questions

Unity freezes when adding Task to List 1 Answer

Monodevelop freezes on saving Javascript OR C#. Critical headdesk! 1 Answer

Unity 3d Network dies on Android 0 Answers

Are coroutines freezing my game? 0 Answers

Main Scene keeps becoming corrupted randomly. 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