やまめも

【 ゲーム開発・日々のことについて 】

【Unity】条件遷移の例(enum、Event、switch)

列挙型(GameController.GameState、GameController.GameLanguage)のパラメータを持った関数とイベントを使って、状態を遷移させる。

#region Init
private void OnEnable () {
	EventsManager.onGameStateEvent += OnGameChangeState;
	EventsManager.onLanguageChangeEvent += onChangeLanguage;

}
private void OnDisable () {
	EventsManager.onGameStateEvent -= OnGameChangeState;
	EventsManager.onLanguageChangeEvent -= onChangeLanguage;

}
#endregion


#region Event
private void OnGameChangeState (GameController.GameState gameState) {
	switch (gameState) {
	case GameController.GameState.Quit :
		quit.SetActive(true);
		break;
	default :
		quit.SetActive(false);
		break;
	}
}
private void onChangeLanguage(GameController.GameLanguage language){
	LanguageUIQuit (language);
}
private void LanguageUIQuit(GameController.GameLanguage language){
	string UIMessege;
	string UIAnswer;

	switch (language) {
	case GameController.GameLanguage.ENGLISH:
		UIMessege = "Are you sure you want to quit the game?";
		UIAnswer  = "Yes";
		break;
	default :
		UIMessege = "Apakah Anda yakin ingin keluar dari permainan ini?";
		UIAnswer  = "Ya";
		break;
	}
}
#endregion