0
我想在游戏中制作一个简单的角色店。我做了4个UI按钮,每个按钮在点击时调用BuySkin函数。这是通用脚本,我把这些按钮中的每一个,然后我只是调整了像价格这样的变量。我在保存店铺状态方面遇到问题。我想到的第一件事就是这个。但是当我购买一个角色然后重新开始游戏时,所有角色都会解锁。有什么建议么?储蓄店状态
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Button : MonoBehaviour {
public int price = 100;
public int bought = 1;
public AnimatorOverrideController overrideAnimator;
private PlayerController player;
private Text costText;
void Start()
{
player = GameObject.FindObjectOfType<PlayerController>();
costText = GetComponentInChildren<Text>();
costText.text = price.ToString();
Load();
if (bought == 2)
{
costText.enabled = false;
}
else
{
costText.enabled = true;
}
}
public void BuySkin()
{
if(CoinManager.coins >= price)
{
if(bought == 1)
{
player.GetComponent<Animator>().runtimeAnimatorController = overrideAnimator;
bought = 2;
CoinManager.coins -= price;
costText.enabled = false;
Save();
}
}
if (bought == 2)
{
player.GetComponent<Animator>().runtimeAnimatorController = overrideAnimator;
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.shopstates");
ShopData data = new ShopData();
data.bought = bought;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.shopstate"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.shopstates", FileMode.Open);
ShopData data = (ShopData)bf.Deserialize(file);
file.Close();
bought = data.bought;
}
}
}
[Serializable]
class ShopData
{
public int bought;
}