代码之家  ›  专栏  ›  技术社区  ›  xnx

我在unity的第一个c游戏需要一些帮助(介绍playerprefs)

  •  1
  • xnx  · 技术社区  · 6 年前

    当我添加关于addMoney变量的player 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);
    
        }
    }
    

    店长

    public class ShopManager : MonoBehaviour
    {
    
        public int item1_cost = 50;
        public int item1_upgrade = 5;
        public int item1_tier = 1;
        public int item2_cost = 50;
        public int item2_upgrade = 5;
        public GameObject moneyManagerObj;
        moneyCount mc;
    
        public Text txt;
        public Text item1;
        public Text item1_text;
    
        // Start is called before the first frame update
        void Start()
        {
    
    
            mc = moneyManagerObj.GetComponent<moneyCount>();
            item1_cost = PlayerPrefs.GetInt("Item1_cost");
            //item1_upgrade = PlayerPrefs.GetInt("Item1_upgrade");
            item1_tier = PlayerPrefs.GetInt("Item1_tier");
    
    
        }
    
        // Update is called once per frame
        void Update()
        {
            item1.text = item1_tier.ToString();
    
            PlayerPrefs.SetInt("Item1_cost", item1_cost);
            //  PlayerPrefs.SetInt("Item1_upgrade", item1_upgrade);
            PlayerPrefs.SetInt("Item1_tier", item1_tier);
    
            if (item1_tier > 0)
            {
    
    
    
                item1_text.text = ("Upgrade");
    
    
    
    
    
            }
    
    
        }
    
    
    
    
        public void on_click()
    
    
        {
    
    
            {
    
                if (mc.money >= item1_cost)
                {
    
    
    
    
                    mc.money -= item1_cost;
                    mc.addMoney += item1_upgrade;
                    item1_tier += 1;
                    item1.text = item1_tier.ToString();
                    item1_cost += 50 * item1_tier;
    
    
                }
    
    
    
            }
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
    
    2 回复  |  直到 5 年前
        1
  •  3
  •   Community Nick Dandoulakis    4 年前

    1. 不要使用 PlayerPrefs 存储保存数据。这不是故意的。这是为了救玩家 例如音量、全屏模式或输入类型。使用的文件 纯文本

    2. 如果不存在保存数据,则读取的值为零(至少从PlayerPrefs读取)。你需要对此作出解释,但目前你还没有。当您转移到另一种保存方法时,您将得到其他错误,如空指针或文件未找到。您必须确定是否存在保存 只有这样,

        2
  •  0
  •   thirteen4054    6 年前

    好的,所以您将默认值赋给 addMoney 即初始化期间 public int addMoney = 1;

    但是在你开始的时候,你又给它分配了一个尚未保存的值 addMoney = PlayerPrefs.GetInt("addmoney");

    PlayerPrefs.SetInt("addmoney",addmoney);