Skip to content

Commit ce40f37

Browse files
author
wanderer
committed
异步读取AssetBundle文件,增加文件锁定,检查游戏是否需要更新,不存在的游戏 , 也返回更新
1 parent 2b0b978 commit ce40f37

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

GameFramework/Runtime/Resource/BundleAssetsHelper.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public List<string> AllAssetPaths
4949
return _allAssetPaths;
5050
}
5151
}
52+
/// <summary>
53+
/// 预加载的所有资源
54+
/// </summary>
55+
private Dictionary<string, Object> _preloadAssets = new Dictionary<string, Object>();
56+
//异步加载锁定的文件
57+
private HashSet<string> _asyncLockFiles = new HashSet<string>();
5258

5359
/// <summary>
5460
/// 设置资源准备
@@ -84,10 +90,7 @@ public void SetResource(Action callback)
8490
StartCoroutine(LoadPlatformMainfest(_readPath, callback));
8591
}
8692

87-
/// <summary>
88-
/// 预加载的所有资源
89-
/// </summary>
90-
private Dictionary<string, Object> _preloadAssets = new Dictionary<string, Object>();
93+
9194

9295
/// <summary>
9396
/// 资源预加载
@@ -427,16 +430,19 @@ private IEnumerator LoadAssetBundleFromPathAsync(string path, Action<AssetBundle
427430
if (!File.Exists(path))
428431
throw new GameException("Assetbundle not found :" + path);
429432

430-
using (Stream abStream = _isEncrypt ? new EncryptFileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 4, false) : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 4, false))
433+
if (_asyncLockFiles.Contains(path))
431434
{
432-
while (!abStream.CanRead)
435+
string fileName = Path.GetFileNameWithoutExtension(path);
436+
while (!_liveAssetBundle.ContainsKey(fileName))
433437
{
434438
yield return null;
435439
}
436-
string fileName = Path.GetFileNameWithoutExtension(path);
437-
yield return null;
438-
AssetBundle assetBundle;
439-
if (!_liveAssetBundle.TryGetValue(fileName, out assetBundle))
440+
callback.Invoke(_liveAssetBundle[fileName]);
441+
}
442+
else
443+
{
444+
_asyncLockFiles.Add(path);
445+
using (Stream abStream = _isEncrypt ? new EncryptFileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 4, false) : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 4, false))
440446
{
441447
float loadTime = Time.realtimeSinceStartup;
442448
var ablfsa = AssetBundle.LoadFromStreamAsync(abStream);
@@ -448,9 +454,10 @@ private IEnumerator LoadAssetBundleFromPathAsync(string path, Action<AssetBundle
448454
yield return null;
449455
loadTime = Time.realtimeSinceStartup - loadTime;
450456
Debug.Log($"LoadAssetBundleFromPath Async time spent: {loadTime} {path} ");
451-
assetBundle = ablfsa.assetBundle;
457+
callback?.Invoke(ablfsa.assetBundle);
452458
}
453-
callback?.Invoke(assetBundle);
459+
yield return null;
460+
_asyncLockFiles.Remove(path);
454461
}
455462
}
456463
#endregion

GameFramework/Runtime/Resource/ResourceVersion.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public bool UpdateResource(Action<float, double, double, float> callback, Action
194194
/// 检查某一个资源是否需要更新
195195
/// </summary>
196196
/// <param name="name"></param>
197-
/// <param name="callback"></param>
197+
/// <param name="callback">[本地是否存在文件,是否需要更新]</param>
198198
public async void CheckResource(string name, Action<bool,bool> callback)
199199
{
200200

@@ -214,7 +214,8 @@ public async void CheckResource(string name, Action<bool,bool> callback)
214214
}
215215
else
216216
{
217-
throw new GameException($"There is no corresponding resource on the resource server: {name}");
217+
callback?.Invoke(false, true);
218+
//throw new GameException($"There is no corresponding resource on the resource server: {name}");
218219
}
219220
}
220221

0 commit comments

Comments
 (0)