通过播放器播放器保存Unity被窃听



对不起,我已经包含了所有代码,但我真的找不到错误:我播放并保存,在我用BuyTaxesBuymarket购买东西并退出并停止它并再次播放后,它会显示所有UI显示,所有市场价格和折扣使用都设置为0。但我不明白为什么。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameScript : MonoBehaviour
{
public Text GoldText;
public Text ArmyPowerText;
public Text PlayerNametext;
public Text Button1Text;
public Text Button2Text;
public int gold;
public int armypower;
public string PlayerName;
public int Level;
public int Income;
public int incomeboost;
public int discount;
public float lvlremainder;

public GameObject ButtonHolder;
public Slider LevelBar;
public float LevelProgress;
private float FillSpeed = 0.5f;

public GameObject UIHolder;
public GameObject StartForm;
public InputField NameInput;
public GameObject ArmyUI;
public GameObject InvestUI;
public GameObject LaboratoryUI;
private float time = 0.0f;
public float IncomePerdiod = 5f;

public bool GameStart = true;
//Invest Prices
//Market
public int marketprice;
public int marketlevel;
public Text marketpricetext;
public Text markettext;
//Taxes
public int taxesprice;
public int taxeslevel;
public Text taxespricetext;
public Text taxestext;

void Start()
{
Load();
if(GameStart == true)
{
gold = 100;
armypower = 0;
Level = 1;
LevelProgress = 0;
//Laboratory
discount = 1;
Income = 1;
incomeboost = 1;
//Invest
marketlevel = 0;
taxeslevel = 0;
ArmyUIHide();
InvestUIHide();
ButtonHide();
UIHide();
StartForm.SetActive(true);
Save();
}
if (GameStart == false)
{
StartForm.SetActive(false);
ArmyUIHide();
InvestUIHide();
ButtonShow();
UIShow();
Save();
}
}

void Update()
{
if(time >= IncomePerdiod)
{
time = 0.0f;
gold += Income * incomeboost;
}
time += Time.deltaTime;
Save();
GoldText.text = "Gold: " + gold;
ArmyPowerText.text = "Army Power: " + armypower;
PlayerNametext.text = PlayerName + " LVL " + Level;
//Market
markettext.text = "Market LVL " + marketlevel; 
marketpricetext.text = marketprice.ToString();
marketprice = 50 * discount;
//Taxes
taxestext.text = "Taxes LVL " + taxeslevel;
taxespricetext.text = taxesprice.ToString();
taxesprice = 250 * discount;
if (LevelBar.value < LevelProgress)
{
LevelBar.value += FillSpeed * Time.deltaTime;
}
if (LevelBar.value > LevelProgress)
{
LevelBar.value = LevelProgress;
}
if (LevelProgress >= 1)
{
Level++;
LevelProgress = 0;
}
}
public void Save()
{
//UI
PlayerPrefs.SetString("gold", gold.ToString());
PlayerPrefs.SetString("armypower", armypower.ToString());
PlayerPrefs.SetString("GameStart", GameStart.ToString());
PlayerPrefs.SetString("PlayerName", PlayerName.ToString());
PlayerPrefs.SetString("Level", Level.ToString());
PlayerPrefs.SetString("LevelProgress", LevelProgress.ToString());
//Laboratory
PlayerPrefs.SetString("discount", discount.ToString());
PlayerPrefs.SetString("Income", Income.ToString());
PlayerPrefs.SetString("incomeboost", incomeboost.ToString());
//Invest
PlayerPrefs.SetString("marketlevel", marketlevel.ToString());
PlayerPrefs.SetString("taxeslevel", taxeslevel.ToString());
}
public void Load()
{
//UI
gold = int.Parse(PlayerPrefs.GetString("gold", "100"));
armypower = int.Parse(PlayerPrefs.GetString("armypower", "0"));
GameStart = bool.Parse(PlayerPrefs.GetString("GameStart", "true"));
PlayerName = PlayerPrefs.GetString("PlayerName", "Guest");
Level = int.Parse(PlayerPrefs.GetString("Level", "1"));
LevelProgress = int.Parse(PlayerPrefs.GetString("LevelProgress", "0"));
//Laboratory
discount = int.Parse(PlayerPrefs.GetString("discount", "1"));
Income = int.Parse(PlayerPrefs.GetString("Income", "1"));
incomeboost = int.Parse(PlayerPrefs.GetString("incomeboost", "1"));
//Invest
marketlevel = int.Parse(PlayerPrefs.GetString("marketlevel", "50"));
taxeslevel = int.Parse(PlayerPrefs.GetString("taxeslevel", "250"));
}
public void ButtonHide()
{
ButtonHolder.SetActive(false);
}
public void ButtonShow()
{
ButtonHolder.SetActive(true);
}
public void UIHide()
{
UIHolder.SetActive(false);
}
public void UIShow()
{
UIHolder.SetActive(true);
}
public void ArmyUIShow()
{
ArmyUI.SetActive(true);
}
public void ArmyUIHide()
{
ArmyUI.SetActive(false);
}
public void LaboratoryUIShow()
{
LaboratoryUI.SetActive(true);
}
public void LaboratoryUIHide()
{
LaboratoryUI.SetActive(false);
}
public void InvestUIShow()
{
InvestUI.SetActive(true);
}
public void InvestUIHide()
{
InvestUI.SetActive(false);
}
public void EnterName()
{
PlayerName = NameInput.text;
StartForm.SetActive(false);
UIShow();
GameStart = false;
ButtonShow();
}
public void ArmyClick()
{
ArmyUIShow();
ButtonHide();
LaboratoryUIHide();
InvestUIHide();
}
public void InvestClick()
{
InvestUIShow();
ButtonHide();
LaboratoryUIHide();
ArmyUIHide();
}
public void LaboratoryClick()
{
ArmyUIHide();
ButtonHide();
LaboratoryUIShow();
InvestUIHide();
}
public void Home()
{
ArmyUIHide();
InvestUIHide();
ButtonShow();
LaboratoryUIHide();
}

//Buy Invest
public void BuyMarket()
{
if(gold >= marketprice && marketlevel < 5)
{
marketlevel++;
Income += 1;
gold -= marketprice;
if (LevelProgress < 1f - 0.05f)
{
LevelProgress += 0.15f;
}
else
{
lvlremainder += (LevelProgress + 0.05f - 1f);
Level++;
LevelProgress = 0;
LevelProgress += lvlremainder;
}
}
}
public void BuyTaxes()
{
if (gold >= taxesprice && taxeslevel < 10)
{
taxeslevel++;
Income += 3;
gold -= taxesprice;
if (LevelProgress < 1f - 0.15f)
{
LevelProgress += 0.15f;
}
else 
{
lvlremainder += (LevelProgress + 0.15f - 1f);
Level++;
LevelProgress = 0;
LevelProgress += lvlremainder;
}
}
}

代码中的保存系统开发不正确,请从StartUpdate方法中删除Save()方法调用,并仅在必要的事件发生时调用,例如游戏结束。

这样做本地道具来保存数据:

private int Gold
{
get => PlayerPrefs.GetInt("gold");
set => PlayerPrefs.SetInt("gold", value);
}

我发现了一个错误,我将其解析为int,用于一个将整个存储系统搞砸的float!

相关内容

  • 没有找到相关文章

最新更新