【问题标题】:My first C# game in Unity (introducing playerprefs)我在 Unity 中的第一个 C# 游戏(介绍 playerprefs)
【发布时间】:2019-08-04 00:58:31
【问题描述】:

在我的游戏中,有一个利润计数,它由我的货币脚本 ex 中的 addMoney 变量控制。利润计数 = addMoney。

当我添加有关我的 addMoney 变量的玩家首选项时,它会将 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;


            }



        }
    }














}

【问题讨论】:

  • 尝试从您的moneyCount脚本中删除addMoney = PlayerPrefs.GetInt("addmoney");。然后运行它并在这里告诉它是否有效
  • 是的,当我删除播放器首选项时它可以工作,这就是我没有它时遇到的问题,它从 1 开始并正常上升,但是当我添加播放器首选项功能时,它从零开始,因此在游戏中是不可能赚钱的
  • 你明白这个问题了吗?
  • @thirteen3054 你应该把它制定出来并添加它作为答案;)
  • 移除播放器偏好可以解决问题,但是当游戏关闭并重新打开时该值不会保存

标签: c# unity3d


【解决方案1】:

两件事:

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

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

【讨论】:

    【解决方案2】:

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

    但是在您开始时,您再次为其分配了一个尚未保存的值addMoney = PlayerPrefs.GetInt("addmoney");

    如果您想创建记录,请这样做PlayerPrefs.SetInt("addmoney",addmoney);

    【讨论】:

    • 您不需要在调用 get 之前设置首选项,GetInt 的重载允许您指定默认值。因此无需在调用 GetInt 之前调用 SetInt。
    • @Eddge 什么不会让这个答案出错 ;) 因为 OP 的错误是在存储正确值之前用 0 覆盖本地起始值 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多