- Home /
Loading a level and some resources asynchronously
I'm using the new Pro feature Resources.LoadAsync() for loading resources asynchronously. This was added in the patch release 4.5.2p1. It doesn't work properly for me on iPhone 4 and I'm wondering if I'm doing something wrong? Any help would be appreciated!
I'm loading 36 textures from the resources folder. On iPhone 5 it works fine and this seems to be because the texture loading is faster and is done before the level loading is. On iPhone 4 the level loading gets to 90% before all of the textures are loaded (it gets to about 31-33 of the textures and then there is a garbage collection and then it never continues). It seems this is part of the problem but I don't know why the level loader is not returning control to the texture loading after 90% of the level is loaded? I yield return null as long as the materials are not ready.
In the Unity editor I had to bypass using the Resources.LoadAsync() altogether.
My code looks like this. The LevelManager class manages loading a scene asynchronously and while this is happening there is an animated loading scene being displayed. Also, at first start the GameManager class loads the required textures in the background and then when loaded updates the materialsAreLoaded field. This field is checked as part of the level loading because the textures are required:
LevelManager
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
public static void Load (string name) {
GameObject go = new GameObject ("LevelManager");
LevelManager instance = go.AddComponent<LevelManager> ();
instance.StartCoroutine (instance.InnerLoad (name));
}
IEnumerator InnerLoad (string name) {
//load transition scene
Object.DontDestroyOnLoad (this.gameObject);
Application.LoadLevelAsync ("Loading");
//wait one frame (for rendering, etc.)
yield return null;
//load the target scene
AsyncOperation asyncLevelLoader = Application.LoadLevelAsync (name);
asyncLevelLoader.allowSceneActivation = false;
while (asyncLevelLoader.progress < 0.9f || GameManager.Instance.materialsAreLoaded == false) {
yield return null;
}
asyncLevelLoader.allowSceneActivation = true;
while (!asyncLevelLoader.isDone) {
Debug.Log ("Waiting for done: " + asyncLevelLoader.progress);
yield return null;
}
Destroy (this.gameObject);
yield return asyncLevelLoader;
}
}
GameManager
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
private static GameManager instance;
public static GameManager Instance {
get {
return instance;
}
}
public float percentMaterialsLoaded = 0.0f;
public bool materialsAreLoaded = false;
public Material[] characterMaterials = null;
public int numberOfMaterials = 36;
void Awake () {
Debug.Log ("GameManager Awake()");
instance = this;
DontDestroyOnLoad (this);
StartCoroutine (LoadCharacters ());
}
IEnumerator LoadCharacters () {
Debug.Log ("GameManager starting to load character materials...");
if (!materialsAreLoaded) {
Debug.Log ("GameManager loading character materials...");
Shader shader = Shader.Find ("Spine/Skeleton");
List<Material> materials = new List<Material> ();
for (int i = 1; i < numberOfMaterials; i++) {
Debug.Log ("GameManager loading character materials " + i);
Material material = new Material (shader);
#if UNITY_EDITOR
Object texture = Resources.Load ("Characters/Textures/CostumePartyCharacters" + (i < 2 ? "" : "" + i));
material.mainTexture = texture as Texture2D;
#else
ResourceRequest resourceRequest = Resources.LoadAsync<Texture2D> ("Characters/Textures/CostumePartyCharacters" + (i < 2 ? "" : "" + i));
while (!resourceRequest.isDone) {
yield return 0;
}
material.mainTexture = resourceRequest.asset as Texture2D;
#endif
materials.Add (material);
percentMaterialsLoaded = i / numberOfMaterials;
}
Debug.Log ("GameManager loaded character materials...");
characterMaterials = materials.ToArray ();
materialsAreLoaded = true;
Debug.Log ("Materials array = " + characterMaterials.Length);
} else {
Debug.Log ("GameManager character materials already loaded...");
yield return null;
}
}
}
Your answer
Follow this Question
Related Questions
loading progress scene slows down game framerate 1 Answer
Additive async loading breaks my lightmapping on mobile devices 0 Answers
app freeze and threading 2 Answers
Unity scene starts loading after splash screen 3 Answers
Partial Async Loading 0 Answers