image - 如何将图像保存到 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 中的图像都将使用如下图所示的按钮显示:
另外,如果您可以向我推荐其他解决方案,我会对其进行搜索。谢谢你。
解决方案
您不应该将图像保存在 PlayerPrefs 中,但您可以将精灵的名称保存到 PlayerPrefs,然后从资源中将其加载到另一个场景中,如下所示:Resources.Load(spriteName);
推荐阅读
- python - Matplotlib 散点图传奇创造之谜
- android - 获取钩子方法的参数值
- python - 为什么软件包没有安装到我的 Mac 上的两个 Python 版本(在 /User/name/Library 和 /usr/local/bin 中)?
- c# - 如何在 RichTextBox 中使用 TextPointer.GetLineStartPosition() 获取下一行
- c# - 读取大文件 - 2GB+ 用于 Google Drive API 上传
- javascript - 将某些 CSV 数据加载到 HTML 文本区域中
- excel - 无法将保存在 VBA 中的公式作为字符串变量插入到单元格中
- c - 如何使用 realloc 向数组添加更多空格?
- javascript - Highcharts:将图表对象作为 Javascript 变量发送?
- ios - 如果它们是通过加载 .json 数据进行初始化的,如何在 init() 中初始化变量?