首页 > 解决方案 > 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上的位置不同吗?还有什么?

这是我的文件夹结构 文件夹结构

标签: c#unity3d

解决方案


WWWUnityWebRequestAPI 用于在 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);
}

此外,该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);
    }
}

并像这样使用:

StartCoroutine(LoadLocalizedText(languageInstance, (status) =>
{
    if (status)
    {
        //Success
    }
}));

推荐阅读