-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoadAddressableScene
More file actions
75 lines (65 loc) · 2.56 KB
/
LoadAddressableScene
File metadata and controls
75 lines (65 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
public class LoadAddressableScene : MonoBehaviour
{
[SerializeField] private AssetReference _scene;
//[SerializeField] private List<AssetReference> _references = new List<AssetReference>(); // In case of multiple scenes
private AsyncOperationHandle<SceneInstance> handle;
public GameObject camera;
public DownloadProgress downloadProgressScript; // So I can pass the progress someone else
public GameObject uiGameObject;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
private void Start()
{
StartCoroutine(DownloadScene());
}
IEnumerator DownloadScene()
{
var downloadScene = Addressables.LoadSceneAsync(_scene, LoadSceneMode.Additive);
downloadScene.Completed += SceneDownloadComplete;
Debug.LogError("Starting scene download");
while(!downloadScene.IsDone)
{
var status = downloadScene.GetDownloadStatus();
float progress = status.Percent; // Stores the current download progress
downloadProgressScript.downloadProgressInput = (int)(progress * 100);
yield return null;
}
Debug.LogError("Download Complete, starting next scene");
downloadProgressScript.downloadProgressInput = 100;
}
private void SceneDownloadComplete(AsyncOperationHandle<SceneInstance> _handle) // Ref to AsyncOperationHandle
{
if (_handle.Status == AsyncOperationStatus.Succeeded)
{
Debug.LogError(_handle.Result.Scene.name + " successfully loaded.");
camera.SetActive(false);
uiGameObject.SetActive(false);
handle = _handle;
StartCoroutine(UnloadScene());
}
}
private IEnumerator UnloadScene()
{
yield return new WaitForSeconds(10); // lets give the scene 10 seconds before unloading
Addressables.UnloadSceneAsync(handle, true).Completed += op =>
{
if (op.Status == AsyncOperationStatus.Succeeded)
{
camera.SetActive(true);
uiGameObject.SetActive(true);
Debug.LogError("Successfully unloaded the scene");
}
};
yield return new WaitForSeconds(5); // 5 seconds after unloading, load it back up again - for fun
StartCoroutine(DownloadScene());
}
}