それではステージクリア処理やゲームオーバー処理の作成に入ります(ついでに、これ以外の細かい処理も一気に作ってしまいます)。
ThirdPersonUserControlの変更
まず、ステージをクリアしたりゲームオーバーになった時にユニティちゃんを操作できてしまうと変なので、そのときはプレイヤーの入力を受け付けないようにする必要があります。そこで、ユーザーの入力を受け付ける役割を持つ「ThirdPersonUserControl」を下のように変更します。
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof (ThirdPersonCharacter))]
public class ThirdPersonUserControl : MonoBehaviour
{
//中略
public bool allowInput = false;
//中略
private void Update()
{
if (!allowInput)
{
return;
}
//中略
}
// Fixed update is called in sync with physics
private void FixedUpdate()
{
if (!allowInput)
{
m_Move = Vector3.zero;
m_Character.Move(m_Move, false, m_Jump);
return;
}
//以下略
}
}
}
GameManagerの書き換え
次にGameManagerのC#スクリプトを下のように書き換えてください。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.ThirdPerson;
[RequireComponent(typeof(MoveSceneManager))]
[RequireComponent(typeof(SaveManager))]
[RequireComponent(typeof(SoundManager))]
public class GameManager : SingletonMonoBehaviour<GameManager>
{
public bool isDebugMode = false;
[SerializeField]
GameObject clearCanvasPrefab;
[SerializeField]
GameObject gameOverCanvasPrefab;
[System.NonSerialized]
public bool countDown = false;
int score = 0;
int numOfCoins = 0;
MoveSceneManager moveSceneManager;
SaveManager saveManager;
SoundManager soundManager;
ThirdPersonUserControl character;
Text coinText;
Canvas canvas;
Button retryButton;
Button nextButton;
Button titleButton;
public int Score
{
set
{
score = value;
SetCoinText();
if(score >= numOfCoins)
{
StageClear();
}
}
get
{
return score;
}
}
public int NumOfCoins
{
set
{
numOfCoins = value;
SetCoinText();
}
get
{
return numOfCoins;
}
}
public void Awake()
{
if (this != Instance)
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
moveSceneManager = GetComponent<MoveSceneManager>();
saveManager = GetComponent<SaveManager>();
soundManager = GetComponent<SoundManager>();
}
void Start()
{
if (Debug.isDebugBuild)
{
if(moveSceneManager.StageName != "Title")
{
LoadComponents();
}
}
}
public void LoadComponents()
{
character = GameObject.FindGameObjectWithTag("Player").GetComponent<ThirdPersonUserControl>();
score = 0;
}
public void StageClear()
{
countDown = false;
character.allowInput = false;
canvas = Instantiate(clearCanvasPrefab, transform.position, Quaternion.identity).GetComponent<Canvas>();
nextButton = GameObject.Find("NextStageButton").GetComponent<Button>();
titleButton = GameObject.Find("TitleButton").GetComponent<Button>();
if (moveSceneManager.CurrentStageNum >= moveSceneManager.NumOfStage - 1)
{
nextButton.interactable = false;
}
else
{
nextButton.onClick.AddListener(() => moveSceneManager.MoveToStage(moveSceneManager.CurrentStageNum + 1));
}
titleButton.onClick.AddListener(() => moveSceneManager.MoveToStage(0));
}
public void GameOver()
{
countDown = false;
character.allowInput = false;
canvas = Instantiate(gameOverCanvasPrefab, transform.position, Quaternion.identity).GetComponent<Canvas>();
retryButton = GameObject.Find("RetryButton").GetComponent<Button>();
titleButton = GameObject.Find("TitleButton").GetComponent<Button>();
retryButton.onClick.AddListener(() => moveSceneManager.MoveToStage(moveSceneManager.CurrentStageNum));
titleButton.onClick.AddListener(() => moveSceneManager.MoveToStage(0));
}
void SetCoinText()
{
coinText = GameObject.Find("Coin_Num").GetComponent<Text>();
coinText.text = Score.ToString() + "/" + numOfCoins.ToString();
}
}
処理の説明
Start()メソッド
主にデバッグ用の処理です。まず、今のシーンがタイトル画面以外ならコイン取得数のテキストのコンポーネントを取得します。それからテストプレイ中なら、必要なコンポーネントを読み込む処理を行っています。
StageClear()メソッド
クリア表示用カンバスを表示して、ボタンにメソッドを登録する処理を行っています。もし次のステージがない(=最終ステージ)であれば「次のステージ」ボタンを無効化するようにします。
なお、AddListenerに引数付きのメソッドを渡すときはラムダ式を使う必要があります。ちょっとした便利テクニックなので覚えておいてください。
GameOver()メソッド
StageClearメソッドと同じ要領です。
GetNumOfCoins()メソッド
ステージ上のコインの枚数を取得します。
SetCoinText()メソッド
取得したコインの枚数を表示するための文字列を生成します。
次のページ→カウントダウン処理の作り方
