2016-05-05 8 views
0

私のゲームでは、ある程度の金額で新しいスキンをリリースすることができます。Unity3dの基本的な購入システム

現在、コインスコアは正しく保管されています。

スキンオプションがある場所に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つのボタン、および残すボタンが含まれています。

C可能であれば、

+3

ロック解除システムの作成方法がわからない場合、どのようにゲームを書きましたか分かりません。 'if(coins> = skinCost){unlockSkin();}コイン - = skinCost; } ' – BenVlodgi

+1

私は' beeCoin = PlayerPrefs.GetInt(highScoreKey、0);を試します。 if(beeCoin> = price){Debug.Log( "もっとコインが必要!");} if(beeCoin> = price){addSkin(); Debug.Log( "Skin ADDED"); beeCoins = price;} 'しかし、成功しません... –

+1

' if(beeCoin> = price) '...同じifステートメントを' if(beeCoin BenVlodgi

答えて

0

私は私の問題を解決します。今では完璧に働いているのボイドOnDisable

if (coin> current Coin) {}、「購入」ボタンを

はスクリプトBuySkin

と私は削除TakeBeeScore、 を追加したスクリプトBeeCoinScoreに添付しています。

BuySkin Script。

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

    } 
}