将 money.text 放在我的商店场景中 [英] Putting money.text in my shop scene

查看:21
本文介绍了将 money.text 放在我的商店场景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 money.text 在我的商店场景中购买一些盾牌.出于某种原因,我的 money.text 仅在另一个场景中,而没有出现在商店场景中.

I want my money.text to be in my shop scene to buy some shield. For some reason my money.text is only on the other scene and it's not going in the shop scene.

这是我的商店脚本.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ShopController : MonoBehaviour {

int MoneyAmount;

int isPowerup1Sold,isPowerup2Sold,isPowerup3Sold;

public Text MoneyAmountText;

public Button Buynow1,Buynow2,Buynow3;

// Use this for initialization
void Start () {
    MoneyAmount = PlayerPrefs.GetInt ("moneyAmount");
}

// Update is called once per frame
void Update () {

    MoneyAmountText.text = "Money : " + MoneyAmount.ToString ();

    isPowerup1Sold = PlayerPrefs.GetInt ("isPowerup1Sold");
    isPowerup2Sold = PlayerPrefs.GetInt ("isPowerup2Sold");
    isPowerup3Sold = PlayerPrefs.GetInt ("isPowerup3Sold");

    if (MoneyAmount >= 50 && isPowerup1Sold == 0)
        Buynow1.interactable = true;
    else
        Buynow1.interactable = false;

    if (MoneyAmount >= 70 && isPowerup2Sold == 0)
        Buynow2.interactable = true;
    else
        Buynow2.interactable = false;

    if (MoneyAmount >= 120 && isPowerup3Sold == 0)
        Buynow3.interactable = true;
    else
        Buynow3.interactable = false;
}

public void buyPowerup1()
{
    MoneyAmount -= 50;
    PlayerPrefs.SetInt ("isPowerup1Sold", 1);
}

public void buyPowerup2()
{
    MoneyAmount -= 70;
    PlayerPrefs.SetInt ("isPowerup2Sold", 1);
}

public void buyPowerup3()
{
    MoneyAmount -= 120;
    PlayerPrefs.SetInt ("isPowerup3Sold", 1);
}
public void exitshop()
{
    PlayerPrefs.SetInt ("moneyAmount", MoneyAmount);
    Application.LoadLevel ("levelselect");
}

}

这是我的金钱脚本.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Money : MonoBehaviour {

public Text MoneyText;
public static int MoneyAmount = 0;
// Use this for initialization
void Start () {
    MoneyAmount = PlayerPrefs.GetInt ("moneyAmount",0);

}

// Update is called once per frame
void Update () {
    MoneyText.text = "Money" + MoneyAmount.ToString();
}
}

这是我的 shield.power 脚本

this is my shield.power script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Money : MonoBehaviour {

public Text MoneyText;
public static int MoneyAmount = 0;
// Use this for initialization
void Start () {
    MoneyAmount = PlayerPrefs.GetInt ("moneyAmount",0);

}

// Update is called once per frame
void Update () {
    MoneyText.text = "Money" + MoneyAmount.ToString();
}
}

这是我把我的钱.文本放在画布场景中的地方.(高亮蓝色).

This is where i put my money.text to show in the canvas scene.(the highlight blue color).

推荐答案

Text 是一个 UI 组件,用于在 UI 上呈现字符串.每个场景都需要有自己的文本组件.然后您需要将金额保存在一个地方,以便每个处理金钱的脚本都没有自己的 MoneyAmount 变量.Money 脚本可能如下所示:

Text is an UI component for rendering strings on UI. Each scene need to have its own Text-component. Then you need to keep the amount of money in one place so that every script dealing with money does not have its own MoneyAmount variable. Money script could look like this:

public class Money : MonoBehaviour {

    static int? cachedAmount = null;
    const string playerPrefsKeyName = "moneyAmount";
    const int startMoney = 0;

    public static int Amount {
        get {
            if (cachedAmount == null) {
                cachedAmount = PlayerPrefs.GetInt (playerPrefsKeyName, startMoney);
            }
            return cachedAmount.Value;
        }
        set {
            if (cachedAmount == null || value != cachedAmount.Value) {
                PlayerPrefs.SetInt (playerPrefsKeyName, value);
                cachedAmount = value;
            }
        }
    }
}

...只要你用钱,你就可以用

...and whenever you use money, you could use

if (Money.Amount >= 70) {
    Money.Amount -= 70;
    MoneyAmountText.text = "Money : " + Money.Amount.ToString ();
}

这篇关于将 money.text 放在我的商店场景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆