docs.unity3d.com
    Show / Hide Table of Contents

    Class GameObjectLocalizer

    The GameObject GameObject Localizer component is responsible for storing and applying all Localized Property Variants Configurations for the it is attached to.

    Inheritance
    Object
    GameObjectLocalizer
    Namespace: UnityEngine.Localization.PropertyVariants
    Syntax
    public class GameObjectLocalizer : MonoBehaviour
    Examples

    This shows how to configure a GameObjectLocalizer to apply changes to a Text component for the Font, Font Size and Text properties.

    public class SetupTextAndFont : MonoBehaviour
    {
    public Text text;
    
    void Start()
    {
        var localizer = gameObject.AddComponent<GameObjectLocalizer>();
    
        // Gets the Tracked text or creates a new tracker
        var trackedText = localizer.GetTrackedObject<TrackedUGuiGraphic>(text);
    
        // Gets the Property Variant for the text or creates a new one
        var textVariant = trackedText.GetTrackedProperty<LocalizedStringProperty>("m_Text");
    
        // The LocalizedString can be modified directly
        textVariant.LocalizedString.SetReference("My String Table", "My Entry");
        textVariant.LocalizedString.Arguments = new object[] {"Argument 1", "Argument 2"};
    
        // Set up the Font
        var fontVariant = trackedText.GetTrackedProperty<LocalizedAssetProperty>("m_FontData.m_Font");
        fontVariant.LocalizedObject = new LocalizedFont { TableReference = "My Assets", TableEntryReference = "My Font" };
    
        // Set up a default Font Size and an override size for French and Japanese. All other Locales will use the default Size.
        var fontSize = trackedText.GetTrackedProperty<IntTrackedProperty>("m_FontData.m_FontSize");
        fontSize.SetValue(LocalizationSettings.ProjectLocale.Identifier, 10); // Default Font Size
        fontSize.SetValue("ja", 12); // Japanese Font Size
        fontSize.SetValue("fr", 11); // French Font Size
    
        // Force an Update
        localizer.ApplyLocaleVariant(LocalizationSettings.SelectedLocale);
    }
    }

    This shows how to configure a GameObjectLocalizer to apply changes to a TextMeshProUGUI component for the Font, Font Size and Text properties.

    public class SetupTmpTextAndFont : MonoBehaviour
    {
    public TextMeshProUGUI text;
    
    void Start()
    {
        var localizer = gameObject.AddComponent<GameObjectLocalizer>();
    
        // Gets the Tracked text or creates a new tracker
        var trackedText = localizer.GetTrackedObject<TrackedUGuiGraphic>(text);
    
        // Gets the Property Variant for the text or creates a new one
        var textVariant = trackedText.GetTrackedProperty<LocalizedStringProperty>("m_text");
    
        // The LocalizedString can be modified directly
        textVariant.LocalizedString.SetReference("My String Table", "My Entry");
        textVariant.LocalizedString.Arguments = new object[] { "Argument 1", "Argument 2" };
    
        // Set up the Font
        var fontVariant = trackedText.GetTrackedProperty<LocalizedAssetProperty>("m_FontData.m_Font");
        fontVariant.LocalizedObject = new LocalizedAsset<TMP_FontAsset>() { TableReference = "My Assets", TableEntryReference = "My Font" };
    
        // Set up a default Font Size and an override size for French and Japanese. All other Locales will use the default Size.
        var fontSize = trackedText.GetTrackedProperty<IntTrackedProperty>("m_FontData.m_FontSize");
        fontSize.SetValue(LocalizationSettings.ProjectLocale.Identifier, 10); // Default Font Size
        fontSize.SetValue("ja", 12); // Japanese Font Size
        fontSize.SetValue("fr", 11); // French Font Size
    
        // Force an Update
        localizer.ApplyLocaleVariant(LocalizationSettings.SelectedLocale);
    }
    }

    This shows how to configure a GameObjectLocalizer to apply changes to a Dropdown for the options values.

    public class SetupDropdown : MonoBehaviour
    {
    public Dropdown dropdown;
    
    void Start()
    {
        var localizer = gameObject.AddComponent<GameObjectLocalizer>();
    
        // Gets the Tracked text or creates a new tracker
        var trackedDropdown = localizer.GetTrackedObject<TrackedUGuiDropdown>(dropdown);
    
        // Setup each option
        for (int i = 0; i < dropdown.options.Count; ++i)
        {
            var optionText = trackedDropdown.GetTrackedProperty<LocalizedStringProperty>($"m_Options.m_Options.Array.data[{i}].m_Text");
            optionText.LocalizedString.SetReference("My String Table", "My Option " + i);
        }
    
        // Force an Update
        localizer.ApplyLocaleVariant(LocalizationSettings.SelectedLocale);
    }
    }

    This shows how to configure a GameObjectLocalizer to apply changes to a TMP_Dropdown for the options values.

    public class SetupTmpDropdown : MonoBehaviour
    {
    public TMP_Dropdown dropdown;
    
    void Start()
    {
        var localizer = gameObject.AddComponent<GameObjectLocalizer>();
    
        // Gets the Tracked text or creates a new tracker
        var trackedDropdown = localizer.GetTrackedObject<TrackedTmpDropdown>(dropdown);
    
        // Setup each option
        for (int i = 0; i < dropdown.options.Count; ++i)
        {
            var optionText = trackedDropdown.GetTrackedProperty<LocalizedStringProperty>($"m_Options.m_Options.Array.data[{i}].m_Text");
            optionText.LocalizedString.SetReference("My String Table", "My Option " + i);
        }
    
        // Force an Update
        localizer.ApplyLocaleVariant(LocalizationSettings.SelectedLocale);
    }
    }

    This shows how to configure a GameObjectLocalizer to apply changes to a for the x, y and width properties. This can be useful when you need to make adjustments due to changes in text length for a particular Locale.

    public class SetupRectTransform : MonoBehaviour
    {
    void Start()
    {
        var localizer = gameObject.AddComponent<GameObjectLocalizer>();
    
        // Gets the Tracked text or creates a new tracker
        var trackedText = localizer.GetTrackedObject<TrackedRectTransform>(transform);
    
        // Gets the Property Variant for the x, y and width
        var xPos = trackedText.GetTrackedProperty<FloatTrackedProperty>("m_AnchoredPosition.x");
        var yPos = trackedText.GetTrackedProperty<FloatTrackedProperty>("m_AnchoredPosition.y");
        var width = trackedText.GetTrackedProperty<FloatTrackedProperty>("m_SizeDelta.x");
    
        xPos.SetValue(LocalizationSettings.ProjectLocale.Identifier, 0); // Default is 0
        xPos.SetValue("ja", 5); // Override for Japanese
    
        yPos.SetValue(LocalizationSettings.ProjectLocale.Identifier, 10); // Default is 10
        yPos.SetValue("fr", 5); // Override for French
    
        width.SetValue(LocalizationSettings.ProjectLocale.Identifier, 100); // Default is 100
        width.SetValue("ja", 50); // Japanese requires less space
        width.SetValue("fr", 150); // French requires more space
    
        // Force an Update
        localizer.ApplyLocaleVariant(LocalizationSettings.SelectedLocale);
    }
    }

    This shows how to configure a GameObjectLocalizer to apply changes to a custom script.

    public class MyScript : MonoBehaviour
    {
    public string myText;
    public Color textColor;
    
    void OnGUI()
    {
        GUI.color = textColor;
        GUILayout.Label(myText);
    }
    }
    
    public static class MyScriptEditor
    {
    public static void SetupLocalization(MyScript script)
    {
        var localizer = script.gameObject.AddComponent<GameObjectLocalizer>();
    
        // Gets the Tracked text or creates a new tracker
        var trackedScript = localizer.GetTrackedObject<TrackedMonoBehaviourObject>(script);
    
        // Gets the Property Variant for the text or creates a new one
        var textVariant = trackedScript.GetTrackedProperty<LocalizedStringProperty>(nameof(MyScript.myText));
        textVariant.LocalizedString.SetReference("My String Table Collection", "My Text");
    
        var redVariant = trackedScript.GetTrackedProperty<FloatTrackedProperty>("textColor.r");
        var greenVariant = trackedScript.GetTrackedProperty<FloatTrackedProperty>("textColor.g");
        var blueVariant = trackedScript.GetTrackedProperty<FloatTrackedProperty>("textColor.b");
    
        // Default to black text
        redVariant.SetValue("en", 0);
        greenVariant.SetValue("en", 0);
        blueVariant.SetValue("en", 0);
    
        // Use Red for French
        redVariant.SetValue("fr", 1);
    
        // Use Green for Japanese
        greenVariant.SetValue("fr", 1);
    
        // Use white for Arabic
        redVariant.SetValue("ar", 1);
        greenVariant.SetValue("ar", 1);
        blueVariant.SetValue("ar", 1);
    }
    }

    Properties

    TrackedObjects

    The objects that are being tracked by this Localizer.

    Declaration
    public List<TrackedObject> TrackedObjects { get; }
    Property Value
    Type Description
    List<TrackedObject>

    Methods

    ApplyLocaleVariant(Locale)

    Apply all variants for the selected Locale to this GameObject.

    Declaration
    public AsyncOperationHandle ApplyLocaleVariant(Locale locale)
    Parameters
    Type Name Description
    Locale locale

    The Locale to apply to the GameObject.

    Returns
    Type Description
    AsyncOperationHandle

    A handle to any loading operations or default if the operation was immediate.

    ApplyLocaleVariant(Locale, Locale)

    Apply all variants for the selected Locale to this GameObject. When a value cannot be found, the fallback value is used.

    Declaration
    public AsyncOperationHandle ApplyLocaleVariant(Locale locale, Locale fallback)
    Parameters
    Type Name Description
    Locale locale

    The Locale to apply to the GameObject.

    Locale fallback

    The fallback Locale to use when a value does not exist for locale.

    Returns
    Type Description
    AsyncOperationHandle

    A handle to any loading operations or default if the operation was immediate.

    GetTrackedObject(Object)

    Returns the TrackedObjects for the target component or null if one does not exist. See GetTrackedObject<T>(Object, Boolean) for a version that will create a new TrackedObject if one does not already exist.

    Declaration
    public TrackedObject GetTrackedObject(Object target)
    Parameters
    Type Name Description
    Object target

    The target object to search for.

    Returns
    Type Description
    TrackedObject

    The TrackedObjects or null if one could not be found.

    GetTrackedObject<T>(Object, Boolean)

    Returns the TrackedObjects for the target component or creates a new instance if create is set to true.

    Declaration
    public T GetTrackedObject<T>(Object target, bool create = true)
        where T : TrackedObject, new()
    Parameters
    Type Name Description
    Object target

    The Target Object to track.

    Boolean create

    Creates a new Tracked Object if one can not be found.

    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    The Type of TrackedObject that should be found or added.

    In This Article
    • Properties
      • TrackedObjects
    • Methods
      • ApplyLocaleVariant(Locale)
      • ApplyLocaleVariant(Locale, Locale)
      • GetTrackedObject(Object)
      • GetTrackedObject<T>(Object, Boolean)
    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