首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity3d中的基础采购系统

Unity3d中的基础采购系统
EN

Stack Overflow用户
提问于 2016-05-05 19:43:51
回答 1查看 1.2K关注 0票数 0

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

目前的硬币得分,正被正确地存储。

我有UI帆布那里有皮肤选项,我想知道如何购买这些皮肤,如果玩家有足够的硬币,或什么都不会发生,如果它没有足够。

按照下面的代码操作。

CoinScore

代码语言:javascript
复制
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添加点数的脚本

代码语言:javascript
复制
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帆布店非常基本,它有4个图片相关的皮肤,价格: 100,200,300和400个硬币,4个按钮购买下面的每幅图像,和一个按钮离开。

如果可能的话,C#。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-07 15:32:24

我解决我的问题。

在“购买”按钮上附加了脚本BuySkin

和脚本BeeCoinScore,我添加了TakeBeeScore,删除:if (coin> current Coin) {},在OnDisable中

现在它运行得很好。

BuySkin脚本

代码语言:javascript
复制
    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脚本

代码语言:javascript
复制
   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();

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37059013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档