docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class GameObjectLocalizer

    The 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, float)
    MonoBehaviour.InvokeRepeating(string, float, float)
    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, out Component)
    Component.TryGetComponent<T>(out T)
    Component.GetComponent(string)
    Component.GetComponentInChildren(Type, bool)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(bool)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, bool)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(bool)
    Component.GetComponentsInChildren<T>(bool, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, bool)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(bool)
    Component.GetComponentsInParent<T>(bool, 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, bool)
    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, bool)
    Object.Destroy(Object, float)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, bool)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, float)
    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
    Assembly: Unity.Localization.dll
    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

    Name Description
    TrackedObjects

    The objects that are being tracked by this Localizer.

    Methods

    Name Description
    ApplyLocaleVariant(Locale)

    Apply all variants for the selected Locale to this GameObject.

    ApplyLocaleVariant(Locale, Locale)

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

    GetTrackedObject(Object)

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

    GetTrackedObject<T>(Object, bool)

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

    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)