From 702434d58e78f28702e952f9f35ee6eaaa2d4afb Mon Sep 17 00:00:00 2001 From: Junrou Nishida Date: Sun, 10 Oct 2021 11:02:12 +0900 Subject: [PATCH] fix: AssetBundle can be loaded only once (#309) * fix(sample): invalid AssetBundle path * fix(sdk): load AssetBundle only once --- .../Samples/Common/Scripts/Bootstrap.cs | 2 +- .../Util/AssetBundleResourceManager.cs | 25 ++++++------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/Assets/Mediapipe/Samples/Common/Scripts/Bootstrap.cs b/Assets/Mediapipe/Samples/Common/Scripts/Bootstrap.cs index 5f4f2cd8a..7e3049b39 100644 --- a/Assets/Mediapipe/Samples/Common/Scripts/Bootstrap.cs +++ b/Assets/Mediapipe/Samples/Common/Scripts/Bootstrap.cs @@ -66,7 +66,7 @@ private IEnumerator Start() { case AssetLoaderType.AssetBundle: { - AssetLoader.Provide(new AssetBundleResourceManager(Path.Combine(Application.streamingAssetsPath, "mediapipe"))); + AssetLoader.Provide(new AssetBundleResourceManager("mediapipe")); break; } case AssetLoaderType.StreamingAssets: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Util/AssetBundleResourceManager.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Util/AssetBundleResourceManager.cs index a54ee2e92..a5c0437b6 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Util/AssetBundleResourceManager.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Util/AssetBundleResourceManager.cs @@ -36,19 +36,9 @@ public override bool IsPrepared(string name) return File.Exists(path); } - private AssetBundle _assetBundle; - private AssetBundle assetBundle - { - get => _assetBundle; - set - { - if (_assetBundle != null) - { - _assetBundle.Unload(false); - } - _assetBundle = value; - } - } + + private AssetBundleCreateRequest _assetBundleReq; + private AssetBundle assetBundle => _assetBundleReq?.assetBundle; public void ClearAllCacheFiles() { @@ -66,15 +56,14 @@ public IEnumerator LoadAssetBundleAsync() yield break; } - var bundleLoadReq = AssetBundle.LoadFromFileAsync(_AssetBundlePath); - yield return bundleLoadReq; + // No need to lock because this code can be run in main thread only. + _assetBundleReq = AssetBundle.LoadFromFileAsync(_AssetBundlePath); + yield return _assetBundleReq; - if (bundleLoadReq.assetBundle == null) + if (_assetBundleReq.assetBundle == null) { throw new IOException($"Failed to load {_AssetBundlePath}"); } - - assetBundle = bundleLoadReq.assetBundle; } public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true)