Wou*_*tte 0 c# unity-game-engine
我用 Unity 制作了一个应用程序,该应用程序已在 Google Play 商店上架一年多了。今天我想将其部署到苹果应用市场,但翻译在那里不起作用。
我使用以下代码加载我的应用程序翻译
public bool LoadLocalizedText(SystemLanguage language)
{
localizedText = new Dictionary<string, string>();
string filePath = Path.Combine(Application.streamingAssetsPath, "localizedText_" + language + ".json");
WWW reader = new WWW(filePath);
if (reader.text == null || reader.text == "") return false;
while (!reader.isDone) { }
string dataAsJson;
dataAsJson = reader.text;
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");
if (localizedText.Count == 0) return false;
else
{
isReady = true;
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
然而不知何故,所有文本字段都显示“未找到本地化文本”,因为它找不到我的资产......这可能是什么?
这是因为 StreamingAssets 文件夹没有复制到 xCode 吗?是因为iOS上的位置不同吗?还有别的事吗?
WWW或APIUnityWebRequest用于读取 Android 上 StreamingAssets 上的文件。对于 iOS,应使用命名空间中的任何 API,System.IO例如。System.IO.File.ReadAllText
像这样的东西:
IEnumerator ReadFromStreamingAssets()
{
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
string result = "";
if (filePath.Contains("://"))
{
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
result = www.downloadHandler.text;
}
else
result = System.IO.File.ReadAllText(filePath);
}
Run Code Online (Sandbox Code Playgroud)
此外,该WWWAPI 被设计为在协程函数中使用,以便您可以等待或放弃它,直到下载完成。你while (!reader.isDone) { }可以冻结 Unity。那应该是 while (!reader.isDone) yield return null;等待每一帧直到下载完成。该LoadLocalizedText函数还必须是协程函数,因此返回类型应该是IEnumerator而不是bool。要使其bool也返回,请用作 Action<bool>参数。
解决这两个问题后,新代码应如下所示:
public IEnumerator LoadLocalizedText(SystemLanguage language, Action<bool> success)
{
localizedText = new Dictionary<string, string>();
string filePath = Path.Combine(Application.streamingAssetsPath, "localizedText_" + language + ".json");
string dataAsJson;
//Android
if (filePath.Contains("://"))
{
WWW reader = new WWW(filePath);
//Wait(Non blocking until download is done)
while (!reader.isDone)
{
yield return null;
}
if (reader.text == null || reader.text == "")
{
success(false);
//Just like return false
yield break;
}
dataAsJson = reader.text;
}
//iOS
else
{
dataAsJson = System.IO.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");
if (localizedText.Count == 0)
success(false);
else
{
isReady = true;
success(true);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用:
StartCoroutine(LoadLocalizedText(languageInstance, (status) =>
{
if (status)
{
//Success
}
}));
Run Code Online (Sandbox Code Playgroud)