Unity3d中的基本购买系统

Basic purchase system in Unity3d

我的游戏中有机会获得COIN,达到一定数量可以发布新皮肤。

当前硬币分数正在正确存储。

我有 UI canvas 那里有皮肤选项,我想知道如果玩家有足够的硬币如何购买这些皮肤,或者如果没有则什么也不会发生够了

遵循以下代码。

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

}

My UI canvas SHOP 非常基本,它有 4 个图像相关的皮肤,价格:100、200、300 和 400 个金币,每个图像下方有 4 个购买按钮,以及一个按钮离开。

C# 如果可能的话。

我解决了我的问题。

在"buy"按钮上附加了脚本BuySkin

我添加的脚本 BeeCoinScore TakeBeeScore, 删除:if (coin> current Coin) {},变为无效OnDisable

现在一切正常。

BuySkin 脚本。

    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!");
            }
        }
     }

BeeCoinScore 脚本。

   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 totalCoinKey = "totalCoin";

       Text CoinScore; // Reference to the Text component.


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

       }
       public void Start(){

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

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

       }

       public void TakeBeeCoinScore (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(){

       PlayerPrefs.SetInt(totalCoinKey, coin);
       PlayerPrefs.Save();

    }
}