「Unity/多言語化」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→翻訳ファイルサンプル) |
|||
行30: | 行30: | ||
==上記DLしたファイル設定とカスタマイズ== | ==上記DLしたファイル設定とカスタマイズ== | ||
+ | LocalizationData.cs | ||
+ | <pre> | ||
+ | [System.Serializable] | ||
+ | public class LocalizationData | ||
+ | { | ||
+ | public LocalizationItem[] items; | ||
+ | } | ||
+ | |||
+ | [System.Serializable] | ||
+ | public class LocalizationItem | ||
+ | { | ||
+ | public string key; | ||
+ | public string value; | ||
+ | } | ||
+ | </pre> | ||
+ | LocalizationManager.cs | ||
+ | <pre> | ||
+ | using System.Collections; | ||
+ | using System.Collections.Generic; | ||
+ | using UnityEngine; | ||
+ | using System.IO; | ||
+ | |||
+ | public class LocalizationManager : MonoBehaviour { | ||
+ | |||
+ | public static LocalizationManager instance; | ||
+ | |||
+ | private Dictionary<string, string> localizedText; | ||
+ | private bool isReady = false; | ||
+ | private string missingTextString = "Localized text not found"; | ||
+ | |||
+ | // Use this for initialization | ||
+ | void Awake () | ||
+ | { | ||
+ | if (instance == null) { | ||
+ | instance = this; | ||
+ | } else if (instance != this) | ||
+ | { | ||
+ | Destroy (gameObject); | ||
+ | } | ||
+ | |||
+ | DontDestroyOnLoad (gameObject); | ||
+ | } | ||
+ | |||
+ | public void LoadLocalizedText(string fileName) | ||
+ | { | ||
+ | localizedText = new Dictionary<string, string> (); | ||
+ | string filePath = Path.Combine (Application.streamingAssetsPath, fileName); | ||
+ | |||
+ | if (File.Exists (filePath)) { | ||
+ | string dataAsJson = File.ReadAllText (filePath); | ||
+ | LocalizationData loadedData = JsonUtility.FromJson<LocalizationData> (dataAsJson); | ||
+ | |||
+ | for (int i = 0; i < loadedData.items.Length; i++) | ||
+ | { | ||
+ | localizedText.Add (loadedData.items [i].key, loadedData.items [i].value); | ||
+ | } | ||
+ | |||
+ | Debug.Log ("Data loaded, dictionary contains: " + localizedText.Count + " entries"); | ||
+ | } else | ||
+ | { | ||
+ | Debug.LogError ("Cannot find file!"); | ||
+ | } | ||
+ | |||
+ | isReady = true; | ||
+ | } | ||
+ | |||
+ | public string GetLocalizedValue(string key) | ||
+ | { | ||
+ | string result = missingTextString; | ||
+ | if (localizedText.ContainsKey (key)) | ||
+ | { | ||
+ | result = localizedText [key]; | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | |||
+ | } | ||
+ | |||
+ | public bool GetIsReady() | ||
+ | { | ||
+ | return isReady; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </pre> | ||
+ | StartupManager.cs | ||
+ | LocalizedText.cs | ||
+ | LocalizedTextEditor.cs | ||
==参考== | ==参考== |
2020年5月19日 (火) 01:54時点における版
ダウンロード
LocalizationData.cs LocalizationManager.cs StartupManager.cs LocalizedText.cs LocalizedTextEditor.cs
翻訳ファイルサンプル
Assets/StreamingAssets/localizedText_ja.json
{ "items":[ {"key":"title","value":"タイトル1"}, {"key":"edit","value":"編集1"} ] }
Assets/StreamingAssets/localizedText_en.json
{ "items":[ {"key":"title","value":"title1"}, {"key":"edit","value":"edit1"} ] }
上記DLしたファイル設定とカスタマイズ
LocalizationData.cs
[System.Serializable] public class LocalizationData { public LocalizationItem[] items; } [System.Serializable] public class LocalizationItem { public string key; public string value; }
LocalizationManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LocalizationManager : MonoBehaviour { public static LocalizationManager instance; private Dictionary<string, string> localizedText; private bool isReady = false; private string missingTextString = "Localized text not found"; // Use this for initialization void Awake () { if (instance == null) { instance = this; } else if (instance != this) { Destroy (gameObject); } DontDestroyOnLoad (gameObject); } public void LoadLocalizedText(string fileName) { localizedText = new Dictionary<string, string> (); string filePath = Path.Combine (Application.streamingAssetsPath, fileName); if (File.Exists (filePath)) { string dataAsJson = File.ReadAllText (filePath); LocalizationData loadedData = JsonUtility.FromJson<LocalizationData> (dataAsJson); for (int i = 0; i < loadedData.items.Length; i++) { localizedText.Add (loadedData.items [i].key, loadedData.items [i].value); } Debug.Log ("Data loaded, dictionary contains: " + localizedText.Count + " entries"); } else { Debug.LogError ("Cannot find file!"); } isReady = true; } public string GetLocalizedValue(string key) { string result = missingTextString; if (localizedText.ContainsKey (key)) { result = localizedText [key]; } return result; } public bool GetIsReady() { return isReady; } }
StartupManager.cs LocalizedText.cs LocalizedTextEditor.cs