如何将布尔值保存到 PlayerPrefs Unity

How to save bool to PlayerPrefs Unity(如何将布尔值保存到 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.SetIntPlayerPrefs.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)