首页 > 解决方案 > 如何将图像保存到 playerprefs 并将它们显示到另一个场景?

问题描述

我是 Unity 的初学者。到目前为止,我所读到的,playerprefs 可以以字节形式存储图像。不过,我在这方面遇到了麻烦。因为,我在将 texture2d 转换为字节时出错。在其中,我使用此代码

Texture tex = Resources.Load("Sprites/white_black") as Texture2D;
byte[] texbyte = tex.EncodeToPNG();
// ^ this line always result as NullReferenceException on my console.

我有两个场景: Scene1 是要保存到 playerprefs 的图像,下面是一个示例:
在此处输入图像描述

在场景 2 中,所有保存在 playerprefs 中的图像都将使用如下图所示的按钮显示:
在此处输入图像描述

另外,如果您可以向我推荐其他解决方案,我会对其进行搜索。谢谢你。

标签: imageunity3d

解决方案


您不应该将图像保存在 PlayerPrefs 中,但您可以将精灵的名称保存到 PlayerPrefs,然后从资源中将其加载到另一个场景中,如下所示:Resources.Load(spriteName);


推荐阅读