How to save bool to PlayerPrefs Unity(如何将布尔值保存到 PlayerPrefs Unity)
问题描述
我为我的游戏设置了一个支付系统,这是我的代码:
I have a payment system for my game set up here's my code :
void Start()
{
T55.interactable = false;
Tiger2.interactable = false;
Cobra.interactable = false;
}
public void ProcessPurchase (ShopItem item)
{
if(item .SKU =="tank")
{
StoreHandler .Instance .Consume (item );
}
}
public void OnConsumeFinished (ShopItem item)
{
if(item .SKU =="tank")
{
T55.interactable = true;
Tiger2.interactable = true;
Cobra.interactable = true;
}
}
现在,每次玩家在游戏中购买东西时,我的 3 个按钮的难处理性都会变为现实;但问题是每次他关闭游戏时,难处理性都会回到错误的方式.
Now each time the player buy something in the game the intractability of my 3 buttons goes to true; but the problem is each time he closes the game the intractability goes back to false how.
我应该保存这个过程,这样玩家就不必再次购买来将它们设置为 true 吗?
Should I save the process so the player doesn't have to buy again to set them back to true?
推荐答案
PlayerPrefs 确实没有布尔类型的重载.它只支持字符串、整数和浮点数.
PlayerPrefs does not have an overload for a boolean type. It only supports string, int and float.
您需要创建一个函数,将 true
转换为 1
并将 false
转换为 0
然后 PlayerPrefs.SetInt
和 PlayerPrefs.GetInt
采用 int
类型的重载.
You need to make a function that converts true
to 1
and false
to 0
then the the PlayerPrefs.SetInt
and PlayerPrefs.GetInt
overload that takes int
type.
类似这样的:
int boolToInt(bool val)
{
if (val)
return 1;
else
return 0;
}
bool intToBool(int val)
{
if (val != 0)
return true;
else
return false;
}
现在,您可以轻松地将 bool
保存到 PlayerPrefs
.
Now, you can easily save bool
to PlayerPrefs
.
void saveData()
{
PlayerPrefs.SetInt("T55", boolToInt(T55.interactable));
PlayerPrefs.SetInt("Tiger2", boolToInt(T55.interactable));
PlayerPrefs.SetInt("Cobra", boolToInt(T55.interactable));
}
void loadData()
{
T55.interactable = intToBool(PlayerPrefs.GetInt("T55", 0));
Tiger2.interactable = intToBool(PlayerPrefs.GetInt("Tiger2", 0));
Cobra.interactable = intToBool(PlayerPrefs.GetInt("Cobra", 0));
}
如果您要保存许多变量,请使用 Json 和 PlayerPrefs 而不是单独保存和加载它们.这里是如何做到这一点的.
If you have many variables to save, use Json and PlayerPrefs instead of saving and loading them individually. Here is how to do that.
这篇关于如何将布尔值保存到 PlayerPrefs Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将布尔值保存到 PlayerPrefs Unity
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01