AssetBundle加载技术

AssetBundle加载技术选型

AssetBundle加载有三套接口,WWWUnityWebRequestAssetBundle,推荐AssetBundle

前两者都要经历将整个文件的二进制流下载或读取到内存中,然后对这段内存文件进行ab资源的读取解析操作,而AssetBundle可以只读取存储于本地的ab文件的头部部分,在需要的情况下,读取ab中的数据段部分(Asset资源)。

所以AssetBundle相对的优势是

  • 不进行下载(不占用下载缓存区内存)

  • 不读取整个文件到内存(不占用原始文件二进制内存)

  • 读取非压缩或LZ4的ab,只读取ab的文件头(约5kb/个)

  • 同步异步加载并行可用

所以,从内存和效率方面,AssetBundle会是目前最优解,而使用非压缩或LZ4读者自己评断(推荐LZ4)

AssetBundle加载方式最重要的接口

  • AssetBundle.LoadFromFile 从本地文件同步加载ab
  • AssetBundle.LoadFromFileAsync 从本地文件异步加载ab
  • AssetBundle.Unload 卸载,注意true和false区别
  • AssetBundle.LoadAsset 从ab同步加载Asset
  • AssetBundle.LoadAssetAsync 从ab异步加载Asset

使用异步AssetBundle加载的时候

可以考虑用update每帧访问的方式替代协程的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

IEnumerator LoadAssetBundle()
{
yield return request;
//do something
}

转变为

void Update()
{
if(request.isDone)
{
//do something
}
}

设计

加载队列

1
2
3
4
private Dictionary<string, AssetBundleObject> _readyABList; //预备加载的列表
private Dictionary<string, AssetBundleObject> _loadingABList; //正在加载的列表
private Dictionary<string, AssetBundleObject> _loadedABList; //加载完成的列表
private Dictionary<string, AssetBundleObject> _unloadABList; //准备卸载的列表

外部接口

实现

加载依赖关系配置

一般在热更流程之后,游戏初始化前,进行游戏初始化的时候,加载的配置是unity导出AssetBundle时候生成的主Mainfest文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
_dependsDataList.Clear();
AssetBundle ab = AssetBundle.LoadFromFile(path);
AssetBundleManifest mainfest = ab.LoadAsset("AssetBundleManifest") as AssetBundleManifest;

foreach(string assetName in mainfest.GetAllAssetBundles())
{
string hashName = assetName.Replace(".ab", "");
string[] dps = mainfest.GetAllDependencies(assetName);
for (int i = 0; i < dps.Length; i++)
dps[i] = dps[i].Replace(".ab", "");
_dependsDataList.Add(hashName, dps);
}

ab.Unload(true); //用完要卸载
ab = null;

加载节点数据结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public delegate void AssetBundleLoadCallBack(AssetBundle ab);

private class AssetBundleObject
{
public string _hashName; //hash标识符

public int _refCount; //引用计数
public List<AssetBundleLoadCallBack> _callFunList = new List<AssetBundleLoadCallBack>(); //回调函数

public AssetBundleCreateRequest _request; //异步加载请求
public AssetBundle _ab; //加载到的ab

public int _dependLoadingCount; //依赖计数
public List<AssetBundleObject> _depends = new List<AssetBundleObject>(); //依赖项
}

依赖加载——递归&引用计数&队列&回调(重难点)

同步加载

实现Demo

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class AssetBundleLoadMgr
{
public delegate void AssetBundleLoadCallBack(AssetBundle ab);

private class AssetBundleObject
{
public string _hashName;

public int _refCount;
public List<AssetBundleLoadCallBack> _callFunList = new List<AssetBundleLoadCallBack>();

public AssetBundleCreateRequest _request;
public AssetBundle _ab;

public int _dependLoadingCount;
public List<AssetBundleObject> _depends = new List<AssetBundleObject>();
}

private static AssetBundleLoadMgr _Instance = null;

public static AssetBundleLoadMgr I
{
get {
if (_Instance == null) _Instance = new AssetBundleLoadMgr();
return _Instance;
}
}

private const int MAX_LOADING_COUNT = 10; //同时加载的最大数量

private List<AssetBundleObject> tempLoadeds = new List<AssetBundleObject>(); //创建临时存储变量,用于提升性能

private Dictionary<string, string[]> _dependsDataList;

private Dictionary<string, AssetBundleObject> _readyABList; //预备加载的列表
private Dictionary<string, AssetBundleObject> _loadingABList; //正在加载的列表
private Dictionary<string, AssetBundleObject> _loadedABList; //加载完成的列表
private Dictionary<string, AssetBundleObject> _unloadABList; //准备卸载的列表

private AssetBundleLoadMgr()
{
_dependsDataList = new Dictionary<string, string[]>();

_readyABList = new Dictionary<string, AssetBundleObject>();
_loadingABList = new Dictionary<string, AssetBundleObject>();
_loadedABList = new Dictionary<string, AssetBundleObject>();
_unloadABList = new Dictionary<string, AssetBundleObject>();

}

public void LoadMainfest()
{
string path = FileVersionMgr.I.GetFilePathByExist("Assets");
if (string.IsNullOrEmpty(path)) return;

_dependsDataList.Clear();
AssetBundle ab = AssetBundle.LoadFromFile(path);

if(ab == null)
{
string errormsg = string.Format("LoadMainfest ab NULL error !");
Debug.LogError(errormsg);
return;
}

AssetBundleManifest mainfest = ab.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
if (mainfest == null)
{
string errormsg = string.Format("LoadMainfest NULL error !");
Debug.LogError(errormsg);
return;
}

foreach(string assetName in mainfest.GetAllAssetBundles())
{
string hashName = assetName.Replace(".ab", "");
string[] dps = mainfest.GetAllDependencies(assetName);
for (int i = 0; i < dps.Length; i++)
dps[i] = dps[i].Replace(".ab", "");
_dependsDataList.Add(hashName, dps);
}

ab.Unload(true);
ab = null;

Debug.Log("AssetBundleLoadMgr dependsCount=" + _dependsDataList.Count);
}

private string GetHashName(string _assetName)
{//读者可以自己定义hash方式,对内存有要求的话,可以hash成uint(或uint64)节省内存
return _assetName.ToLower();
}

private string GetFileName(string _hashName)
{//读者可以自己实现自己的对应关系
return _hashName + ".ab";
}

// 获取一个资源的路径
private string GetAssetBundlePath(string _hashName)
{//读者可以自己实现的对应关系,笔者这里有多语言和文件版本的处理
string lngHashName = GetHashName(LocalizationMgr.I.GetAssetPrefix() + _hashName);
if (_dependsDataList.ContainsKey(lngHashName))
_hashName = lngHashName;

return FileVersionMgr.I.GetFilePath(GetFileName(_hashName));
}

public bool IsABExist(string _assetName)
{
string hashName = GetHashName(_assetName);
return _dependsDataList.ContainsKey(hashName);
}

//同步加载
public AssetBundle LoadSync(string _assetName)
{
string hashName = GetHashName(_assetName);
var abObj = LoadAssetBundleSync(hashName);
return abObj._ab;
}

//异步加载(已经加载直接回调),每次加载引用计数+1
public void LoadAsync(string _assetName, AssetBundleLoadCallBack callFun)
{
string hashName = GetHashName(_assetName);
LoadAssetBundleAsync(hashName, callFun);
}
//卸载(异步),每次卸载引用计数-1
public void Unload(string _assetName)
{
string hashName = GetHashName(_assetName);
UnloadAssetBundleAsync(hashName);
}

private AssetBundleObject LoadAssetBundleSync(string _hashName)
{
AssetBundleObject abObj = null;
if (_loadedABList.ContainsKey(_hashName)) //已经加载
{
abObj = _loadedABList[_hashName];
abObj._refCount++;

foreach (var dpObj in abObj._depends)
{
LoadAssetBundleSync(dpObj._hashName); //递归依赖项,附加引用计数
}

return abObj;
}
else if (_loadingABList.ContainsKey(_hashName)) //在加载中,异步改同步
{
abObj = _loadingABList[_hashName];
abObj._refCount++;

foreach(var dpObj in abObj._depends)
{
LoadAssetBundleSync(dpObj._hashName); //递归依赖项,加载完
}

DoLoadedCallFun(abObj, false); //强制完成,回调

return abObj;
}
else if (_readyABList.ContainsKey(_hashName)) //在准备加载中
{
abObj = _readyABList[_hashName];
abObj._refCount++;

foreach (var dpObj in abObj._depends)
{
LoadAssetBundleSync(dpObj._hashName); //递归依赖项,加载完
}

string path1 = GetAssetBundlePath(_hashName);
abObj._ab = AssetBundle.LoadFromFile(path1);

_readyABList.Remove(abObj._hashName);
_loadedABList.Add(abObj._hashName, abObj);

DoLoadedCallFun(abObj, false); //强制完成,回调

return abObj;
}

//创建一个加载
abObj = new AssetBundleObject();
abObj._hashName = _hashName;

abObj._refCount = 1;

string path = GetAssetBundlePath(_hashName);
abObj._ab = AssetBundle.LoadFromFile(path);

if(abObj._ab == null)
{
try
{
//同步下载解决
byte[] bytes = AssetsDownloadMgr.I.DownloadSync(GetFileName(abObj._hashName));
if (bytes != null && bytes.Length != 0)
abObj._ab = AssetBundle.LoadFromMemory(bytes);
}
catch (Exception ex)
{
Debug.LogError("LoadAssetBundleSync DownloadSync" + ex.Message);
}
}

//加载依赖项
string[] dependsData = null;
if (_dependsDataList.ContainsKey(_hashName))
{
dependsData = _dependsDataList[_hashName];
}

if (dependsData != null && dependsData.Length > 0)
{
abObj._dependLoadingCount = 0;

foreach (var dpAssetName in dependsData)
{
var dpObj = LoadAssetBundleSync(dpAssetName);

abObj._depends.Add(dpObj);
}

}

_loadedABList.Add(abObj._hashName, abObj);

return abObj;
}

private void UnloadAssetBundleAsync(string _hashName)
{
AssetBundleObject abObj = null;
if (_loadedABList.ContainsKey(_hashName))
abObj = _loadedABList[_hashName];
else if (_loadingABList.ContainsKey(_hashName))
abObj = _loadingABList[_hashName];
else if (_readyABList.ContainsKey(_hashName))
abObj = _readyABList[_hashName];

if (abObj == null)
{
string errormsg = string.Format("UnLoadAssetbundle error ! assetName:{0}",_hashName);
Debug.LogError(errormsg);
return;
}

if (abObj._refCount == 0)
{
string errormsg = string.Format("UnLoadAssetbundle refCount error ! assetName:{0}", _hashName);
Debug.LogError(errormsg);
return;
}

abObj._refCount--;

foreach (var dpObj in abObj._depends)
{
UnloadAssetBundleAsync(dpObj._hashName);
}

if (abObj._refCount == 0)
{
_unloadABList.Add(abObj._hashName, abObj);
}
}


private AssetBundleObject LoadAssetBundleAsync(string _hashName, AssetBundleLoadCallBack _callFun)
{
AssetBundleObject abObj = null;
if (_loadedABList.ContainsKey(_hashName)) //已经加载
{
abObj = _loadedABList[_hashName];
DoDependsRef(abObj);
_callFun(abObj._ab);
return abObj;
}
else if(_loadingABList.ContainsKey(_hashName)) //在加载中
{
abObj = _loadingABList[_hashName];
DoDependsRef(abObj);
abObj._callFunList.Add(_callFun);
return abObj;
}
else if (_readyABList.ContainsKey(_hashName)) //在准备加载中
{
abObj = _readyABList[_hashName];
DoDependsRef(abObj);
abObj._callFunList.Add(_callFun);
return abObj;
}

//创建一个加载
abObj = new AssetBundleObject();
abObj._hashName = _hashName;

abObj._refCount = 1;
abObj._callFunList.Add(_callFun);

//加载依赖项
string[] dependsData = null;
if (_dependsDataList.ContainsKey(_hashName))
{
dependsData = _dependsDataList[_hashName];
}

if (dependsData != null && dependsData.Length > 0)
{
abObj._dependLoadingCount = dependsData.Length;

foreach(var dpAssetName in dependsData)
{
var dpObj = LoadAssetBundleAsync(dpAssetName,
(AssetBundle _ab) =>
{
if(abObj._dependLoadingCount <= 0)
{
string errormsg = string.Format("LoadAssetbundle depend error ! assetName:{0}", _hashName);
Debug.LogError(errormsg);
return;
}

abObj._dependLoadingCount--;

//依赖加载完
if (abObj._dependLoadingCount == 0 && abObj._request != null && abObj._request.isDone)
{
DoLoadedCallFun(abObj);
}
}
);

abObj._depends.Add(dpObj);
}

}

if (_loadingABList.Count < MAX_LOADING_COUNT) //正在加载的数量不能超过上限
{
DoLoad(abObj);

_loadingABList.Add(_hashName, abObj);
}
else _readyABList.Add(_hashName, abObj);

return abObj;
}

private void DoDependsRef(AssetBundleObject abObj)
{
abObj._refCount++;

if (abObj._depends.Count == 0) return;
foreach (var dpObj in abObj._depends)
{
DoDependsRef(dpObj); //递归依赖项,加载完
}
}

private void DoLoad(AssetBundleObject abObj)
{
if (AssetsDownloadMgr.I.IsNeedDownload(GetFileName(abObj._hashName)))
{//这里是关联下载逻辑,可以实现异步下载再异步加载
AssetsDownloadMgr.I.DownloadAsync(GetFileName(abObj._hashName),
() =>
{
string path = GetAssetBundlePath(abObj._hashName);
abObj._request = AssetBundle.LoadFromFileAsync(path);

if (abObj._request == null)
{
string errormsg = string.Format("LoadAssetbundle path error ! assetName:{0}", abObj._hashName);
Debug.LogError(errormsg);
}
}
);
}
else
{
string path = GetAssetBundlePath(abObj._hashName);
abObj._request = AssetBundle.LoadFromFileAsync(path);

if (abObj._request == null)
{
string errormsg = string.Format("LoadAssetbundle path error ! assetName:{0}", abObj._hashName);
Debug.LogError(errormsg);
}
}

}

private void DoLoadedCallFun(AssetBundleObject abObj, bool isAsync = true)
{
//提取ab
if(abObj._request != null)
{
abObj._ab = abObj._request.assetBundle; //如果没加载完,会异步转同步
abObj._request = null;
_loadingABList.Remove(abObj._hashName);
_loadedABList.Add(abObj._hashName, abObj);
}

if (abObj._ab == null)
{
string errormsg = string.Format("LoadAssetbundle _ab null error ! assetName:{0}", abObj._hashName);
string path = GetAssetBundlePath(abObj._hashName);
errormsg += "\n File " + File.Exists(path) + " Exists " + path;

try
{//尝试读取二进制解决
if(File.Exists(path))
{
byte[] bytes = File.ReadAllBytes(path);
if (bytes != null && bytes.Length != 0)
abObj._ab = AssetBundle.LoadFromMemory(bytes);
}
}
catch (Exception ex)
{
Debug.LogError("LoadAssetbundle ReadAllBytes Error " + ex.Message);
}

if (abObj._ab == null)
{
//同步下载解决
byte[] bytes = AssetsDownloadMgr.I.DownloadSync(GetFileName(abObj._hashName));
if (bytes != null && bytes.Length != 0)
abObj._ab = AssetBundle.LoadFromMemory(bytes);

if (abObj._ab == null)
{//同步下载还不能解决,移除
if (_loadedABList.ContainsKey(abObj._hashName)) _loadedABList.Remove(abObj._hashName);
else if (_loadingABList.ContainsKey(abObj._hashName)) _loadingABList.Remove(abObj._hashName);

Debug.LogError(errormsg);

if (isAsync)
{//异步下载解决
AssetsDownloadMgr.I.AddDownloadSetFlag(GetFileName(abObj._hashName));
}
}
}
}

//运行回调
foreach (var callback in abObj._callFunList)
{
callback(abObj._ab);
}
abObj._callFunList.Clear();
}

private void UpdateLoad()
{
if (_loadingABList.Count == 0) return;
//检测加载完的
tempLoadeds.Clear();
foreach (var abObj in _loadingABList.Values)
{
if (abObj._dependLoadingCount == 0 && abObj._request != null && abObj._request.isDone)
{
tempLoadeds.Add(abObj);
}
}
//回调中有可能对_loadingABList进行操作,提取后回调
foreach (var abObj in tempLoadeds)
{
//加载完进行回调
DoLoadedCallFun(abObj);
}

}

private void DoUnload(AssetBundleObject abObj)
{
//这里用true,卸载Asset内存,实现指定卸载
if(abObj._ab == null)
{
string errormsg = string.Format("LoadAssetbundle DoUnload error ! assetName:{0}", abObj._hashName);
Debug.LogError(errormsg);
return;
}

abObj._ab.Unload(true);
abObj._ab = null;
}

private void UpdateUnLoad()
{
if (_unloadABList.Count == 0) return;

tempLoadeds.Clear();
foreach (var abObj in _unloadABList.Values)
{
if (abObj._refCount == 0 && abObj._ab != null)
{//引用计数为0并且已经加载完,没加载完等加载完销毁
DoUnload(abObj);
_loadedABList.Remove(abObj._hashName);

tempLoadeds.Add(abObj);
}

if (abObj._refCount > 0)
{//引用计数加回来(销毁又瞬间重新加载,不销毁,从销毁列表移除)
tempLoadeds.Add(abObj);
}
}

foreach(var abObj in tempLoadeds)
{
_unloadABList.Remove(abObj._hashName);
}
}

private void UpdateReady()
{
if (_readyABList.Count == 0) return;
if (_loadingABList.Count >= MAX_LOADING_COUNT) return;

tempLoadeds.Clear();
foreach (var abObj in _readyABList.Values)
{
DoLoad(abObj);

tempLoadeds.Add(abObj);
_loadingABList.Add(abObj._hashName, abObj);

if (_loadingABList.Count >= MAX_LOADING_COUNT) break;
}

foreach (var abObj in tempLoadeds)
{
_readyABList.Remove(abObj._hashName);
}
}

public void Update()
{
UpdateLoad();
UpdateReady();
UpdateUnLoad();
}
}