2016-05-05 2 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(); монеты - = скины; } ' – BenVlodgi

+1

Я пытаюсь' beeCoin = PlayerPrefs.GetInt (highScoreKey, 0); if (beeCoin> = цена) {Debug.Log («Нужно больше монет!»);} if (beeCoin> = цена) {addSkin(); Debug.Log («Skin ADDED»); beeCoins - = price;} 'но не sucess ... –

+1

у вас есть тот же оператор if дважды в строке' if (beeCoin> = price) '... первый должен быть' if (beeCoin BenVlodgi

ответ

0

Я решаю свою проблему.

на кнопку «купить» присоединили скрипт BuySkin

и сценарий BeeCoinScore я добавил TakeBeeScore, удален: if (coin> current Coin) {}, в пустоту OnDisable

Теперь она работает отлично.

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

    } 
}