-
Notifications
You must be signed in to change notification settings - Fork 33
/
LocusBundleSample.cs
173 lines (147 loc) · 5.4 KB
/
LocusBundleSample.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Collections;
using UnityEngine;
using BundleSystem;
using System.Threading.Tasks;
public class LocusBundleSample : MonoBehaviour
{
bool m_DownloadCancelRequested = false;
IEnumerator Start()
{
//show log message
BundleManager.LogMessages = true;
//show some ongui elements for debugging
BundleManager.ShowDebugGUI = true;
//initialize bundle system & load local bundles
yield return BundleManager.Initialize();
//get download size from latest bundle manifest
var manifestReq = BundleManager.GetManifest();
yield return manifestReq;
if (!manifestReq.Succeeded)
{
//handle error
Debug.LogError(manifestReq.ErrorCode);
}
Debug.Log($"Need to download { BundleManager.GetDownloadSize(manifestReq.Result) * 0.000001f } mb");
//start downloading
var downloadReq = BundleManager.DownloadAssetBundles(manifestReq.Result);
while(!downloadReq.IsDone)
{
if(downloadReq.CurrentCount >= 0)
{
Debug.Log($"Current File {downloadReq.CurrentCount}/{downloadReq.TotalCount}, " +
$"Progress : {downloadReq.Progress * 100}%, " +
$"FromCache {downloadReq.CurrentlyLoadingFromCache}");
}
//if user requests cancel
if(m_DownloadCancelRequested) downloadReq.Cancel();
yield return null;
}
if(!downloadReq.Succeeded)
{
//handle error
Debug.LogError(downloadReq.ErrorCode);
}
//start to game
}
public void CancelDownload()
{
m_DownloadCancelRequested = true;
}
IEnumerator ApiSamples()
{
//Sync loading
{
var loaded = BundleManager.Load<Texture2D>("Texture", "TextureName");
//do something
BundleManager.ReleaseObject(loaded);
}
//Async loading
{
var loadReq = BundleManager.LoadAsync<Texture2D>("Texture", "TextureName");
yield return loadReq;
//do something
loadReq.Dispose();
}
//Asnyc loading with
{
//use using clause for easier release
using (var loadReq = BundleManager.LoadAsync<Texture2D>("Texture", "TextureName"))
{
yield return loadReq;
//do something
}
}
//Instantiate Sync
{
var loaded = BundleManager.Load<GameObject>("Prefab", "PrefabName");
//do something
var instance = BundleManager.Instantiate(loaded);
BundleManager.ReleaseObject(loaded);
}
//Instantiate Async with using clause(which is recommended, or just dispose request)
{
using (var loadReq = BundleManager.LoadAsync<GameObject>("Prefab", "PrefabName"))
{
yield return loadReq;
var instance = BundleManager.Instantiate(loadReq.Asset);
}
}
//load scene
{
//Sync
BundleManager.LoadScene("Scene", "SomeScene", UnityEngine.SceneManagement.LoadSceneMode.Single);
//Async
yield return BundleManager.LoadSceneAsync("Scene", "SomeScene", UnityEngine.SceneManagement.LoadSceneMode.Single);
}
}
async Task AsyncAwaitSamples()
{
//initialize with task aupport
{
//show log message
BundleManager.LogMessages = true;
//show some ongui elements for debugging
BundleManager.ShowDebugGUI = true;
//initialize bundle system & load local bundles
await BundleManager.Initialize();
//get download size from latest bundle manifest
var manifestReq = await BundleManager.GetManifest();
if (!manifestReq.Succeeded)
{
//handle error
Debug.LogError(manifestReq.ErrorCode);
}
//load asset with async/await
using (var loadReq = await BundleManager.LoadAsync<GameObject>("Prefab", "PrefabName"))
{
var instance = BundleManager.Instantiate(loadReq.Asset);
}
}
}
IEnumerator AdvancedApiSamples()
{
//load asset and assign release when gameobject is destroyed
{
//use using clause for easier release
Texture2D memberTexture;
using (var loadReq = BundleManager.LoadAsync<Texture2D>("Texture", "TextureName"))
{
yield return loadReq;
memberTexture = loadReq.Asset;
BundleManager.TrackObjectWithOwner(gameObject, memberTexture);
}
//here the texture will not be unloaded until gameobject destruction.
//so you don't have to care about it's release
//if we need to swap the texture
using (var loadReq = BundleManager.LoadAsync<Texture2D>("Texture", "TextureName2"))
{
yield return loadReq;
//cancel owner and untrack
BundleManager.UntrackObjectWithOwner(gameObject, memberTexture);
memberTexture = loadReq.Asset;
//re-track with owner with new asset
BundleManager.TrackObjectWithOwner(gameObject, memberTexture);
}
}
}
}