Unity3d中的基本购买系统

时间:2016-05-05 19:43:51

标签: c# unity3d unityscript unity5 unity3d-2dtools

在我的游戏中存在获得COIN的机会,有一定数量可以释放新皮肤。

目前硬币分数正确存储。

我有UI画布,其中有皮肤选项,我想知道如果玩家有足够的硬币如何购买这些皮肤,或者如果没有足够的东西没有任何反应。

请遵守以下代码。

CoinScore

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BeeCoinScore: MonoBehaviour
{

    public static BeeCoinScore instance;

    public static int coin = 0;  
    public int currentCoin = 0;
    string highScoreKey = "totalCoin";

    Text CoinScore;                      // Reference to the Text component.


    void Awake ()
    {
        // Set up the reference.
        CoinScore = GetComponent <Text> ();

    }
    void Start(){

        //Get the highScore from player prefs if it is there, 0 otherwise.
        coin = PlayerPrefs.GetInt(highScoreKey, 0);    
    }
    public void AddBeeCoinScore (int _point) {

        coin += _point;
        GetComponent<Text> ().text = "Bee Coins: " + coin;

    }


    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        CoinScore.text = "Bee Coins: " + coin;
    }


    void OnDisable(){

        //If our scoree is greter than highscore, set new higscore and save.
        if(coin>currentCoin){
            PlayerPrefs.SetInt(highScoreKey, coin);
            PlayerPrefs.Save();
        }
    }

}

向CoinScore添加积分的脚本

using UnityEngine;
using System.Collections;

public class BeeCoin : MonoBehaviour {

    public int point;
    private float timeVida;
    public float tempoMaximoVida;

    private BeeCoinScore coin;

    AudioSource coinCollectSound;

    void Awake() {


        coin = GameObject.FindGameObjectWithTag ("BeeCoin").GetComponent<BeeCoinScore> () as BeeCoinScore;
    }

    // Use this for initialization
    void Start () {

        coinCollectSound = GameObject.Find("SpawnControllerBeeCoin").GetComponent<AudioSource>();

    }

    void OnCollisionEnter2D(Collision2D colisor)
    {
        if (colisor.gameObject.CompareTag ("Bee")) {


            coinCollectSound.Play ();

            coin.AddBeeCoinScore (point);
            Destroy (gameObject);
        }

        if (colisor.gameObject.tag == "Floor") {
            Destroy (gameObject, 1f);
        }
    }

}

我的UI画布SHOP这是非常基本的,它有4个相关皮肤的图像,价格:100,200,300和400个硬币,每个图像下面有4个按钮,还有一个按钮可以离开。

如果可能的话#。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。

在&#34;购买&#34;按钮已附加脚本out << "poly 1" << endl; Polynomial *newPol1 = new Polynomial(); try { cin >> newPol1; } catch (char* s) { cout << s << endl; }

脚本BuySkin我添加了BeeCoinScore, 删除:TakeBeeScore,进入void OnDisable

现在它工作得很好。

BuySkin Script。

if (coin> current Coin) {}

BeeCoinScore脚本。

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;


    public class BuySkin : MonoBehaviour {

        public int price;

        public void OnClick()
        {
            if (BeeCoinScore.coin >= price) {

                BeeCoinScore.coin -= price;
                Debug.Log ("New skin added");
            }

            if (BeeCoinScore.coin < price) {

                Debug.Log ("Need more coins!");
            }
        }
     }