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 GameObject it is attached to.

    Inheritance
    Object
    Object
    Component
    Behaviour
    MonoBehaviour
    GameObjectLocalizer
    Inherited Members
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(String, Single)
    MonoBehaviour.InvokeRepeating(String, Single, Single)
    MonoBehaviour.CancelInvoke(String)
    MonoBehaviour.IsInvoking(String)
    MonoBehaviour.StartCoroutine(String)
    MonoBehaviour.StartCoroutine(String, Object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(String)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(Object)
    MonoBehaviour.useGUILayout
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, Component)
    Component.TryGetComponent<T>(T)
    Component.GetComponent(String)
    Component.GetComponentInChildren(Type, Boolean)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(Boolean)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, Boolean)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(Boolean)
    Component.GetComponentsInChildren<T>(Boolean, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, Boolean)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(Boolean)
    Component.GetComponentsInParent<T>(Boolean, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.CompareTag(String)
    Component.SendMessageUpwards(String, Object, SendMessageOptions)
    Component.SendMessageUpwards(String, Object)
    Component.SendMessageUpwards(String)
    Component.SendMessageUpwards(String, SendMessageOptions)
    Component.SendMessage(String, Object)
    Component.SendMessage(String)
    Component.SendMessage(String, Object, SendMessageOptions)
    Component.SendMessage(String, SendMessageOptions)
    Component.BroadcastMessage(String, Object, SendMessageOptions)
    Component.BroadcastMessage(String, Object)
    Component.BroadcastMessage(String)
    Component.BroadcastMessage(String, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(Object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, Boolean)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, Boolean)
    Object.Destroy(Object, Single)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, Boolean)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, Single)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.name
    Object.hideFlags
    Namespace: UnityEngine.Localization.PropertyVariants
    Syntax
    [ExecuteAlways]
    [DisallowMultipleComponent]
    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 RectTransform 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 MonoBehaviour 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.


    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    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