Search Unity

get/set Label on Addressables at runtime?

Discussion in 'Addressables' started by jister, Apr 15, 2020.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I can't seem to find a way to get the label set on an addressable. There is AssetLabelReference which gives a drop down of all available Labels. docs shows is has a field
    Code (CSharp):
    1. public string labelString { get; set; }
    so can we some how use this to get or set Labels on addressables at runtime?
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ok silly me, you access it through an AssetReference...
     
  3. sujanmishra

    sujanmishra

    Joined:
    Mar 28, 2018
    Posts:
    3
    using System.Collections.Generic;
    using System.Linq;
    using UnityEditor.AddressableAssets.Settings;
    using UnityEngine;

    public class SetAddressableLabel
    {

    private void SetLabel(string label)
    {
    var settings = GetInstances<AddressableAssetSettings>("addressableAssetSettings");
    // if you haven't changed default name "addressableAssetSettings" , otherwise go find asset for AddressableAssetSettings under AddressableAssetData
    var get = settings.GetLabels();
    if (!get.Contains(label)) settings.AddLabel(label);
    }

    public T GetInstances<T>(string assetName) where T : ScriptableObject
    {
    // this method should find any scriptableObject of given type and given name , can reuse this method for other SO
    T asset = null;
    var assetList = GetAllInstances<T>().ToList();
    asset = assetList.First(o => o.name == assetName); // most likely we should get it here
    return asset != null ? asset : assetList.FirstOrDefault();
    }

    public T[] GetAllInstances<T>() where T : ScriptableObject
    {
    var assetList = Resources.FindObjectsOfTypeAll<T>();
    return assetList;
    }

    }
     
    Last edited: Jul 21, 2020