//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using GameFramework; using GameFramework.Localization; using GameFramework.Resource; using UnityEngine; namespace UnityGameFramework.Runtime { /// /// 本地化组件。 /// [DisallowMultipleComponent] [AddComponentMenu("Game Framework/Localization")] public sealed class LocalizationComponent : GameFrameworkComponent { private const int DefaultPriority = 0; private ILocalizationManager m_LocalizationManager = null; private EventComponent m_EventComponent = null; [SerializeField] private bool m_EnableLoadDictionaryUpdateEvent = false; [SerializeField] private bool m_EnableLoadDictionaryDependencyAssetEvent = false; [SerializeField] private string m_LocalizationHelperTypeName = "UnityGameFramework.Runtime.DefaultLocalizationHelper"; [SerializeField] private LocalizationHelperBase m_CustomLocalizationHelper = null; [SerializeField] private int m_CachedBytesSize = 0; /// /// 获取或设置本地化语言。 /// public Language Language { get { return m_LocalizationManager.Language; } set { m_LocalizationManager.Language = value; } } /// /// 获取系统语言。 /// public Language SystemLanguage { get { return m_LocalizationManager.SystemLanguage; } } /// /// 获取字典数量。 /// public int DictionaryCount { get { return m_LocalizationManager.DictionaryCount; } } /// /// 获取缓冲二进制流的大小。 /// public int CachedBytesSize { get { return m_LocalizationManager.CachedBytesSize; } } /// /// 游戏框架组件初始化。 /// protected override void Awake() { base.Awake(); m_LocalizationManager = GameFrameworkEntry.GetModule(); if (m_LocalizationManager == null) { Log.Fatal("Localization manager is invalid."); return; } m_LocalizationManager.ReadDataSuccess += OnReadDataSuccess; m_LocalizationManager.ReadDataFailure += OnReadDataFailure; if (m_EnableLoadDictionaryUpdateEvent) { m_LocalizationManager.ReadDataUpdate += OnReadDataUpdate; } if (m_EnableLoadDictionaryDependencyAssetEvent) { m_LocalizationManager.ReadDataDependencyAsset += OnReadDataDependencyAsset; } } private void Start() { BaseComponent baseComponent = GameEntry.GetComponent(); if (baseComponent == null) { Log.Fatal("Base component is invalid."); return; } m_EventComponent = GameEntry.GetComponent(); if (m_EventComponent == null) { Log.Fatal("Event component is invalid."); return; } if (baseComponent.EditorResourceMode) { m_LocalizationManager.SetResourceManager(baseComponent.EditorResourceHelper); } else { m_LocalizationManager.SetResourceManager(GameFrameworkEntry.GetModule()); } LocalizationHelperBase localizationHelper = Helper.CreateHelper(m_LocalizationHelperTypeName, m_CustomLocalizationHelper); if (localizationHelper == null) { Log.Error("Can not create localization helper."); return; } localizationHelper.name = "Localization Helper"; Transform transform = localizationHelper.transform; transform.SetParent(this.transform); transform.localScale = Vector3.one; m_LocalizationManager.SetDataProviderHelper(localizationHelper); m_LocalizationManager.SetLocalizationHelper(localizationHelper); m_LocalizationManager.Language = baseComponent.EditorResourceMode && baseComponent.EditorLanguage != Language.Unspecified ? baseComponent.EditorLanguage : m_LocalizationManager.SystemLanguage; if (m_CachedBytesSize > 0) { EnsureCachedBytesSize(m_CachedBytesSize); } } /// /// 确保二进制流缓存分配足够大小的内存并缓存。 /// /// 要确保二进制流缓存分配内存的大小。 public void EnsureCachedBytesSize(int ensureSize) { m_LocalizationManager.EnsureCachedBytesSize(ensureSize); } /// /// 释放缓存的二进制流。 /// public void FreeCachedBytes() { m_LocalizationManager.FreeCachedBytes(); } /// /// 读取字典。 /// /// 字典资源名称。 public void ReadData(string dictionaryAssetName) { m_LocalizationManager.ReadData(dictionaryAssetName); } /// /// 读取字典。 /// /// 字典资源名称。 /// 加载字典资源的优先级。 public void ReadData(string dictionaryAssetName, int priority) { m_LocalizationManager.ReadData(dictionaryAssetName, priority); } /// /// 读取字典。 /// /// 字典资源名称。 /// 用户自定义数据。 public void ReadData(string dictionaryAssetName, object userData) { m_LocalizationManager.ReadData(dictionaryAssetName, userData); } /// /// 读取字典。 /// /// 字典资源名称。 /// 加载字典资源的优先级。 /// 用户自定义数据。 public void ReadData(string dictionaryAssetName, int priority, object userData) { m_LocalizationManager.ReadData(dictionaryAssetName, priority, userData); } /// /// 解析字典。 /// /// 要解析的字典字符串。 /// 是否解析字典成功。 public bool ParseData(string dictionaryString) { return m_LocalizationManager.ParseData(dictionaryString); } /// /// 解析字典。 /// /// 要解析的字典字符串。 /// 用户自定义数据。 /// 是否解析字典成功。 public bool ParseData(string dictionaryString, object userData) { return m_LocalizationManager.ParseData(dictionaryString, userData); } /// /// 解析字典。 /// /// 要解析的字典二进制流。 /// 是否解析字典成功。 public bool ParseData(byte[] dictionaryBytes) { return m_LocalizationManager.ParseData(dictionaryBytes); } /// /// 解析字典。 /// /// 要解析的字典二进制流。 /// 用户自定义数据。 /// 是否解析字典成功。 public bool ParseData(byte[] dictionaryBytes, object userData) { return m_LocalizationManager.ParseData(dictionaryBytes, userData); } /// /// 解析字典。 /// /// 要解析的字典二进制流。 /// 字典二进制流的起始位置。 /// 字典二进制流的长度。 /// 是否解析字典成功。 public bool ParseData(byte[] dictionaryBytes, int startIndex, int length) { return m_LocalizationManager.ParseData(dictionaryBytes, startIndex, length); } /// /// 解析字典。 /// /// 要解析的字典二进制流。 /// 字典二进制流的起始位置。 /// 字典二进制流的长度。 /// 用户自定义数据。 /// 是否解析字典成功。 public bool ParseData(byte[] dictionaryBytes, int startIndex, int length, object userData) { return m_LocalizationManager.ParseData(dictionaryBytes, startIndex, length, userData); } /// /// 根据字典主键获取字典内容字符串。 /// /// 字典主键。 /// 要获取的字典内容字符串。 public string GetString(string key) { return m_LocalizationManager.GetString(key); } /// /// 根据字典主键获取字典内容字符串。 /// /// 字典主键。 /// 字典参数 0。 /// 要获取的字典内容字符串。 public string GetString(string key, object arg0) { return m_LocalizationManager.GetString(key, arg0); } /// /// 根据字典主键获取字典内容字符串。 /// /// 字典主键。 /// 字典参数 0。 /// 字典参数 1。 /// 要获取的字典内容字符串。 public string GetString(string key, object arg0, object arg1) { return m_LocalizationManager.GetString(key, arg0, arg1); } /// /// 根据字典主键获取字典内容字符串。 /// /// 字典主键。 /// 字典参数 0。 /// 字典参数 1。 /// 字典参数 2。 /// 要获取的字典内容字符串。 public string GetString(string key, object arg0, object arg1, object arg2) { return m_LocalizationManager.GetString(key, arg0, arg1, arg2); } /// /// 根据字典主键获取字典内容字符串。 /// /// 字典主键。 /// 字典参数。 /// 要获取的字典内容字符串。 public string GetString(string key, params object[] args) { return m_LocalizationManager.GetString(key, args); } /// /// 是否存在字典。 /// /// 字典主键。 /// 是否存在字典。 public bool HasRawString(string key) { return m_LocalizationManager.HasRawString(key); } /// /// 根据字典主键获取字典值。 /// /// 字典主键。 /// 字典值。 public string GetRawString(string key) { return m_LocalizationManager.GetRawString(key); } /// /// 移除字典。 /// /// 字典主键。 /// 是否移除字典成功。 public bool RemoveRawString(string key) { return m_LocalizationManager.RemoveRawString(key); } /// /// 清空所有字典。 /// public void RemoveAllRawStrings() { m_LocalizationManager.RemoveAllRawStrings(); } private void OnReadDataSuccess(object sender, ReadDataSuccessEventArgs e) { m_EventComponent.Fire(this, LoadDictionarySuccessEventArgs.Create(e)); } private void OnReadDataFailure(object sender, ReadDataFailureEventArgs e) { Log.Warning("Load dictionary failure, asset name '{0}', error message '{1}'.", e.DataAssetName, e.ErrorMessage); m_EventComponent.Fire(this, LoadDictionaryFailureEventArgs.Create(e)); } private void OnReadDataUpdate(object sender, ReadDataUpdateEventArgs e) { m_EventComponent.Fire(this, LoadDictionaryUpdateEventArgs.Create(e)); } private void OnReadDataDependencyAsset(object sender, ReadDataDependencyAssetEventArgs e) { m_EventComponent.Fire(this, LoadDictionaryDependencyAssetEventArgs.Create(e)); } } }