docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Method SetAssetOverride

    SetAssetOverride<T>(T)

    Provides support for overriding the localized asset for the Entry. Note this is only temporary and will not persist in the Editor or if the table is reloaded. This allows for a table to be updated in the player. To remove the override, call ReleaseAsset(AssetTableEntry).

    Declaration
    public void SetAssetOverride<T>(T asset) where T : Object
    Parameters
    Type Name Description
    T asset

    The asset reference to use instead of Address.

    Type Parameters
    Name Description
    T

    The type to store the asset as locally.

    Examples

    This example shows how you can update the AssetTable entry values when the Locale is changed.

    public class UpdateAssetTableExample : MonoBehaviour
    {
        public LocalizedAssetTable myAssetTable = new LocalizedAssetTable("My Asset Table");
    
        public Texture englishTexture;
        public Texture frenchTexture;
    
        void OnEnable()
        {
            myAssetTable.TableChanged += UpdateTable;
        }
    
        void OnDisable()
        {
            myAssetTable.TableChanged -= UpdateTable;
        }
    
        Texture GetTextureForLocale(LocaleIdentifier localeIdentifier)
        {
            if (localeIdentifier.Code == "en")
                return englishTexture;
            else if (localeIdentifier == "fr")
                return frenchTexture;
            return null;
        }
    
        void UpdateTable(AssetTable value)
        {
            var entry = value.GetEntry("My Table Entry") ?? value.AddEntry("My Table Entry", string.Empty);
            entry.SetAssetOverride(GetTextureForLocale(value.LocaleIdentifier));
        }
    }

    This example shows how you can update all AssetTable entries at the start and ensure that the tables are never unloaded so that the changes are persistent throughtout the lifetime of the player.

    public class OverrideAllAssetTables : MonoBehaviour
    {
        public LocalizedAssetTable myAssetTable = new LocalizedAssetTable("My Asset Table");
    
        public Texture englishTexture;
        public Texture frenchTexture;
    
        IEnumerator Start()
        {
            yield return LocalizationSettings.InitializationOperation;
    
            foreach (var locale in LocalizationSettings.AvailableLocales.Locales)
            {
                var table = LocalizationSettings.AssetDatabase.GetTableAsync(myAssetTable.TableReference, locale);
    
                // Acquire a reference to the table. This will prevent the table from unloading until we have released it with Addressables.Release.
                Addressables.ResourceManager.Acquire(table);
    
                yield return table;
                UpdateTable(table.Result);
            }
        }
    
        Texture GetTextureForLocale(LocaleIdentifier localeIdentifier)
        {
            if (localeIdentifier.Code == "en")
                return englishTexture;
            else if (localeIdentifier == "fr")
                return frenchTexture;
            return null;
        }
    
        void UpdateTable(AssetTable value)
        {
            var entry = value.GetEntry("My Table Entry") ?? value.AddEntry("My Table Entry", string.Empty);
            entry.SetAssetOverride(GetTextureForLocale(value.LocaleIdentifier));
        }
    }
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)