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

How do you get a localized Sprite from AssetTableEntry?

Discussion in 'Localization Tools' started by cathode26, Sep 17, 2021.

  1. cathode26

    cathode26

    Joined:
    Jul 21, 2011
    Posts:
    27
    Example for getting a localized string:

    StringTableEntry tableEntry = currentStringTable[localizationID];
    string localizedString = tableEntry.GetLocalizedString(LocalizationSettings.SelectedLocale.Formatter);

    How do we get a localized Sprite?

    I tried to use the same style but instead of StringTableEntry I tried to use AssetTableEntry.
    How do you get the localized sprite from AssetTableEntry?

    Thanks!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Oh yes we don't have a method inside the entry here.
    The simplest way is to just use the AssetDatabase.
    LocalizationSettings.AssetDatabase.GetLocalizedAsset<Texture>("My Table", "My Entry:);


    You can also get the asset from the AssetTable using GetAssetAsync.

    Ill create a task to add support directly to the entry for the future.

    Also for the string you don't need to pass in the formatter, it will use the SelectedLocale formatter by default.
     
  3. cathode26

    cathode26

    Joined:
    Jul 21, 2011
    Posts:
    27

    Thanks!

    public IEnumerator SetLocalizedSprite(string localizationID, SpriteRenderer spriteRenderer)
    {
    AsyncOperationHandle<Sprite> spriteAsyncOp = currentAssetTable.GetAssetAsync<Sprite>((TableEntryReference)localizationID);
    yield return spriteAsyncOp;
    spriteRenderer.sprite = spriteAsyncOp.Result;​
    }
     
    karl_jones likes this.
  4. cathode26

    cathode26

    Joined:
    Jul 21, 2011
    Posts:
    27
    One more question I have is if I should be holding on to a reference of the AsyncOperationHandle while that Asset is in use?
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Thats fine to do but if you do plan to hold a reference then you should use Addressables.ResourceManager.Aquire so that it does not get unloaded and Release when you have finished holding on to the reference. Otherwise, the value may suddenly go null on you at some point when all references are released.