Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MarkOfVarberg · Jul 29, 2014 at 08:54 PM · loadingbackgroundasync

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;
     }
   }
 }









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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

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


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