Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- //using TMPro;
- using UnityEngine;
- using UnityEngine.AddressableAssets;
- using UnityEngine.Events;
- using UnityEngine.ResourceManagement;
- using UnityEngine.ResourceManagement.AsyncOperations;
- using UnityEngine.ResourceManagement.ResourceProviders;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class AddressableSceneLoader : MonoBehaviour
- {
- public string Scene = "NewScene";
- public LoadSceneMode loadMode = LoadSceneMode.Single;
- AsyncOperationHandle<SceneInstance> downloadSceneOperation;
- public UnityEvent OnStart;
- public EventFloat OnUpdate;
- public UnityEvent onSuccess;
- public UnityEventString onFalied;
- public TextMeshPro Status;
- public TextMeshProUGUI Progress;
- private Coroutine load = null;
- public void Load(string sceneName) {
- this.Scene = sceneName;
- }
- public void StartLoad()
- {
- OnStart.Invoke();
- if (downloadSceneOperation.IsValid())
- {
- downloadSceneOperation.Task.Dispose();
- }
- load = StartCoroutine("LoadRoutine");
- }
- private void OnDisable()
- {
- if (downloadSceneOperation.IsValid())
- {
- downloadSceneOperation.Task.Dispose();
- }
- }
- IEnumerator LoadRoutine()
- {
- yield return null;
- if (downloadSceneOperation.IsValid())
- {
- Debug.LogError("Download Operation is still in progress! Release it before repeating");
- yield break;
- }
- Debug.Log("Start downloading " + Scene);
- if (Status != null) {
- Status.text = "Start downloading " + Scene;
- }
- if (Progress != null) {
- Progress.text = "0%";
- }
- downloadSceneOperation = Addressables.LoadScene(Scene, loadMode);
- downloadSceneOperation.Completed += DownloadOperation_Completed;
- string info = "";
- while (downloadSceneOperation.IsValid() && !downloadSceneOperation.IsDone)
- {
- OnUpdate.Invoke(downloadSceneOperation.PercentComplete);
- info = "Downloading " + Scene + ": " + ((int)(downloadSceneOperation.PercentComplete * 100f)).ToString();
- if (Status != null && !string.IsNullOrEmpty(info))
- {
- Status.text = info;
- Debug.Log(info);
- }
- if (Progress != null && !string.IsNullOrEmpty(info))
- {
- Progress.text = ((int)(downloadSceneOperation.PercentComplete * 100f)) + "%";
- Debug.Log(((int)(downloadSceneOperation.PercentComplete * 100f)) + "%");
- }
- yield return null;
- }
- yield return null;
- }
- public void StopLoad() {
- }
- private void DownloadOperation_Completed(AsyncOperationHandle<SceneInstance> operation)
- {
- Debug.Log("downloadOperation " + operation.Status);
- if (operation.Status != AsyncOperationStatus.Succeeded)
- {
- var stat = "Downloading " + Scene + " FAILED! operation.Result: " + operation.Result.ToString() + " operation.Status: " + operation.Status.ToString();
- Debug.Log(stat);
- if (Status != null)
- {
- Status.text = stat;
- }
- onFalied.Invoke(stat);
- }
- else
- {
- Debug.Log("Downloading '" + Scene + "' " + operation.Status.ToString());
- if (Status != null)
- {
- Status.text = "Downloading '" + Scene + "' " + operation.Status.ToString();
- }
- onSuccess.Invoke();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement