【发布时间】:2018-08-14 01:07:44
【问题描述】:
我知道我的标题有很多重复项,但我似乎找不到与我相同的问题。让我粘贴我的代码。
LocalizationManager.cs
rh.eLanguage language = tzGlobal.Instance.OPTION.language;
string json = StreetUtility.LoadJsonFromStreamingAssets("notice.json");
if (json != null)
{
// Separate only the necessary parts.
LitJson.JsonData data = LitJson.JsonMapper.ToObject(json);
json = data[language.ToString()].ToJson();
notice = LitJson.JsonMapper.ToObject<string[]>(json);
}
string path = string.Format("{0}/{1}/language", rh.Const.LOCALIZATION_PATH, language);
json = StreetUtility.LoadJsonFromResources(path);
if (json != null)
{
// json load.
Dictionary<string, string> dic = LitJson.JsonMapper.ToObject<Dictionary<string, string>>(json);
// dictionary copy.
dic_localization_text = new Dictionary<eTextKey, string>();
eTextKey e;
for (int i = 0; i < dic.Count; i++)
{
e = (eTextKey)i;
dic_localization_text[e] = dic[e.ToString()];
}
// Run registered localize function.
for (int i = 0; i < list_localize_method.Count; i++)
{
complete = false;
list_localize_method[i].Invoke();
yield return new WaitUntil(() => complete);
}
}
Debug.LogWarning("TODO: Loading popup off.");
注意:我有 2 个版本(PC 版)和(移动版)PC 版运行良好,现在我的问题是移动版
注意: 这是一个安卓应用程序(apk)
我在LocalizationManager 上所做的是我正在从我的流媒体资产(notice.json)中加载 json 文件,但问题是在我的 logcat 上它有这个错误
:KeyNotFoundException: 给定的键不在dictionary.at System.Collections.Generic.Dictionary`2[System.String,LitJson.JsonData].get_Item (System.String key) [0x00000] in :0 在 LitJson.JsonData.get_Item (System.String prop_name) [0x00000] in :0 在 LocalizationManager+c__Iterator0.MoveNext () [0x00000] in :0 在 UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in :0
也许你会问我在做什么。我正在做一个Choose Language Scenario.
正如你在图片上看到的,如果我选择韩国,它必须更改为韩国等等。
更多信息在这里是我的StreetUtility.LoadJsonFromStreamingAsset函数
public static string LoadJsonFromStreamingAssets(string path_with_extention_under_streaming_assets_folder)
{
string json = null;
try
{
//Android Platform
#if UNITY_ANDROID
string full_path = Application.persistentDataPath + path_with_extention_under_streaming_assets_folder;
WWW reader = new WWW(full_path);
while (!reader.isDone) { }
json = reader.text;
Debug.Log("Loaded Files: " + json);
#elif UNITY_IOS //IOS Platform
#elif UNITY_STANDALONE //PC Platform
string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
StreamReader reader = new StreamReader(full_path);
json = reader.ReadToEnd().Trim();
reader.Close();
Debug.Log(json);
#endif
}
catch (Exception e)
{
Debug.LogWarningFormat("Failed to Load.\n{0}\n{1}", e, path_with_extention_under_streaming_assets_folder);
}
return json;
}
这行代码也与错误有关。我很确定
#if UNITY_ANDROID
string full_path = Application.persistentDataPath + path_with_extention_under_streaming_assets_folder;
WWW reader = new WWW(full_path);
while (!reader.isDone) { }
json = reader.text;
Debug.Log("Loaded Files: " + json);
非常感谢您,如果您不懂我的英语,我会道歉。请问一下。再次感谢。
【问题讨论】:
-
在
StreetUtility.LoadJsonFromStreamingAsset上。 -
你记录的 json 字符串是什么?
-
@PeakCoder 先生,我登录是什么意思?
-
@mjwills hmmm 那么如何将 json 文件解析到 android 设备??