Advertisement
dnnkeeper

AddressableSceneLoader for Unity

Apr 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. //using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.AddressableAssets;
  7. using UnityEngine.Events;
  8. using UnityEngine.ResourceManagement;
  9. using UnityEngine.ResourceManagement.AsyncOperations;
  10. using UnityEngine.ResourceManagement.ResourceProviders;
  11. using UnityEngine.SceneManagement;
  12. using UnityEngine.UI;
  13.  
  14. public class AddressableSceneLoader : MonoBehaviour
  15. {
  16.     public string Scene = "NewScene";
  17.  
  18.     public LoadSceneMode loadMode = LoadSceneMode.Single;
  19.  
  20.     AsyncOperationHandle<SceneInstance> downloadSceneOperation;
  21.  
  22.     public UnityEvent OnStart;
  23.  
  24.     public EventFloat OnUpdate;
  25.  
  26.     public UnityEvent onSuccess;
  27.  
  28.     public UnityEventString onFalied;
  29.  
  30.     public TextMeshPro Status;
  31.     public TextMeshProUGUI Progress;
  32.  
  33.     private Coroutine load = null;
  34.  
  35.     public void Load(string sceneName) {
  36.         this.Scene = sceneName;
  37.     }
  38.  
  39.     public void StartLoad()
  40.     {
  41.         OnStart.Invoke();
  42.         if (downloadSceneOperation.IsValid())
  43.         {
  44.             downloadSceneOperation.Task.Dispose();
  45.         }
  46.  
  47.         load = StartCoroutine("LoadRoutine");
  48.     }
  49.  
  50.     private void OnDisable()
  51.     {
  52.         if (downloadSceneOperation.IsValid())
  53.         {
  54.             downloadSceneOperation.Task.Dispose();
  55.         }
  56.     }
  57.  
  58.     IEnumerator LoadRoutine()
  59.     {
  60.         yield return null;
  61.  
  62.         if (downloadSceneOperation.IsValid())
  63.         {
  64.             Debug.LogError("Download Operation is still in progress! Release it before repeating");
  65.             yield break;
  66.         }
  67.  
  68.         Debug.Log("Start downloading " + Scene);
  69.         if (Status != null) {
  70.             Status.text = "Start downloading " + Scene;
  71.         }
  72.         if (Progress != null) {
  73.             Progress.text = "0%";
  74.         }
  75.  
  76.         downloadSceneOperation = Addressables.LoadScene(Scene, loadMode);
  77.         downloadSceneOperation.Completed += DownloadOperation_Completed;
  78.  
  79.         string info = "";
  80.         while (downloadSceneOperation.IsValid() && !downloadSceneOperation.IsDone)
  81.         {
  82.             OnUpdate.Invoke(downloadSceneOperation.PercentComplete);
  83.             info = "Downloading " + Scene + ": " + ((int)(downloadSceneOperation.PercentComplete * 100f)).ToString();
  84.  
  85.             if (Status != null && !string.IsNullOrEmpty(info))
  86.             {
  87.                 Status.text = info;
  88.                 Debug.Log(info);
  89.             }
  90.             if (Progress != null && !string.IsNullOrEmpty(info))
  91.             {
  92.                 Progress.text = ((int)(downloadSceneOperation.PercentComplete * 100f)) + "%";
  93.                 Debug.Log(((int)(downloadSceneOperation.PercentComplete * 100f)) + "%");
  94.             }
  95.            
  96.             yield return null;
  97.         }
  98.  
  99.         yield return null;
  100.     }
  101.  
  102.  
  103.  
  104.     public void StopLoad() {
  105.  
  106.     }
  107.  
  108.     private void DownloadOperation_Completed(AsyncOperationHandle<SceneInstance> operation)
  109.     {
  110.         Debug.Log("downloadOperation " + operation.Status);
  111.  
  112.         if (operation.Status != AsyncOperationStatus.Succeeded)
  113.         {
  114.             var stat = "Downloading " + Scene + " FAILED! operation.Result: " + operation.Result.ToString() + " operation.Status: " + operation.Status.ToString();
  115.             Debug.Log(stat);
  116.             if (Status != null)
  117.             {
  118.                 Status.text = stat;
  119.             }
  120.  
  121.             onFalied.Invoke(stat);
  122.         }
  123.         else
  124.         {
  125.  
  126.             Debug.Log("Downloading '" + Scene + "' " + operation.Status.ToString());
  127.             if (Status != null)
  128.             {
  129.                 Status.text = "Downloading '" + Scene + "' " + operation.Status.ToString();
  130.             }
  131.             onSuccess.Invoke();
  132.         }
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement