Unity Streaming Assets iOS 不工作 [英] Unity Streaming Assets iOS not working

查看:43
本文介绍了Unity Streaming Assets iOS 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Unity 中制作了一款应用,该应用已在 Google Play 商店上架了一年多.今天我想在 Apple App Market 上部署它,但翻译在那里不起作用.

I've made an app in Unity which has been on the Google Play Store for over a year. Today I wanted to deployt it on Apple App Market but the translations don't work there.

我使用以下代码加载我的应用翻译

I use the following code to load my app translations

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;
    }
}

但不知何故,所有文本字段都显示未找到本地化文本",因为它找不到我的资产......这可能是什么?

Yet somehow, all text fields display "Localized Text Not Found" because it cannot find my Assets... what could this be?

这是因为 StreamingAssets 文件夹没有复制到 xCode 吗?是因为iOS上的位置不同吗?还有什么?

Is this because the StreamingAssets folder isn't copied to xCode? Is it because the location is different on iOS? Something else?

这是我的文件夹结构

推荐答案

WWWUnityWebRequest API 用于读取 Android 上 StreamingAssets 上的文件.对于 iOS,应该使用 System.IO 命名空间中的任何 API,例如 System.IO.File.ReadAllText.

The WWW or UnityWebRequest API is used to read files on the StreamingAssets on Android. For iOS, The any API from the System.IO namespace such as System.IO.File.ReadAllText should be used.

像这样:

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 作为参数.


Also, the WWW API is made to be used in a coroutine function so that you can wait or yield it until the download is complete. Your while (!reader.isDone) { } can freeze Unity. That should be while (!reader.isDone) yield return null; which waits every frame until download is finished. The LoadLocalizedText function must also be a coroutine function so the return type should be IEnumerator instead of bool. To make it return bool too, use Action<bool> as parameter.

解决这两个问题后,下面是新代码的样子:

After fixing both issues, below is what the new code should look like:

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
    }
}));

这篇关于Unity Streaming Assets iOS 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆