「Unity/多言語化」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
編集の要約なし |
||
| 69行目: | 69行目: | ||
Destroy (gameObject); | Destroy (gameObject); | ||
} | } | ||
DontDestroyOnLoad (gameObject); | DontDestroyOnLoad (gameObject); | ||
} | } | ||
public void LoadLocalizedText(string fileName) | public void LoadLocalizedText(string fileName) | ||
{ | { | ||
localizedText = new Dictionary<string, string> (); | localizedText = new Dictionary<string, string> (); | ||
string filePath = Path.Combine (Application.streamingAssetsPath, fileName); | string filePath = Path.Combine (Application.streamingAssetsPath, fileName); | ||
if (File.Exists (filePath)) { | if (File.Exists (filePath)) { | ||
string dataAsJson = File.ReadAllText (filePath); | string dataAsJson = File.ReadAllText (filePath); | ||
| 86行目: | 83行目: | ||
localizedText.Add (loadedData.items [i].key, loadedData.items [i].value); | localizedText.Add (loadedData.items [i].key, loadedData.items [i].value); | ||
} | } | ||
Debug.Log ("Data loaded, dictionary contains: " + localizedText.Count + " entries"); | Debug.Log ("Data loaded, dictionary contains: " + localizedText.Count + " entries"); | ||
} else | } else | ||
| 95行目: | 91行目: | ||
isReady = true; | isReady = true; | ||
} | } | ||
public string GetLocalizedValue(string key) | public string GetLocalizedValue(string key) | ||
{ | { | ||
| 103行目: | 98行目: | ||
result = localizedText [key]; | result = localizedText [key]; | ||
} | } | ||
return result; | return result; | ||
} | } | ||
| 112行目: | 105行目: | ||
return isReady; | return isReady; | ||
} | } | ||
} | } | ||
</pre> | </pre> | ||
StartupManager.cs | StartupManager.cs | ||
LocalizedText.cs | LocalizedText.cs | ||
<pre> | |||
using System.Collections.Generic; | |||
using UnityEngine; | |||
using UnityEngine.UI; | |||
public class LocalizedText : MonoBehaviour { | |||
public string key; | |||
void Start () | |||
{ | |||
Text text = GetComponent<Text> (); | |||
LocalizationManager manager = LocalizationManager.instance; | |||
if (manager == null) | |||
{ | |||
return; | |||
} | |||
string str = manager.GetLocalizedValue(key); | |||
if (str == null) | |||
{ | |||
return; | |||
} | |||
text.text = str; | |||
} | |||
} | |||
</pre> | |||
LocalizedTextEditor.cs | LocalizedTextEditor.cs | ||
<pre> | |||
using System.Collections; | |||
using System.Collections.Generic; | |||
using UnityEngine; | |||
using UnityEditor; | |||
using System.IO; | |||
public class LocalizedTextEditor : EditorWindow | |||
{ | |||
public LocalizationData localizationData; | |||
[MenuItem ("Window/Localized Text Editor")] | |||
static void Init() | |||
{ | |||
EditorWindow.GetWindow (typeof(LocalizedTextEditor)).Show (); | |||
} | |||
private void OnGUI() | |||
{ | |||
if (localizationData != null) | |||
{ | |||
SerializedObject serializedObject = new SerializedObject (this); | |||
SerializedProperty serializedProperty = serializedObject.FindProperty ("localizationData"); | |||
EditorGUILayout.PropertyField (serializedProperty, true); | |||
serializedObject.ApplyModifiedProperties (); | |||
if (GUILayout.Button ("Save data")) | |||
{ | |||
SaveGameData (); | |||
} | |||
} | |||
if (GUILayout.Button ("Load data")) | |||
{ | |||
LoadGameData (); | |||
} | |||
if (GUILayout.Button ("Create new data")) | |||
{ | |||
CreateNewData (); | |||
} | |||
} | |||
private void LoadGameData() | |||
{ | |||
string filePath = EditorUtility.OpenFilePanel ("Select localization data file", Application.streamingAssetsPath, "json"); | |||
if (!string.IsNullOrEmpty (filePath)) | |||
{ | |||
string dataAsJson = File.ReadAllText (filePath); | |||
localizationData = JsonUtility.FromJson<LocalizationData> (dataAsJson); | |||
} | |||
} | |||
private void SaveGameData() | |||
{ | |||
string filePath = EditorUtility.SaveFilePanel ("Save localization data file", Application.streamingAssetsPath, "", "json"); | |||
if (!string.IsNullOrEmpty(filePath)) | |||
{ | |||
string dataAsJson = JsonUtility.ToJson(localizationData); | |||
File.WriteAllText (filePath, dataAsJson); | |||
} | |||
} | |||
private void CreateNewData() | |||
{ | |||
localizationData = new LocalizationData (); | |||
} | |||
} | |||
</pre> | |||
==参考== | ==参考== | ||
2020年5月18日 (月) 16:56時点における版
ダウンロード
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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LocalizedText : MonoBehaviour {
public string key;
void Start ()
{
Text text = GetComponent<Text> ();
LocalizationManager manager = LocalizationManager.instance;
if (manager == null)
{
return;
}
string str = manager.GetLocalizedValue(key);
if (str == null)
{
return;
}
text.text = str;
}
}
LocalizedTextEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class LocalizedTextEditor : EditorWindow
{
public LocalizationData localizationData;
[MenuItem ("Window/Localized Text Editor")]
static void Init()
{
EditorWindow.GetWindow (typeof(LocalizedTextEditor)).Show ();
}
private void OnGUI()
{
if (localizationData != null)
{
SerializedObject serializedObject = new SerializedObject (this);
SerializedProperty serializedProperty = serializedObject.FindProperty ("localizationData");
EditorGUILayout.PropertyField (serializedProperty, true);
serializedObject.ApplyModifiedProperties ();
if (GUILayout.Button ("Save data"))
{
SaveGameData ();
}
}
if (GUILayout.Button ("Load data"))
{
LoadGameData ();
}
if (GUILayout.Button ("Create new data"))
{
CreateNewData ();
}
}
private void LoadGameData()
{
string filePath = EditorUtility.OpenFilePanel ("Select localization data file", Application.streamingAssetsPath, "json");
if (!string.IsNullOrEmpty (filePath))
{
string dataAsJson = File.ReadAllText (filePath);
localizationData = JsonUtility.FromJson<LocalizationData> (dataAsJson);
}
}
private void SaveGameData()
{
string filePath = EditorUtility.SaveFilePanel ("Save localization data file", Application.streamingAssetsPath, "", "json");
if (!string.IsNullOrEmpty(filePath))
{
string dataAsJson = JsonUtility.ToJson(localizationData);
File.WriteAllText (filePath, dataAsJson);
}
}
private void CreateNewData()
{
localizationData = new LocalizationData ();
}
}