В настоящее время счет монет сохраняется правильно. >
У меня есть холст пользовательского интерфейса, в котором есть параметры скинов, я хочу знать, как сделать так, чтобы эти скины были куплены, если у игрока достаточно монет, или что ничего не произойдет, если его недостаточно.
Следуйте приведенным ниже кодам.
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();
}
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 = "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 score is greter than highscore, set new higscore and save.
if (coin > currentCoin){
PlayerPrefs.SetInt(highScoreKey, coin);
PlayerPrefs.Save();
}
}
}
Код: Выделить всё
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 () as BeeCoinScore;
}
void Start () {
coinCollectSound = GameObject.Find("SpawnControllerBeeCoin").GetComponent();
}
void OnCollisionEnter2D(Collision2D colisor)
{
if (colisor.gameObject.CompareTag ("Bee")) {
coinCollectSound.Play ();
coin.AddBeeCoinScore (point);
Destroy (gameObject);
}
if (colisor.gameObject.tag == "Floor") {
Destroy (gameObject, 1f);
}
}
}
C#, если возможно.
Подробнее здесь: https://stackoverflow.com/questions/370 ... in-unity3d
Мобильная версия