Unity3d 如何在Playerprefs上正确保存布尔值?

Unity3d 如何在Playerprefs上正确保存布尔值?,unity3d,Unity3d,我想制作一个商店系统如果我购买了物品,物品将保存在Playerprefs,当我重新启动或重新打开游戏时,无需再次购买,因为我已经进入setInt,但我不知道何时调用getInt。我试图用一个新的int索引在start方法中调用它,但它不起作用。这是我的剧本 用于存储项目类型 [System.Serializable] public class m_ShopItem { public Sprite theIcon; public int price; public boo

我想制作一个商店系统如果我购买了物品,物品将保存在
Playerprefs
,当我重新启动或重新打开游戏时,无需再次购买,因为我已经进入
setInt
,但我不知道何时调用
getInt
。我试图用一个新的int索引在start方法中调用它,但它不起作用。这是我的剧本

用于存储项目类型

[System.Serializable]
public class m_ShopItem 
{
    public Sprite theIcon;
    public int price;
    public bool isBuyed;
}
然后我列一个清单

public List<m_ShopItem> ShopItemsList = new List<m_ShopItem>();
 [SerializeField] GameObject ShopPanel;
    [SerializeField] Transform theContent;
    GameObject g;
    Button buyBtn;
    public GameObject itemTemplate;

    private void Start()
    {

        for (int i = 0; i < ShopItemsList.Count; i++)
        {

            g = Instantiate(itemTemplate, theContent);
            g.transform.GetChild(0).GetComponent<Image>().sprite = ShopItemsList[i].theIcon;
            g.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = ShopItemsList[i].price.ToString();
            buyBtn = g.transform.GetChild(2).GetComponent<Button>();
            if (ShopItemsList[i].isBuyed)
            {
                DisableBuyButton();
            }
            ShopItemsList[i].isBuyed = PlayerPrefs.GetInt("isBuyed") == 1 ? true : false;// i call it Get here 
            Debug.Log(ShopItemsList[i].isBuyed);
            buyBtn.AddEventListener(i, OnShopItemBtnClicked);
        }
    }
    void OnShopItemBtnClicked(int itemIndex)
    {
        if (m_shopManager.Instance.HasEnoughCoins(ShopItemsList[itemIndex].price))
        {
            m_shopManager.Instance.UseCoins(ShopItemsList[itemIndex].price);
            //purchase Item
            ShopItemsList[itemIndex].isBuyed = true;
            PlayerPrefs.SetInt("isBuyed", ShopItemsList[itemIndex].isBuyed ? 1 : 0); // SetThe Int here when button clicked and buy the item

            //disable the button
            buyBtn = theContent.GetChild(itemIndex).GetChild(2).GetComponent<Button>();
            DisableBuyButton();

            //add avatar
            m_Profile.Instance.AddBallType(ShopItemsList[itemIndex].theIcon);
        }
        else
        {
            //NoCoinsAnim.SetTrigger("NoCoins");
            Debug.Log("You don't have enough coins!!");
        }
    }

    void DisableBuyButton()
    {
        buyBtn.interactable = false;
        buyBtn.transform.GetChild(0).GetComponent<Text>().text = "PURCHASED";
    }
public List ShopItemsList=new List();
[SerializeField]游戏对象商店面板;
[序列化字段]转换内容;
游戏对象g;
按钮buyBtn;
公共游戏对象项目模板;
私有void Start()
{
for(int i=0;i

但结果是一样的,当我进入主菜单或重新启动游戏时,我必须重新调整


有人能告诉我脚本有什么问题吗?

您当前的问题是,您只创建了一个值等于1或0的对象

因此,如果你只购买一件商店商品,所有商品都会解锁。因为您只检查是
true
(1)还是
false
(0)

要解决这个问题,我们需要创建一个与商店商品数量相等的唯一列表。如果我们将
ShopItemsList
中的索引添加到
isBuyed
名称中,我们就可以做到这一点

创建初始播放器参考:

//在Start函数之前调用
私人空间{
//循环浏览所有购物项目
for(int i=0;i
现在我们可以检查它,而不是在
Start()
中只检查一次

检查是否购买了物品:

private void Start(){
//循环浏览所有购物项目
for(int i=0;i
当我们从商店购买产品时,我们还需要确保设置正确。为了确保只需添加从函数中获得的
itemIndex
参数,并将其添加到
isBuyed
名称中即可

购买物品:

void onshopItemBtclicked(int itemIndex){
...
//购买物品。
ShopItemsList[itemIndex].isBuyed=true;
PlayerPrefs.SetInt(“isBuyed”+itemIndex,ShopItemsList[itemIndex].isBuyed?1:0);
...
}

您需要调用
ShopItemsList[i].isBuyed=PlayerPrefs.GetInt(“isBuyed”)==1?真:假
在检查
ShopItemsList[i]的值之前。isBuyed
是的,如果我在all item.isBuyed之前检查,当我单击buy
PlayerPrefs.SetInt(“isBuyed”,ShopItemsList[itemIndex]。isBuyed?1:0)时,将在@MathewHDso购买所有列出的物品be
PlayerPrefs.SetInt(“isBuyed”+ItemIndex,ShopItemsList[ItemIndex]。isBuyed?1:0)太对了?是的,我会添加更多的文字来解释。谢谢,但它仍然没有保存,可能是因为在唤醒时创建初始的playerPref,这样它会在重新启动游戏时重置。是的,sry
awake
只是一个函数,它将在
开始之前调用。因此,如果已经设置了,则需要添加一个检查。我编辑了我的答案,将其也包括在内。