c# - Unity Streaming Assets iOS 不工作
问题描述
我在 Unity 中制作了一个应用程序,该应用程序已在 Google Play Store 上架了一年多。今天我想在 Apple App Market 上部署它,但翻译在那里不起作用。
我使用以下代码加载我的应用翻译
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;
}
}
然而不知何故,所有文本字段都显示“未找到本地化文本”,因为它找不到我的资产......这可能是什么?
这是因为 StreamingAssets 文件夹没有复制到 xCode 吗?是因为iOS上的位置不同吗?还有什么?
解决方案
WWW
或UnityWebRequest
API 用于在 Android 上读取 StreamingAssets 上的文件。对于 iOS,应使用System.IO
命名空间中的任何 API,例如。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);
}
此外,该WWW
API 可用于协程函数,以便您可以等待或放弃它,直到下载完成。您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);
}
}
并像这样使用:
StartCoroutine(LoadLocalizedText(languageInstance, (status) =>
{
if (status)
{
//Success
}
}));
推荐阅读
- arrays - Numpy for 循环运行时间太长
- laravel - Laravel 5.4 Handler.php 从 Ajax 调用重定向
- jquery - 带有外部链接的 Fancybox 内嵌图像
- powershell - PS 调用命令和 writeallbyes
- python - spaCy NLP word.pos 返回数字而不是 POS 标签
- sql - 在 sql server 2008 R2 中解析 Tab 和 Spaces 时有什么区别
- java - 无法通过 getMethod() 找到公共方法
- docker - 错误的撰写文件版本
- java - 用weblogic中的系统属性替换注释
- bash - 在 groovy 中的 shell 内运行循环命令