123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- public class Loader : MonoBehaviour
- {
- //How much of the loading animation is dedicated to the async level loading, the rest is for the loading of asset bundles
- const float LEVEL_LOADING_PERCENTAGE = 0.5f;
- void Start()
- {
- Application.backgroundLoadingPriority = ThreadPriority.Low;
- NotificationCenter.AddListener(OnStartFadeOut, NotificationType.LoadingStartFadeOut);
- NotificationCenter.AddListener(OnApplyingSkinProgress, NotificationType.ApplyingSkinProgress);
- NotificationCenter.AddListener(OnSkinApplied, NotificationType.SkinApplied);
- StartCoroutine(LoadLevel());
- }
- void OnDestroy()
- {
- NotificationCenter.RemoveListener(OnStartFadeOut, NotificationType.LoadingStartFadeOut);
- }
- IEnumerator LoadLevel()
- {
-
- yield return null;
- #if UNITY_EDITOR
- yield return new WaitForSeconds(1);
- NotificationCenter.Post(NotificationType.LoadingProgress, 0.25f);
- yield return new WaitForSeconds(1);
- NotificationCenter.Post(NotificationType.LoadingProgress, 0.45f);
- yield return new WaitForSeconds(1);
- NotificationCenter.Post(NotificationType.LoadingProgress, 0.65f);
- yield return new WaitForSeconds(1);
- NotificationCenter.Post(NotificationType.LoadingProgress, 0.85f);
- yield return new WaitForSeconds(1);
- NotificationCenter.Post(NotificationType.LoadingProgress, 1.0f);
- yield return SceneManager.LoadSceneAsync(1);
- #elif UNITY_WEBPLAYER
- while (Application.GetStreamProgressForLevel(1) != 1)
- {
- NotificationCenter.Post(NotificationType.LoadingProgress, Application.GetStreamProgressForLevel(1) * 0.4f); //40% for streaming the next level
- yield return null;
- }
- yield return StartCoroutine(PreDownloadDefaultSkin(0.4f, 0.4f)); //40% percent reserved for predownloading the default skin
- AsyncOperation asyncOp = SceneManager.LoadSceneAsync(1);
- while (!asyncOp.isDone)
- {
- NotificationCenter.Post(NotificationType.LoadingProgress, 0.4f + asyncOp.progress * 0.2f); //20% was for streaming the rest of the game
- yield return null;
- }
- #else
- AsyncOperation asyncOp = SceneManager.LoadSceneAsync(1);
- while (!asyncOp.isDone)
- {
- NotificationCenter.Post(NotificationType.LoadingProgress, asyncOp.progress * LEVEL_LOADING_PERCENTAGE);
- yield return null;
- }
- #endif
- }
- void OnApplyingSkinProgress(Notification note)
- {
- float skinProgress = (float)note.data;
- NotificationCenter.Post(NotificationType.LoadingProgress, LEVEL_LOADING_PERCENTAGE + skinProgress * (1 - LEVEL_LOADING_PERCENTAGE));
- }
- void OnSkinApplied(Notification note)
- {
- NotificationCenter.Post(NotificationType.LoadingStartFadeOut);
- }
- // IEnumerator PreDownloadDefaultSkin(float percentAlreadyDownloaded, float percentAllocatedForPreDownloading)
- // {
- // AVDebug.Log("Predownloading default backgrounds");
- // //backgrounds
- // string url = SkinDownloadManager.GetSubPartURL("default", "Backgrounds");
- //#if UNITY_EDITOR
- // using (WWW www = new WWW(url)) //don't use caching in editor
- //#else
- // using (WWW www = WWW.LoadFromCacheOrDownload(url, SkinDownloadManager.BUNDLES_VERSION))
- //#endif
- // {
- // while (!www.isDone)
- // {
- // NotificationCenter.Post(NotificationType.LoadingProgress, percentAlreadyDownloaded + www.progress * percentAllocatedForPreDownloading/2);
- // yield return null;
- // }
- // www.Dispose();
- // }
- // AVDebug.Log("Predownloading default atlas");
- // //atlas
- // url = SkinDownloadManager.GetSubPartURL("default", SkinDownloadManager.GetAtlasNameAccordingToResolution());
- //#if UNITY_EDITOR
- // using (WWW www = new WWW(url)) //don't use caching in editor
- //#else
- // using (WWW www = WWW.LoadFromCacheOrDownload(url, SkinDownloadManager.BUNDLES_VERSION))
- //#endif
- // {
- // while (!www.isDone)
- // {
- // NotificationCenter.Post(NotificationType.LoadingProgress, percentAlreadyDownloaded + www.progress * percentAllocatedForPreDownloading/2);
- // yield return null;
- // }
- // www.Dispose();
- // }
- // }
- void OnStartFadeOut(Notification note)
- {
- iTween.ValueTo(gameObject, iTween.Hash(
- "from", 1,
- "to", 0,
- "time", 1,
- "onupdate", "OniTweenFadeOutUpdate",
- "onupdatetarget", gameObject,
- "oncomplete", "OniTweenFadeOutComplete",
- "oncompletetarget", gameObject));
- }
- void OniTweenFadeOutUpdate(float newValue)
- {
- NotificationCenter.Post(NotificationType.LoadingFadeOutUpdate, newValue);
- }
- void OniTweenFadeOutComplete()
- {
- Destroy(GameObject.Find("LoadingBackgroundSpermPool"));
- Destroy(gameObject);
- NotificationCenter.Post(NotificationType.LoadingFadeOutComplete);
- }
- }
|