第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

C#
慕尼黑8549860 2022-08-20 16:58:22
在我的游戲中,有一個利潤計數(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);    }}
查看完整描述

2 回答

?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

兩件事:

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

  2. 如果不存在保存數(shù)據(jù),則讀出的值為零(至少從 PlayerPrefs 中讀?。D枰獙Υ素撠?zé),而目前您不是。當(dāng)您改用其他保存方法時,您會收到其他錯誤,例如空指針或未找到文件。您必須確定保存是否存在,并且僅當(dāng)存在保存時,才應(yīng)從中讀取。


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

TA貢獻1895條經(jīng)驗 獲得超3個贊

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

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

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


查看完整回答
反對 回復(fù) 2022-08-20
  • 2 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號