Unity Addressable Asset SystemでSystem.Exceptionの例外が発生して読み込みエラーが出る

実現したいこと

AWSのS3にBundleファイルをアップロード、CloudFrontをCDNとしてUnityからアセットをダウンロード

キャッシュがなければダウンロード

キャッシュを読み込んでAudioClipにセット、音を鳴らす

というプログラムを書きました。

発生している問題・分からないこと

キャッシュは正常にダウンロードされているようです。
CloudFrontの方をみてもリクエスト数が増えています。

このエラーが発生した後、アプリを閉じ、再び開くとちゃんと読み込めて音がなります。

参照するときにエラーが出ているのかな、、、、とは想いましたが改善方法がわかりません。

エラーメッセージ

error

1System.Exception: Unable to load dependent bundle from location Assets/Scenes/4.11_Vocalchop.mp3 2UnityEngine.AddressableAssets.Addressables:LoadAssetAsync<UnityEngine.AudioClip> (object) 3AddressableLoader:LoadBGM (string) (at Assets/Scripts/AddressableLoader.cs:105) 4AddressableLoader/<DownloadAndLoadBGM>d__9:MoveNext () (at Assets/Scripts/AddressableLoader.cs:93) 5UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr) 6 7 8Failed to load BGM: System.Exception: Unable to load dependent bundle from location Assets/Scenes/4.11_Vocalchop.mp3 9UnityEngine.Debug:LogError (object) 10AddressableLoader:<LoadBGM>b__10_0 (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<UnityEngine.AudioClip>) (at Assets/Scripts/AddressableLoader.cs:118) 11DelegateList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<UnityEngine.AudioClip>>:Invoke (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<UnityEngine.AudioClip>) (at ./Library/PackageCache/com.unity.addressables@1.21.21/Runtime/ResourceManager/Util/DelegateList.cs:75) 12UnityEngine.ResourceManagement.ResourceManager:Update (single)

該当のソースコード

C#

1using UnityEngine;2using UnityEngine.UI;3using UnityEngine.AddressableAssets;4using UnityEngine.ResourceManagement.AsyncOperations;5using System.Collections;6 7public class AddressableLoader : MonoBehaviour 8{9 public Text statusText;10 public Slider progressBar;11 public Text progressPercentageText;12 public Text downloadSpeedText;13 14 private long totalDownloadSize;15 private float previousDownloadSize;16 private float previousTime;17 18 void Start()19 {20 progressBar.gameObject.SetActive(false); // プログレスバーを非表示にする21 CheckAndDownloadBGM();22 }23 24 void CheckAndDownloadBGM()25 {26 string address = "VcChopOST"; // Addressablesの名前27 28 // キャッシュを確認29 Addressables.GetDownloadSizeAsync(address).Completed += handle =>30 {31 if (handle.Status == AsyncOperationStatus.Succeeded)32 {33 totalDownloadSize = handle.Result;34 if (totalDownloadSize > 0)35 {36 // ダウンロードが必要37 StartCoroutine(DownloadAndLoadBGM(address));38 }39 else40 {41 // キャッシュからロード42 statusText.text = "ロード準備";43 LoadBGM(address);44 }45 }46 else47 {48 string errorMessage = "Failed to get download size. Exception: " + handle.OperationException;49 Debug.LogError(errorMessage);50 statusText.text = "エラー: ダウンロードサイズの取得に失敗しました。 " + handle.OperationException;51 }52 };53 }54 55 IEnumerator DownloadAndLoadBGM(string address)56 {57 statusText.text = "データダウンロード中";58 progressBar.gameObject.SetActive(true); // プログレスバーを表示59 60 AsyncOperationHandle downloadHandle = Addressables.DownloadDependenciesAsync(address);61 62 previousDownloadSize = 0;63 previousTime = Time.time;64 65 while (!downloadHandle.IsDone)66 {67 float downloadProgress = downloadHandle.PercentComplete;68 progressBar.value = downloadProgress;69 progressPercentageText.text = $"{(downloadProgress * 100):F2}%";70 71 // ダウンロード速度計算72 float currentTime = Time.time;73 float currentDownloadSize = downloadProgress * totalDownloadSize;74 float deltaSize = currentDownloadSize - previousDownloadSize;75 float deltaTime = currentTime - previousTime;76 77 if (deltaTime > 0)78 {79 float downloadSpeed = deltaSize / deltaTime; // バイト/秒80 downloadSpeedText.text = $"{(downloadSpeed / 1024):F2} KB/s";81 }82 83 previousDownloadSize = currentDownloadSize;84 previousTime = currentTime;85 86 yield return null;87 }88 89 if (downloadHandle.Status == AsyncOperationStatus.Succeeded)90 {91 progressBar.gameObject.SetActive(false); // プログレスバーを非表示92 yield return new WaitForSeconds(1.0f);93 LoadBGM(address);94 }95 else96 {97 string errorMessage = "Failed to download dependencies. Exception: " + downloadHandle.OperationException;98 Debug.LogError(errorMessage);99 statusText.text = "エラー: ダウンロードに失敗しました。 " + downloadHandle.OperationException.Message;100 }101 }102 103 void LoadBGM(string address)104 {105 Addressables.LoadAssetAsync<AudioClip>(address).Completed += handle =>106 {107 if (handle.Status == AsyncOperationStatus.Succeeded)108 {109 statusText.text = "ロード完了";110 AudioClip bgmClip = handle.Result;111 AudioSource audioSource = gameObject.AddComponent<AudioSource>();112 audioSource.clip = bgmClip;113 audioSource.Play();114 }115 else116 {117 string errorMessage = $"Failed to load BGM: {handle.OperationException}";118 Debug.LogError(errorMessage);119 statusText.text = "エラー: ロードに失敗しました。 " + handle.OperationException.Message;120 }121 };122 }123}

試したこと・調べたこと

上記の詳細・結果

ネットで調べたり、ChatGPT等で聞いてみました。
しかし原因すらわからず改善はしませんでした。

再起動すると治るということはダウンロード直後は参照系のなにかしらが(内部的な仕組みを正直理解できてないです)Nullだったりするのかなと思いましたが
だからどうすればいいの。となっています。
1秒待つ処理も入れましたが意味はなかったようです。

補足

2022.3.21f1 LTSのUnityを使用しています。
Addressablesは1.21.21のバージョンです。

コメントを投稿

0 コメント