-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressableManager.cs
114 lines (97 loc) · 3.59 KB
/
AddressableManager.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Events;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
public class AddressableManager : MonoBehaviour
{
public static AddressableManager Instance;
public AssetReference sceneReference;
private AsyncOperationHandle<SceneInstance> sceneLoadHandle;
private float sizeToDownload = -1;
public static UnityEvent<float> ProgressEvent;
public static UnityEvent<bool> CompletionEvent;
private void Awake()
{
if (Instance == null)
Instance = this;
else
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
ProgressEvent = new UnityEvent<float>();
CompletionEvent = new UnityEvent<bool>();
// Addressables.ClearDependencyCacheAsync(sceneReference);
Addressables.CheckForCatalogUpdates();
StartCoroutine(GetDownloadSize());
}
public void OnPlayButtionClick()
{
if (IsAssetDownloaded())
{
StartCoroutine(LoadScene());
}
else
{
var data = new CommonPopup.PopupData(title: "DOWNLOAD", description: $"Download {sizeToDownload.ToString("0.##")} MB", null, "OK", () => DownloadScene(), "Cancel");
GameManager.Instance.commonPopup.PushPopup(data);
}
}
public IEnumerator GetDownloadSize()
{
AsyncOperationHandle<long> getDownloadSize = Addressables.GetDownloadSizeAsync(sceneReference);
yield return getDownloadSize;
sizeToDownload = getDownloadSize.Result / 1024f / 1024f;
Debug.Log("Download size in MB: " + sizeToDownload.ToString("0.##"));
}
public bool IsAssetDownloaded()
{
return sizeToDownload == 0;
}
public void DownloadScene()
{
StartCoroutine(DownloadAndCacheScene());
}
private IEnumerator DownloadAndCacheScene()
{
AsyncOperationHandle downloadHandle = Addressables.DownloadDependenciesAsync(sceneReference);
downloadHandle.Completed += (AsyncOperationHandle) =>
{
Debug.Log("Downloading asset complete");
StartCoroutine(GetDownloadSize());
};
while (downloadHandle.PercentComplete < 1 && !downloadHandle.IsDone)
{
var progress = downloadHandle.GetDownloadStatus().Percent;
Debug.Log("Downloading asset: " + progress.ToString());
ProgressEvent.Invoke(progress);
yield return null;
}
CompletionEvent.Invoke(downloadHandle.Status == AsyncOperationStatus.Succeeded);
Addressables.Release(downloadHandle);
}
public IEnumerator LoadScene()
{
sceneLoadHandle = Addressables.LoadSceneAsync(sceneReference, LoadSceneMode.Single);
sceneLoadHandle.Completed += OnSceneLoaded;
while (sceneLoadHandle.PercentComplete < 1 && !sceneLoadHandle.IsDone)
{
Debug.Log("Loading asset: " + sceneLoadHandle.GetDownloadStatus().Percent.ToString());
yield return null;
}
}
private void OnSceneLoaded(AsyncOperationHandle<SceneInstance> handle)
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
SceneInstance sceneInstance = handle.Result;
//SceneManager.LoadScene(sceneInstance.Scene.name);
}
else
{
Debug.LogError("Failed to load scene. Error: " + handle.OperationException.Message);
}
sceneLoadHandle.Completed -= OnSceneLoaded;
}
}