Class GameObjectLocalizer
The GameObject Localizer component is responsible for storing and applying all Localized Property Variants Configurations for the GameObject it is attached to.
Inherited Members
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 |
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 |