- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
public class Localization { // ... public static string Translate(string id) { string key = id.ToLower(); // case insensitive id are used string lang = LocalizationManager.Instance().GetCurrentLanguage(); LoadTranslations(); // only loaded once int lang_index = FindLangIndex(lang); if (lang_index == -1) { Debug.Log("Unknown translation language: " + lang); return id; } string translation = (string)_translationData.content[lang_index][key]; if (translation == null) { Debug.Log("Missing translation for text id: "+id); return id; } return translation; } // ... }
Localizing image assets is a greater challenge because they are often used automatically in unity3d and added to GameObjects using the component system. To localize these assets, we use components that can be “dragged-and-dropped” into the Prefabs or GameObject instances that uses these localized image assets to automatically choose the correct texture. The abstract class
LocalizedAsset
is used to perform this task.
public abstract class LocalizedAsset : MonoBehaviour { public Texture TranslateAsset(Texture initTexture) { Texture translatedTexture = null; if (initTexture != null) { translatedTexture = TranslateAsset(initTexture.name); } return translatedTexture; } public Texture TranslateAsset(string assetName) { Texture translatedTexture = null; string langAssetName = StripLanguageExtension(assetName) + LocalizationManager.Instance().CurrentLanguageToExtension(); translatedTexture = (Texture)LocalizationManager.Instance().LoadAsset(langAssetName); return translatedTexture; } public abstract void LoadAssets(); public void Start() { LocalizedAssetsObserver.AddLocalizedAsset(this); LoadAssets(); } }
The
LoadAssets
method must be implemented by subclasses such that it will ensure that all the localized assets present in the current object will be correctly translated. The
TranslateAsset
methods can be used to get the translated textures easily. The
Start
implementation will then load all the localized assets of the object at***ntime for all the subclasses. The observer design pattern is used to register any localized assets in the aim of having the possibility to to reload them as needed when the language is changed at***ntime by the user. An example implementation of
LocalizedAsset
is provided below:
public class LocalizedMaterialAsset : LocalizedAsset { public override void LoadAssets() { // Assuming only 1 material is used... MeshRenderer renderer = (MeshRenderer)gameObject.GetComponent("MeshRenderer"); if (renderer != null && renderer.material.mainTexture != null) { renderer.material.mainTexture = TranslateAsset(renderer.material.mainTexture); } } }
Thus, adding the
LocalizedMaterialAsset
to any Prefab or GameObjects that have a certain material component will automatically translate the texture image used by that material. The
LocalizationManager
singleton instance is responsible for keeping track of the current localization state (current language, etc…) and uses the Unity3D
Resources
class to dynamically load localized assets:
public class LocalizationManager : MonoBehaviour { // ... public Object LoadAsset(string assetName) { return Resources.Load("Localization/"+_currentLanguage+"/"+assetName); } // ... }
Thus using a csv database and Unity3D components and dynamic resources enabled us to easily localize our game to multiple languages. I hope this can help other Unity3D community members to localize their game.
由 u8 发表 |
|