Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug [Android] Addressable Load very slow Via AssetBundle.LoadFromFileAsync

Discussion in 'Addressables' started by florinel2102, Nov 30, 2022.

  1. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    75
    Why is loading local bundles on Android still so slow (like really slow) via AssetBundle.LoadFromFileAsync ? When I switch to load via UnityWebRequest ("Use UnityWebRequest for Local Asset Bundle" option from settings) the speed process is much faster than AssetBundle , but the problem is that the bundles are duplicated(https://docs.unity3d.com/Packages/com.unity.addressables@1.18/manual/LoadingAssetBundles.html), so in my case the game becomes almost twice in size .

    Also , why is this system seen as a replacement for the `old` Resource system when obviously has a major problem and it's not emphasized anywhere (I'm talking about Android) ?
     
  2. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    75
    Since there will be no fix for this anytime soon , I advise to everyone who has the same problem to move back to Resources and drop out completely if possible Addressable , but keep the flow to be ready when this bug is fixed , here it's quick code which wrap Resources and Addressable into one , so when it's ready you've just to modify the call .

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.AddressableAssets;
    4. using UnityEngine.ResourceManagement.AsyncOperations;
    5. using Object = UnityEngine.Object;
    6.  
    7. namespace General
    8. {
    9.     public static class ResourceBundle
    10.     {
    11.         public static BundleAsyncOp<T> ResourceLoad<T>(string key , bool isLoad) where T : Object
    12.         {
    13.             var op = Resources.LoadAsync<T>(key);
    14.  
    15.             return new BundleAsyncOp<T>(op);
    16.         }
    17.    
    18.         public static BundleAsyncOp<T> AddressableLoad<T>(string key) where T : Object
    19.         {
    20.             var op = Addressables.LoadAssetAsync<T>(key);
    21.        
    22.             return new BundleAsyncOp<T>(op , true);
    23.         }
    24.    
    25.         public static BundleAsyncOp<T> AddressableInstantiate<T>(string key) where T : Object
    26.         {
    27.             var op = Addressables.LoadAssetAsync<T>(key);
    28.        
    29.             return new BundleAsyncOp<T>(op , false);
    30.         }
    31.     }
    32.  
    33.     public class BundleAsyncOp<T> where T : Object
    34.     {
    35.         /// <summary>
    36.         /// Specific for addressable , IsLoad asset or instantiate gameobject
    37.         /// </summary>
    38.         private readonly bool IsLoad;
    39.         private readonly bool IsAddressable;
    40.    
    41.         private readonly ResourceRequest ResourceRequest;
    42.         private readonly AsyncOperationHandle<T> AddressableOperation;
    43.  
    44.         public Action<BundleAsyncOp<T>> Completed;
    45.  
    46.         public bool IsDone => IsAddressable ? AddressableOperation.IsDone : ResourceRequest.isDone;
    47.         public T Result
    48.         {
    49.             get
    50.             {
    51.                 if (IsAddressable) return AddressableOperation.Result;
    52.            
    53.                 return (T) ResourceRequest.asset;
    54.             }
    55.         }
    56.    
    57.         public BundleAsyncOp(AsyncOperationHandle<T> addressableOperation, bool isLoad)
    58.         {
    59.             IsAddressable = true;
    60.  
    61.             AddressableOperation = addressableOperation;
    62.             IsLoad = isLoad;
    63.         }
    64.    
    65.         public BundleAsyncOp( ResourceRequest resourceRequest)
    66.         {
    67.             IsAddressable = false;
    68.             IsLoad = false;
    69.  
    70.             ResourceRequest = resourceRequest;
    71.             ResourceRequest.completed += operation =>
    72.             {
    73.                 Completed?.Invoke(this);
    74.             };
    75.         }
    76.     }
    77. }