c# - Unity List.Add 阻止 Android 上的 Sprite 更新

标签 c# android list unity-game-engine sprite

我有一个在按下按钮时调用的方法,

private List<Page> _pages = new List<Page>();
public void LoadKern(int requestedKern)
    {
        TextAsset pages = Resources.Load("kern" + requestedKern) as TextAsset;

        JSONArray jsonPages = JSON.Parse(pages.text)["pages"].AsArray;

        foreach (JSONNode page in jsonPages)
        {
            _pages.Add(new Page(page["image"], page["text"]));
        }

        ImageSpriteRenderer.sprite = Resources.Load<Sprite>(_pages[currentPage].image);
        TextSpriteRenderer.sprite = Resources.Load<Sprite>(_pages[currentPage].text);
    }

代码在模拟器中运行时工作完美,但每当我将其部署到 Android 设备或使用 Untiy Remote 4它不再更新 Sprite 。

每当我删除此行并手动设置资源时,按下按钮时它都会更新。

_pages.Add(new Page(page["image"], page["text"]));

这似乎很奇怪,它可以在桌面上运行,但不能在 Android 上运行,是我缺少什么吗?

最佳答案

我觉得很难放在评论里,所以写在这里作为答案。

验证 Resource.Load<> 导致的问题的最简单方法是添加以下代码:

TextAsset pages = Resources.Load("kern" + requestedKern) as TextAsset;
Debug.Load(pages + "are Loading");  // to see if it is really loaded successfully

连接您的设备并打开 adb 日志,用“正在加载”过滤消息。 如果您看到页面为 null,那么很明显 Resource.Load<> 就是罪魁祸首。

如果是Resource.Load<>问题,可以考虑使用StreamingAssets :

Any files placed in a folder called StreamingAssets in a Unity project will be copied verbatim to a particular folder on the target machine. You can retrieve the folder using the Application.streamingAssetsPath property. It’s always best to use Application.streamingAssetsPath to get the location of the StreamingAssets folder, it will always point to the correct location on the platform where the application is running.

在 Android 上,您应该使用:

path = "jar:file://" + Application.dataPath + "!/assets/";

我怀疑您加载文本资源是在运行时进行的,当您打包应用程序时,文本资源不会被读取,并且可能会被排除在项目之外,因为Unity认为这是“未使用”的。当您在 Android 上运行该应用程序时,失败是很自然的。

使用 StreamingAssets 方法,您可以强制 Unity“逐字”复制文本资源,从而确保它在运行时可访问!

关于c# - Unity List.Add 阻止 Android 上的 Sprite 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387414/

相关文章:

c# - Lambda 和 Foreach

c# - 每行运行 Lua 脚本

android - onStart 调用两次,中间没有 onStop?

android - 如何打开启动器的小部件选择器?

C#\避免内存不足异常

c# - 调用对象方法而不考虑参数类型

c# - 如何从键盘设置二维数组中的值

android - 为我的模拟器使用 Google Play 服务

list - 将标记数组添加到Flutter_Map仅在移动 map 后显示

r - Stata - 在字符之前提取数字,创建列表