0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】AddressablesのDownloadStatusで「Exception: Attempting to use an invalid operation handle」が発生する原因

Last updated at Posted at 2025-01-14

該当コード

Addressables.DownloadDependenciesAsyncのロード進捗率を取得しようとしたらタイトルのエラーが出ました。

public async void Load() {
    AsyncOperationHandle downloadHandle;
    IList<IResourceLocation> locations = await Addressables.LoadResourceLocationsAsync("hoge");
    var locationGroups = locations.GroupBy(_ => _.DependencyHashCode);
    foreach(IGrouping<int, IResourceLocation> group in locationGroups) {
        downloadHandle = Addressables.DownloadDependenciesAsync(group.ToList(), true);
        while(downloadHandle.Status == AsyncOperationStatus.None) {
            var percentage = downloadHandle.GetDownloadStatus().Percent;
            Debug.Log(percentage);
        }
        Addressables.Release(downloadHandle);
    }
}

原因

上記コードの場合、DownloadDependenciesAsyncの第二引数(autoReleaseHandle)をtrueにして自動的に開放してしまっているため、ダウンロードの完了と同時にAsyncOperationHandleがnullになってしまう事が原因です。

単純にdownloadHandle.Statusの評価が出来ないことによりエラーが発生しています。

downloadHandle = Addressables.DownloadDependenciesAsync(group.ToList());

autoReleaseHandleを無効化すればエラーが解消されます。

終わりに

autoReleaseHandleを有効にしてダウンロード、かつDownloadStatusを監視していたため発生したエラーでした。

デフォルト値がfalseなので同じ症状が発生する人は少なそうですが、
意外と見落としがちなので同じ症状に困っている人に届けば幸いです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?