在我的游戲中,有一個利潤計數(shù)由我的貨幣腳本中的addMoney變量控制,例如利潤計數(shù)= addMoney。當(dāng)我添加關(guān)于我的addMoney變量的玩家pref時,它將profitCount默認為0,而實際上它應(yīng)該是1。這是我的第一款游戲,所以很容易成為我誤解或忽視的一件小事。錢計數(shù)public class moneyCount : MonoBehaviour{ float timeTillAdd = 1; public int addMoney = 1; public int money; public Text txt; // Start is called before the first frame update void Start() { money = PlayerPrefs.GetInt("Money"); addMoney = PlayerPrefs.GetInt("addmoney"); } // Update is called once per frame void Update() { PlayerPrefs.SetInt("addmoney", addMoney); if (Time.time >= timeTillAdd) { money += addMoney; timeTillAdd++; } txt.text = money.ToString(); PlayerPrefs.SetInt("Money", money); }}利潤計數(shù)using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class profitCount : MonoBehaviour{ public int profitAmount; public GameObject moneyManagerObj; moneyCount mc; public Text txt; // Start is called before the first frame update void Start() { mc = moneyManagerObj.GetComponent<moneyCount>(); // profitAmount = PlayerPrefs.GetInt("profitamount"); } // Update is called once per frame void Update() { profitAmount = mc.addMoney; txt.text = profitAmount.ToString(); // PlayerPrefs.SetInt("profitamount", profitAmount); }}
我在Unity中的第一款C#游戲(介紹playerprefs)
慕尼黑8549860
2022-08-20 16:58:22