亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我在Unity中的第一款C#游戲(介紹playerprefs)

我在Unity中的第一款C#游戲(介紹playerprefs)

C#
慕尼黑8549860 2022-08-20 16:58:22
在我的游戲中,有一個利潤計數由我的貨幣腳本中的addMoney變量控制,例如利潤計數= addMoney。當我添加關于我的addMoney變量的玩家pref時,它將profitCount默認為0,而實際上它應該是1。這是我的第一款游戲,所以很容易成為我誤解或忽視的一件小事。錢計數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);    }}利潤計數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);    }}
查看完整描述

2 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

兩件事:

  1. 不要用于存儲保存數據。它不是為此而生的。它旨在保存播放器首選項,例如音量,全屏模式或輸入類型。使用的文件是純文本,不支持復雜的數據類型。PlayerPrefsPlayerPrefs

  2. 如果不存在保存數據,則讀出的值為零(至少從 PlayerPrefs 中讀取)。您需要對此負責,而目前您不是。當您改用其他保存方法時,您會收到其他錯誤,例如空指針或未找到文件。您必須確定保存是否存在,并且僅當存在保存時,才應從中讀取。


查看完整回答
反對 回復 2022-08-20
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

好的,所以你在初始化期間分配了默認值,即addMoneypublic int addMoney = 1;

但是,在開始時,您再次為它分配了一個尚未保存的值。addMoney = PlayerPrefs.GetInt("addmoney");

如果要創建記錄,請按如下方式操作PlayerPrefs.SetInt("addmoney",addmoney);


查看完整回答
反對 回復 2022-08-20
  • 2 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號