Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AssetReferenceT - how to cook it right?

Discussion in 'Addressables' started by palex-nx, Sep 30, 2019.

  1. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Hello!

    I'm trying to wrap my head around addressables system and what I want is to have and asset with name, descriptin, and icon, to reference real item prefab with 3d model, materials and all that stuff in asset bundle. This is needed to show to player list of items without requiring to download all bundles, only the choosen one.

    So far i've create a scriptable object ItemCard hanving all those properties:

    Code (CSharp):
    1. public class ItemCard : ScriptableObject
    2. {
    3.     public string name;
    4.     public string description;
    5.     public Sprite icon;
    6. }
    Now trying to add reference to the proper item prefab. For this I've created a script

    Code (CSharp):
    1. public class AssetReferenceItem : AssetReferenceT<ItemBehaviour>
    2. {
    3.     public AssetReferenceItem(string guid) : base(guid) {  }
    4. }
    And added a line to ItemCard script:

    Code (CSharp):
    1. public AssetReferenceItem prefab;
    But nothing shows up in the Inspector pane. When I change the line to

    Code (CSharp):
    1. public AssetReferenceGameObject prefab;
    the inspector shows a field for selecting a prefab, but it allows me to select any prefab. I want to filter the list and make it possible to select only prefabs with ItemBehaviour script attached.

    What I did wrong here?
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
    Added [System.Serializable], the prefab field will shows up.
    upload_2019-10-3_12-35-9.png

    But it won't work for two reasons.

    1) AssetReferenceT will filter addressables as below. TObject is a behavior and GetType returns GameObject, never matches.

    upload_2019-10-3_12-36-46.png

    2) TObject is passing to LoadAssetAsync<TObject>, behaviour is not supported type there. To load a behaviour, you need load game object first, then GetComponent<TObject> from it.

    I don't think of a good way to achieve what you want. Perhaps some hacky way to override all the methods below.

    upload_2019-10-3_12-50-54.png
     
    palex-nx likes this.
  3. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Thanks, now I got how it works. Definitely it needs some lightweight prefab filter mechanism because not all prefabs fits all references and there may be a lot of prefabs in the project.