How to LoadAssetsAsync as custom class (or component)

I am trying to migrate our project from Resources to Addressable system and encountered minor inconvenience that frustrates me immensely.

Resources.Load allows to load asset as any custom type (including inherited from), for example we were loading assets as BaseBullet, and each type had it`s own overrides of parent class.

Right now i am not able to run Addressables.LoadAssetsAsync with anything else, but .
I looked into examples provided, but the only place where they load as custom type is by using AssetReference (or rather custom ComponentReference), and it is not applicable for LoadAssetsAsync.

TLDR: How to make Addressables.LoadAssetsAsync(“label”) work?

Yes, Resources.Load did magic for you. It creates the gameobject from the prefab, leaves it on the scene and returns the MonoBehaviour for you.

To reference component, check this out - https://github.com/Unity-Technologies/Addressables-Sample/blob/master/Basic/ComponentReference/Assets/Scripts/ComponentReference.cs

And load custom class derived from ScriptableObject is natively supported.

1 Like

Can you elaborate what you mean it creates gameobject in scene? Because i have never observed that happening when i just load from Resources.

I did take a look at ComponentReference script, but two issues with it:

  1. Internally it calls GetComponent and still loads object as GameObject. In that case I can just do it in my asset-manager script, but I would like to avoid calling GetComponent on all of my prefabs.
  2. It is direct reference to single asset (Did I got this part wrong?). I would like to load multiple assets in one go based on Label, through LoadAssetsAsync method.

P.S. I just went through all documentation again and found this part:
You cannot load a GameObject’s component directly through Addressables. You must load or instantiate the GameObject, then retrieve the component reference from it.

So i guess my question is answered.

I was wrong. Resources.Load will not create the gameobject from the prefab. It loads the prefab into memory (you can retrieve it by comp.gameObject), and return the component from the prefab for you. So no matter how many times Resources.Load get invoked, it returns the same component instance (of the same prefab instance).

Pretty much same as LoadAssetAsync does for prefab.

Anyway, you get that idea that component can not be load independently, and underlying Unity always serialize component to be part of game object.

1 Like