using UnityEngine.Events;
using UnityEngine.Localization.Settings;
namespace UnityEngine.Localization.Components
{
///
/// Abstract class that can be inherited from to create a general purpose Localized Asset Component.
/// This Component handles the Localization of the asset and calls
/// whenever a new Localized Asset is ready.
///
///
/// This example shows how the [Font](https://docs.unity3d.com/ScriptReference/Font.html) asset of a [UGUI Text Component](https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.UI.Text.html) could be localized.
/// 
///
///
/// The type of Asset to be Localized. Must inherit from [UnityEngine.Object](https://docs.unity3d.com/ScriptReference/Object.html)
/// The **Serializable** LocalizedAsset class. This will be used for the property.
[ExecuteAlways]
public abstract class LocalizedAssetBehaviour : LocalizedMonoBehaviour
where TObject : Object
where TReference : LocalizedAsset, new()
{
[SerializeField]
TReference m_LocalizedAssetReference = new TReference();
LocalizedAsset.ChangeHandler m_ChangeHandler;
///
/// Reference to the Table and Entry which will be used to identify the asset being localized.
///
public TReference AssetReference
{
get => m_LocalizedAssetReference;
set
{
ClearChangeHandler();
m_LocalizedAssetReference = value;
if (isActiveAndEnabled)
RegisterChangeHandler();
}
}
protected virtual void OnEnable() => RegisterChangeHandler();
protected virtual void OnDisable() => ClearChangeHandler();
void OnDestroy() => ClearChangeHandler();
void OnValidate() => AssetReference?.ForceUpdate();
internal virtual void RegisterChangeHandler()
{
if (AssetReference == null)
return;
if (m_ChangeHandler == null)
m_ChangeHandler = UpdateAsset;
AssetReference.AssetChanged += m_ChangeHandler;
}
internal virtual void ClearChangeHandler()
{
if (AssetReference != null)
AssetReference.AssetChanged -= m_ChangeHandler;
}
///
/// Called when has been loaded. This will occur when the game first starts after
/// has completed and whenever
/// the is changed.
///
///
protected abstract void UpdateAsset(TObject localizedAsset);
}
///
/// A version of which also includes a [UnityEvent](https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html) with the localized asset.
/// Using the event it is possible to Localize Components without writing scripts specific to the Component that can be configured in the Inspector.
///
///
/// This example shows how a [Font](https://docs.unity3d.com/ScriptReference/Font.html) asset could be localized.
/// 
///
///
/// The type of Asset to be Localized. Must inherit from [UnityEngine.Object](https://docs.unity3d.com/ScriptReference/Object.html)
/// The Serializable LocalizedAsset class. This will be used for the property.
/// The Serializable [UnityEvent](https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html) that should be called when the asset is loaded.
public class LocalizedAssetEvent : LocalizedAssetBehaviour
where TObject : Object
where TReference : LocalizedAsset, new()
where TEvent : UnityEvent, new()
{
[SerializeField]
TEvent m_UpdateAsset = new TEvent();
///
/// Unity Event that is invoked when the localized asset is updated.
///
public TEvent OnUpdateAsset
{
get => m_UpdateAsset;
set => m_UpdateAsset = value;
}
#if UNITY_EDITOR
internal override void ClearChangeHandler()
{
Editor_UnregisterKnownDrivenProperties(OnUpdateAsset);
base.ClearChangeHandler();
}
#endif
///
protected override void UpdateAsset(TObject localizedAsset)
{
#if UNITY_EDITOR
if (!LocalizationSettings.Instance.IsPlayingOrWillChangePlaymode)
{
if (AssetReference.IsEmpty)
{
Editor_UnregisterKnownDrivenProperties(OnUpdateAsset);
return;
}
Editor_RegisterKnownDrivenProperties(OnUpdateAsset);
OnUpdateAsset.Invoke(localizedAsset);
Editor_RefreshEventObjects(OnUpdateAsset);
}
else
#endif
{
OnUpdateAsset.Invoke(localizedAsset);
}
}
}
}