在我的第一个C#游戏中需要一些帮助(介绍playerprefs)

时间:2019-02-01 17:48:00

标签: c# unity3d

在我的游戏中,有一个获利计数,该获利计数由我的money脚本ex中的addMoney变量控制。 profitcount = addMoney,无论如何,当我添加有关我的addMoney变量的玩家偏好设置时,它默认将ProfitCount设置为0时(实际上应为1),非常感谢,因为这是我的第一场比赛,这很容易是一件小事我被误解或忽视了。

moneyCount

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);

    }
}

shopManager

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 个答案:

答案 0 :(得分:2)

两件事:

  1. 请勿使用PlayerPrefs存储保存数据。这不是为了那个。它用于保存播放器的首选项,例如音量,全屏模式或输入类型。 PlayerPrefs使用的文件是纯文本,并且不支持复杂的数据类型。

  2. 如果不存在保存数据,则读出的值为零(至少从PlayerPrefs中读出)。您需要考虑这一点,而目前还没有。当您使用其他保存方法时,您会遇到其他错误,例如空指针或找不到文件。您必须确定保存是否存在,并且只有存在时,您才应该从中读取。

答案 1 :(得分:0)

好的,因此您在初始化期间为addMoney分配了默认值,即public int addMoney = 1;

但随后你开始你又赋予它已尚未保存的值addMoney = PlayerPrefs.GetInt("addmoney");

如果您要创建记录,请执行以下操作PlayerPrefs.SetInt("addmoney",addmoney);