Loader.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. public class Loader : MonoBehaviour
  5. {
  6. //How much of the loading animation is dedicated to the async level loading, the rest is for the loading of asset bundles
  7. const float LEVEL_LOADING_PERCENTAGE = 0.5f;
  8. void Start()
  9. {
  10. Application.backgroundLoadingPriority = ThreadPriority.Low;
  11. NotificationCenter.AddListener(OnStartFadeOut, NotificationType.LoadingStartFadeOut);
  12. NotificationCenter.AddListener(OnApplyingSkinProgress, NotificationType.ApplyingSkinProgress);
  13. NotificationCenter.AddListener(OnSkinApplied, NotificationType.SkinApplied);
  14. StartCoroutine(LoadLevel());
  15. }
  16. void OnDestroy()
  17. {
  18. NotificationCenter.RemoveListener(OnStartFadeOut, NotificationType.LoadingStartFadeOut);
  19. }
  20. IEnumerator LoadLevel()
  21. {
  22. yield return null;
  23. #if UNITY_EDITOR
  24. yield return new WaitForSeconds(1);
  25. NotificationCenter.Post(NotificationType.LoadingProgress, 0.25f);
  26. yield return new WaitForSeconds(1);
  27. NotificationCenter.Post(NotificationType.LoadingProgress, 0.45f);
  28. yield return new WaitForSeconds(1);
  29. NotificationCenter.Post(NotificationType.LoadingProgress, 0.65f);
  30. yield return new WaitForSeconds(1);
  31. NotificationCenter.Post(NotificationType.LoadingProgress, 0.85f);
  32. yield return new WaitForSeconds(1);
  33. NotificationCenter.Post(NotificationType.LoadingProgress, 1.0f);
  34. yield return SceneManager.LoadSceneAsync(1);
  35. #elif UNITY_WEBPLAYER
  36. while (Application.GetStreamProgressForLevel(1) != 1)
  37. {
  38. NotificationCenter.Post(NotificationType.LoadingProgress, Application.GetStreamProgressForLevel(1) * 0.4f); //40% for streaming the next level
  39. yield return null;
  40. }
  41. yield return StartCoroutine(PreDownloadDefaultSkin(0.4f, 0.4f)); //40% percent reserved for predownloading the default skin
  42. AsyncOperation asyncOp = SceneManager.LoadSceneAsync(1);
  43. while (!asyncOp.isDone)
  44. {
  45. NotificationCenter.Post(NotificationType.LoadingProgress, 0.4f + asyncOp.progress * 0.2f); //20% was for streaming the rest of the game
  46. yield return null;
  47. }
  48. #else
  49. AsyncOperation asyncOp = SceneManager.LoadSceneAsync(1);
  50. while (!asyncOp.isDone)
  51. {
  52. NotificationCenter.Post(NotificationType.LoadingProgress, asyncOp.progress * LEVEL_LOADING_PERCENTAGE);
  53. yield return null;
  54. }
  55. #endif
  56. }
  57. void OnApplyingSkinProgress(Notification note)
  58. {
  59. float skinProgress = (float)note.data;
  60. NotificationCenter.Post(NotificationType.LoadingProgress, LEVEL_LOADING_PERCENTAGE + skinProgress * (1 - LEVEL_LOADING_PERCENTAGE));
  61. }
  62. void OnSkinApplied(Notification note)
  63. {
  64. NotificationCenter.Post(NotificationType.LoadingStartFadeOut);
  65. }
  66. // IEnumerator PreDownloadDefaultSkin(float percentAlreadyDownloaded, float percentAllocatedForPreDownloading)
  67. // {
  68. // AVDebug.Log("Predownloading default backgrounds");
  69. // //backgrounds
  70. // string url = SkinDownloadManager.GetSubPartURL("default", "Backgrounds");
  71. //#if UNITY_EDITOR
  72. // using (WWW www = new WWW(url)) //don't use caching in editor
  73. //#else
  74. // using (WWW www = WWW.LoadFromCacheOrDownload(url, SkinDownloadManager.BUNDLES_VERSION))
  75. //#endif
  76. // {
  77. // while (!www.isDone)
  78. // {
  79. // NotificationCenter.Post(NotificationType.LoadingProgress, percentAlreadyDownloaded + www.progress * percentAllocatedForPreDownloading/2);
  80. // yield return null;
  81. // }
  82. // www.Dispose();
  83. // }
  84. // AVDebug.Log("Predownloading default atlas");
  85. // //atlas
  86. // url = SkinDownloadManager.GetSubPartURL("default", SkinDownloadManager.GetAtlasNameAccordingToResolution());
  87. //#if UNITY_EDITOR
  88. // using (WWW www = new WWW(url)) //don't use caching in editor
  89. //#else
  90. // using (WWW www = WWW.LoadFromCacheOrDownload(url, SkinDownloadManager.BUNDLES_VERSION))
  91. //#endif
  92. // {
  93. // while (!www.isDone)
  94. // {
  95. // NotificationCenter.Post(NotificationType.LoadingProgress, percentAlreadyDownloaded + www.progress * percentAllocatedForPreDownloading/2);
  96. // yield return null;
  97. // }
  98. // www.Dispose();
  99. // }
  100. // }
  101. void OnStartFadeOut(Notification note)
  102. {
  103. iTween.ValueTo(gameObject, iTween.Hash(
  104. "from", 1,
  105. "to", 0,
  106. "time", 1,
  107. "onupdate", "OniTweenFadeOutUpdate",
  108. "onupdatetarget", gameObject,
  109. "oncomplete", "OniTweenFadeOutComplete",
  110. "oncompletetarget", gameObject));
  111. }
  112. void OniTweenFadeOutUpdate(float newValue)
  113. {
  114. NotificationCenter.Post(NotificationType.LoadingFadeOutUpdate, newValue);
  115. }
  116. void OniTweenFadeOutComplete()
  117. {
  118. Destroy(GameObject.Find("LoadingBackgroundSpermPool"));
  119. Destroy(gameObject);
  120. NotificationCenter.Post(NotificationType.LoadingFadeOutComplete);
  121. }
  122. }