【Unity】得点表示と終了処理
このブログは、自分が学んだこと、調べたことをアウトプットするために忘備録的に残しているブログとなっております。
間違った情報や私の勘違いなどをお伝えしてしまうこともあるかと思います。
そこのところをご理解いただき、ご覧ください。
また、間違いや勘違いについて、ご指摘ご鞭撻いただければ幸いです。
今回は、得点の表示とゲームオーバーの表示、およびそれらの処理を行います。
得点とゲームオーバー時の表示を設定する。
1.UI(TextMeshPro)の追加
ヒエラルキー上を右クリックし、
・Score
・GameOver
・Select
の3つの"テキスト-TextMeshPro"を作成・追加します。
2.表示位置、フォントサイズ、その他の調整
インスペクターで調整します。
・Score
- アンカー:画面左上
- 位置:X 10 Y -10 (少し画面端から離す)
- 幅:200 高さ:30 (表示内容に合わせるとよい)
- Text Input:Score:0000 (スクリプトで初期化します)
- Font Size:24 (画面の大きさに合わせる感じで)
- Alignment:左詰め 上詰め
- Wrapping:Disabled
- Overflow:Overflow
※Shift + Altを押しながらクリックします。
・GameOver
- アンカー:画面中央
- 位置:X 0 Y 0
- 幅:300 高さ:40 (表示内容に合わせるとよい)
- Text Input:Game Over (スクリプトで初期化します)
- Font Style:B(Bold)
- Font Size:36 (画面の大きさに合わせる感じで)
- Alignment:中央揃え 中央揃え
- Wrapping:Disabled
- Overflow:Overflow
・Select
- アンカー:画面中央
- 位置:X 0 Y -50
- 幅:300 高さ:30 (表示内容に合わせるとよい)
- Text Input:Q->QUIT Space->Replay (スクリプトで初期化します)
- Font Size:24 (画面の大きさに合わせる感じで)
- Alignment:中央揃え 中央揃え
- Wrapping:Disabled
- Overflow:Overflow
2.スクリプト
GameController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro; //TextMeshProを使うのに必要
using UnityEngine.SceneManagement; //Scene01を再読み込みするのに必要//UnityEditorで作成中の場合有効にする
#if UNITY_EDITOR
using UnityEditor;
#endifpublic class GameController : MonoBehaviour
{
//敵機をGameObjectとして宣言する
public GameObject CirclePreFab;
//得点、表示する文言、ゲームの状態を表す変数を定義
private int score;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI gameOverText;
public TextMeshProUGUI selectText;
private bool isGameOver;// Start is called before the first frame update
void Start()
{
//GenerateCircle()を開始する
StartCoroutine("GenerateCircle");
//変数の初期化、表示のクリア
score = 0;
UpdateScoreText();
gameOverText.text = "";
selectText.text = "";
isGameOver = false;
}// Update is called once per frame
void Update()
{
//ゲームオーバー状態になれば、
if (!isGameOver)
{
return;
}
//以下を実行する。
if (Input.GetKey(KeyCode.Space))
{
SceneManager.LoadScene("Scene01"); //Scene01を再読み込み、初めからプレイ
}
//
if (Input.GetKey(KeyCode.Q))
{
#if UNITY_EDITOR
EditorApplication.isPlaying = false; //下記メソッドを作成中に実行させるための条件設定。
#else
Application.Quit(); //アプリケーションの終了。本来作成中は、このメソッドは実行されない。
#endif
}
}//敵機(Circle)を生成する
IEnumerator GenerateCircle()
{
while (true)
{
Instantiate(
CirclePreFab,
//X軸方向はランダム、Y軸方向6.0f、Z軸方向0.0fに敵機を生成
new Vector3(Random.Range(-8.5f, 8.5f), 6.0f, 0.0f),
transform.rotation
);
//1秒ごとに実行する。
//yield return new WaitForSeconds(1.0f);
//変更
yield return new WaitForSeconds(Random.value * 2.0f);
}
}//得点の加算
public void AddScore(int addScore)
{
score += addScore;
UpdateScoreText();
}
//得点の表示更新
public void UpdateScoreText()
{
scoreText.text = "Score:" + score;
}
//ゲームオーバー時の処理
public void GameOver()
{
isGameOver = true;
gameOverText.text = "GameOver";
selectText.text = "Q->QUIT Space->Replay";
}
}
CirclePreFabController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CirclePreFabController : MonoBehaviour
{
//GameObjectの宣言
public GameObject circleexplosion;
public GameController gameController; //GameControllerのゲームオブジェクト// Start is called before the first frame update
void Start()
{
//あらかじめGameControllerのゲームオブジェクトを取得しておく
gameController = GameObject.FindWithTag("GameController").GetComponent<GameController>();
}// Update is called once per frame
void Update()
{
transform.position += new Vector3(
0f,
-0.5f * Time.deltaTime,
0f
);
}private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet")) //Tagが"Bullet"だったら
{
Instantiate(circleexplosion, transform.position, transform.rotation); //追記 爆発エフェクト
Destroy(collision.gameObject); //ぶつかったもの(Bullet)
Destroy(gameObject); //gameObject:自分(Circle)自身
gameController.AddScore(10); //gameController.AddScore()の呼び出し。10点加算する}
if (collision.gameObject.CompareTag("Diamond")) //Tagが"Diamond"だったら
{
gameController.GameOver(); //gameController.GameOver()の呼び出し
Instantiate(circleexplosion, transform.position, transform.rotation);
Destroy(gameObject); //gameObject:自分(Circle)自身
}}
}
はなはだ簡単ではありますが、今回で終了です。