LevelAsyncLoader.cs 765 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. using System.Collections;
  3. public class LevelAsyncLoader : MonoBehaviour {
  4. private string loadName;
  5. private float delay = 0;
  6. private AsyncOperation async;
  7. private float startTime;
  8. private bool isLoading;
  9. public void init(string levelName, float delay=0)
  10. {
  11. this.loadName = levelName;
  12. this.delay = delay;
  13. }
  14. void Start ()
  15. {
  16. startTime = GameTime.time;
  17. }
  18. // Update is called once per frame
  19. void Update ()
  20. {
  21. if(!isLoading)
  22. {
  23. if(GameTime.time - startTime > delay)
  24. {
  25. isLoading = true;
  26. StartCoroutine(loadScene());
  27. }
  28. }
  29. else
  30. {
  31. Debuger.Log("Loading ("+loadName+") "+async.progress);
  32. }
  33. }
  34. IEnumerator loadScene()
  35. {
  36. async = Application.LoadLevelAsync(loadName);
  37. yield return async;
  38. }
  39. }