docs.unity3d.com
    Show / Hide Table of Contents

    Class LocalizedTable<TTable, TEntry>

    Provides a way to access a LocalizationTable at runtime. See LocalizedStringTable and LocalizedAssetTable for implementations.

    Inheritance
    Object
    LocalizedTable<TTable, TEntry>
    LocalizedAssetTable
    LocalizedStringTable
    Namespace: UnityEngine.Localization
    Syntax
    public abstract class LocalizedTable<TTable, TEntry> : object where TTable : DetailedLocalizationTable<TEntry> where TEntry : TableEntry
    Type Parameters
    Name Description
    TTable
    TEntry

    Properties

    CurrentLoadingOperation

    The current loading operation for the table when using TableChanged or null if one is not available.

    Declaration
    public AsyncOperationHandle<TTable>? CurrentLoadingOperation { get; }
    Property Value
    Type Description
    Nullable<AsyncOperationHandle<TTable>>

    Database

    Declaration
    protected abstract LocalizedDatabase<TTable, TEntry> Database { get; }
    Property Value
    Type Description
    LocalizedDatabase<TTable, TEntry>

    IsEmpty

    Does TableReference contain a valid reference?

    Declaration
    public bool IsEmpty { get; }
    Property Value
    Type Description
    Boolean

    TableReference

    Provides a reference to the LocalizationTable. A table reference can be either the name of the table or the table collection name Guid.

    Declaration
    public TableReference TableReference { get; set; }
    Property Value
    Type Description
    TableReference
    Remarks

    Note: Changing this value will trigger an update to any TableChanged subscribers.

    Methods

    ForceUpdate()

    Declaration
    protected void ForceUpdate()

    GetTable()

    Provides the table with the TableReference.

    Declaration
    public AsyncOperationHandle<TTable> GetTable()
    Returns
    Type Description
    AsyncOperationHandle<TTable>

    Returns the loading operation for the requested table.

    Remarks

    The event provides notification once the operation has finished and the table has been found or an error has occurred. A table may have already been loaded during a previous operation or when using Preload mode. Check the property to see if the table is already loaded and immediately available. See Async operation handling for further details.

    ToString()

    Returns a string representation including the TableReference.

    Declaration
    public override string ToString()
    Returns
    Type Description
    String

    Events

    TableChanged

    Provides a callback that will be invoked when the table is available or has changed.

    Declaration
    public event LocalizedTable<TTable, TEntry>.ChangeHandler TableChanged
    Event Type
    Type Description
    LocalizedTable.ChangeHandler<>
    Remarks

    The following events will trigger an update:

    • The first time the action is added to the event.
    • The SelectedLocale changing.
    • The TableReference changing.

    When the first LocalizedTable<TTable, TEntry>.ChangeHandler is added, a loading operation (see CurrentLoadingOperation) automatically starts. When the operation completes, the localized table is sent to the subscriber. If you add any additional subscribers added after loading has completed, they are also sent the latest localized table. This ensures that a subscriber will always have the correct localized value regardless of when it was added.

    Examples

    This example shows how the TableChanged event can be used to print out the contents of the table.

    using UnityEditor;
    using UnityEditor.Localization;
    using UnityEngine;
    using UnityEngine.Localization;
    using UnityEngine.Localization.Settings;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.UI;
    
    #region localized-string-events
    
    public class LocalizedStringWithEvents : MonoBehaviour
    {
    public LocalizedString myString;
    
    string localizedText;
    
    /// <summary>
    /// Register a ChangeHandler. This is called whenever the string needs to be updated.
    /// </summary>
    void OnEnable()
    {
       myString.StringChanged += UpdateString;
    }
    
    void OnDisable()
    {
       myString.StringChanged -= UpdateString;
    }
    
    void UpdateString(string s)
    {
       localizedText = s;
    }
    
    void OnGUI()
    {
       EditorGUILayout.LabelField(localizedText);
    }
    }
    #endregion
    
    #region localized-string-smart
    
    /// <summary>
    /// This example expects a Smart String with a named placeholder of `TimeNow`, such as "The time now is {TimeNow}".
    /// </summary>
    public class LocalizedStringSmart : MonoBehaviour
    {
    public LocalizedString myString;
    
    string localizedText;
    
    public float TimeNow => Time.time;
    
    /// <summary>
    /// Register a ChangeHandler. This is called whenever we need to update our string.
    /// </summary>
    void OnEnable()
    {
       myString.Arguments = new[] { this };
       myString.StringChanged += UpdateString;
    }
    
    void OnDisable()
    {
       myString.StringChanged -= UpdateString;
    }
    
    void UpdateString(string s)
    {
       localizedText = s;
    }
    
    void OnGUI()
    {
       // This calls UpdateString immediately (if the table is loaded) or when the table is available.
       myString.RefreshString();
       GUILayout.Label(localizedText);
    }
    }
    #endregion
    
    #region get-localized-string
    
    public class LocalizedStringGetExample : MonoBehaviour
    {
    public Text myText;
    
    public LocalizedString myString = new LocalizedString
    {
       TableReference = "My String Table",
       TableEntryReference = "My Game Text"
    };
    
    void OnEnable()
    {
       LocalizationSettings.SelectedLocaleChanged += LocaleChanged;
       LoadString();
    }
    
    void OnDisable()
    {
       LocalizationSettings.SelectedLocaleChanged -= LocaleChanged;
    }
    
    void LocaleChanged(Locale locale)
    {
       LoadString();
    }
    
    void LoadString()
    {
       var operation = myString.GetLocalizedString();
       UpdateString(operation);
    }
    
    void UpdateString(AsyncOperationHandle<string> value)
    {
       if (!value.IsDone)
       {
           // Defer the callback until the operation is finished
           value.Completed += UpdateString;
           return;
       }
    
       myText.text = value.Result;
    }
    }
    #endregion
    
    public class LocalizedStringConstructor
    {
    #region localized-string-constructor
    
    public LocalizedString usingNames = new LocalizedString("My String Table", "My Game Text");
    
    public LocalizedString usingTableNameAndEntryId = new LocalizedString("My String Table", 4324324541);
    
    public LocalizedString usingTableGuidAndEntryId = new LocalizedString(new System.Guid("6e79ded14bc9e0a4d9bf2b8aac246bfe"), 323453434);
    
    public LocalizedString usingTableGuidAndEntryName = new LocalizedString(new System.Guid("6e79ded14bc9e0a4d9bf2b8aac246bfe"), "Start Game");
    #endregion
    
    #region localized-string-constructor-editor
    
    public LocalizedString GenerateLocalizedStringInEditor()
    {
       // The main advantage to using a table Guid and entry Id is that references will not be lost when changes are made to the Table name or Entry name.
       var collection = LocalizationEditorSettings.GetStringTableCollection("My String Table");
       var entry = collection.SharedData.GetEntry("Start Game");
       return new LocalizedString(collection.SharedData.TableCollectionNameGuid, entry.Id);
    }
    
    #endregion
    
    void SetTableReferenceExamples()
    {
       #region localized-string-table-reference
    
       var localizedString = new LocalizedString();
    
       // Table Reference can be set with the name of the table
       localizedString.TableReference = "My String Table";
    
       // Or the Table Collection Guid
       localizedString.TableReference = new System.Guid("6e79ded14bc9e0a4d9bf2b8aac246bfe");
       #endregion
    }
    
    void SetTableEntryReferenceExamples()
    {
       #region localized-string-table-entry-reference
    
       var localizedString = new LocalizedString();
    
       // Table Entry Reference can be set with the name of the Key
       localizedString.TableEntryReference = "Exit Game";
    
       // Or the Key Id
       localizedString.TableEntryReference = 342423423;
       #endregion
    }
    
    void SetReferenceExamples()
    {
       #region localized-string-set-reference
    
       var localizedString = new LocalizedString();
    
       // Setting with names
       localizedString.SetReference("UI Text", "Options");
    
       // Setting with the table name and Key Id
       localizedString.SetReference("UI Text", 3432444324);
    
       // Setting with the Table Name Guid and Key name
       localizedString.SetReference(new System.Guid("6e79ded14bc9e0a4d9bf2b8aac246bfe"), "Options");
    
       // Setting with the Table Name Guid and the Key Id
       localizedString.SetReference(new System.Guid("6e79ded14bc9e0a4d9bf2b8aac246bfe"), 3432444324);
       #endregion
    }
    }

    Extension Methods

    TupleExtensions.IsValueTuple(Object)
    TupleExtensions.GetValueTupleItemObjects(Object)
    TupleExtensions.GetValueTupleItemObjectsFlattened(Object)
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023