ハイスコア

ハイスコアの作成は、Unity公式チュートリアルを参考にした。japan.unity3d.com

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

// プレイ画面でのスコアの表示
public class Score : MonoBehaviour {

	public GameObject ScoreText;

	public int CurrentScore;
	public int HighScore;

	public static Score instance = null;
	void Awake(){
		if(instance == null){
			instance = this;
		}
		// ハイスコアの初期設定
		if (PlayerPrefs.GetInt("HighScore") == 0){
			PlayerPrefs.SetInt("HighScore",0);
		}
		else {
			HighScore = PlayerPrefs.GetInt("HighScore");
		}
	}

	void Start () {
	}

	void Update () {
		// PlayerPrefbsからとってきたhighscoreと、今のスコアを比較する。
		if(HighScore < CurrentScore){
			HighScore = CurrentScore;
		}

		ScoreText.GetComponent<Text>().text = "" + CurrentScore;

	}
	public void ScoreSave(){
		// ScoreSaveのときに初めて、PlayerPrefsにセットする。
		PlayerPrefs.SetInt ("HighScore", HighScore);
		PlayerPrefs.Save();

	}
}