Search Unity

Is there a way to set Asset Reference via code?

Discussion in 'Addressables' started by ChariotsGamingLanceG, May 15, 2020.

  1. ChariotsGamingLanceG

    ChariotsGamingLanceG

    Joined:
    Jun 27, 2019
    Posts:
    2
    Basically I'd like to assign all my asset references on runtime but it seems that I'm only able to set the reference via manual selecting it in the inspector asset reference drop down list. How do I retrieve only the asset reference from Addressables? I believe Loading won't be an option as I do not want to populate memory with assets that might not be used.

    I got till this point and am stuck. What's a GUI ID and how do i retrieve it from my list of Addressables? A sample code would help.
    Code (CSharp):
    1. AssetReference assetReference = new AssetReference("");
     
    Last edited: May 15, 2020
    MialeeMialee likes this.
  2. ChariotsGamingLanceG

    ChariotsGamingLanceG

    Joined:
    Jun 27, 2019
    Posts:
    2
    Does anyone have a solutions on this? I'll probably try this method but if anyone has a more efficient way of setting asset reference on runtime please do share.
    Code (CSharp):
    1. AssetDatabase.AssetPathToGUID("Assets/texture.jpg");
    Also I'm wondering if there's a way to loadassets from addressables without async as the one that seems to be included right now is deprecated. The one with async seems to be more of an inconvenience as I've to set delays till the actual asset is loaded into memory while if I use Resource.Load i get the assets loaded instantly.
     
    Last edited: May 17, 2020
  3. HouJay

    HouJay

    Joined:
    Sep 14, 2019
    Posts:
    1
    Hey, I've found it.
    The following code is to assign ar the Address1 asset, which Address1 is your asset address.
    Code (CSharp):
    1. AssetReference ar = new AssetReference("Address1");
    Code (CSharp):
    1. AssetReference ar = new AssetReference("Address1");