using System.Collections.Generic; using UnityEngine.Localization.Events; using UnityEngine.Localization.Settings; using UnityEngine.Localization.Tables; namespace UnityEngine.Localization.Components { /// /// Component that can be used to Localize a string. /// Provides an update event that can be used to automatically update the string /// when the or changes. /// Allows for configuring optional arguments that will be used by **Smart Format** or [String.Format](https://docs.microsoft.com/en-us/dotnet/api/system.string.format). /// /// /// This example shows how a Localized String Event can be dynamically updated with a different localized string or new formatting data. /// /// /// /// ![](../manual/images/scripting/LocalizeStringEventExample_Inspector.png) /// /// /// Example of String Table Contents /// /// ![Example of String Table Contents](../manual/images/scripting/LocalizeStringEventExample_StringTable.png) /// /// /// Example results in Game /// /// ![Example results in Game](../manual/images/scripting/LocalizeStringEventExample_GameView.gif) /// [AddComponentMenu("Localization/Localize String Event")] public class LocalizeStringEvent : LocalizedMonoBehaviour { [SerializeField] LocalizedString m_StringReference = new LocalizedString(); [SerializeField] List m_FormatArguments = new List(); [SerializeField] UnityEventString m_UpdateString = new UnityEventString(); LocalizedString.ChangeHandler m_ChangeHandler; /// /// References the and of the localized string. /// public LocalizedString StringReference { get => m_StringReference; set { // Unsubscribe from the old string reference. ClearChangeHandler(); m_StringReference = value; if (isActiveAndEnabled) RegisterChangeHandler(); } } /// /// Event that will be sent when the localized string is available. /// public UnityEventString OnUpdateString { get => m_UpdateString; set => m_UpdateString = value; } /// /// Forces the string to be regenerated, such as when the string formatting argument values have changed. /// public void RefreshString() { StringReference?.RefreshString(); } /// /// Changes the value of a LocalizeString. /// /// A reference to the table that will be set to StringReference of a LocalizeString public void SetTable(string tableReference) { if (StringReference == null) StringReference = new LocalizedString(); StringReference.TableReference = tableReference; } /// /// Changes the value of a LocalizeString. /// /// A reference to the entry in the table that will be set to StringReference of a LocalizeString public void SetEntry(string entryName) { if (StringReference == null) StringReference = new LocalizedString(); StringReference.TableEntryReference = entryName; } /// /// Starts listening for changes to . /// protected virtual void OnEnable() => RegisterChangeHandler(); /// /// Stops listening for changes to . /// protected virtual void OnDisable() => ClearChangeHandler(); void OnDestroy() => ClearChangeHandler(); /// /// Invokes the event. /// /// protected virtual void UpdateString(string value) { #if UNITY_EDITOR if (!LocalizationSettings.Instance.IsPlayingOrWillChangePlaymode) { if (StringReference == null || StringReference.IsEmpty) { Editor_UnregisterKnownDrivenProperties(OnUpdateString); return; } Editor_RegisterKnownDrivenProperties(OnUpdateString); OnUpdateString.Invoke(value); Editor_RefreshEventObjects(OnUpdateString); } else #endif { OnUpdateString.Invoke(value); } } void OnValidate() { RefreshString(); } internal virtual void RegisterChangeHandler() { if (StringReference == null) return; if (m_FormatArguments.Count > 0) { StringReference.Arguments = m_FormatArguments.ToArray(); if (Application.isPlaying) Debug.LogWarningFormat("LocalizeStringEvent({0}) is using the deprecated Format Arguments field which will be removed in the future. Consider upgrading to use String Reference Local Variables instead.", name, this); } if (m_ChangeHandler == null) m_ChangeHandler = UpdateString; StringReference.StringChanged += m_ChangeHandler; } internal virtual void ClearChangeHandler() { #if UNITY_EDITOR Editor_UnregisterKnownDrivenProperties(OnUpdateString); #endif if (StringReference != null) StringReference.StringChanged -= m_ChangeHandler; } } }