- Home /
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.
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 :)
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
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);
}
}
Your answer
Follow this Question
Related Questions
Unity freezes when adding Task to List 1 Answer
Unity Editor Crash !!! 0 Answers
Difficulty publishing for android device 1 Answer