ROLL-A-BALL TUTORIAL #7
Add end of the game
We add conditions for the end of the game.
So we should add the function.
It is the function that displays “YOU WIN” on the screen when the game player collects all items in the game.
The necessary elements to realize the function is as follows.
1. A word to mean the end of the game.
2. A judgment that finish collecting all item.
1. Make a UI of end of the game
1-1. Make label.
At first, we make a UI at end of the game.
1-1. Make label.
At first, we make a UI at end of the game.
1-2. Set the value of the label.
Next, we set the value of the “WinnerLabel” object.
1. Select “WinnerLabel” object in “Hierarchy” view.
2. Set the value of “RectTransform” in the “Inspector” view. It is as follows.
(PosX:0, PosY:0, Width:300, Height:100)
3. Change “Text” into “YOU WIN” in the Text component.
4. Set “FontSize” into “60” in the Text component.
2. Set conditions for the end of the game
We set conditions in Game Controller.
The game player finishes collecting all items. It is the conditions of victory of the game.
Therefore, the game player wins if the number of items becomes Zero.
GameController.cs
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public UnityEngine.UI.Text scoreLabel;
public void Update ()
{
int count = GameObject.FindGameObjectsWithTag ("Item").Length;
scoreLabel.text = count.ToString ();
if (count == 0) {
// program when game player wins.
}
}
}
3. Display Label when the game finished
We display “YOU WIN” on the screen when the player completes the game.
3-1. Set referring from “GameController” to “WinnerLabel”.
GameController.cs
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public UnityEngine.UI.Text scoreLabel;
public GameObject winnerLabelObject;
public void Update ()
{
int count = GameObject.FindGameObjectsWithTag ("Item").Length;
scoreLabel.text = count.ToString ();
if (count == 0) {
// program when game player wins.
}
}
}
After change source code, return to Unity Editor.
We set referring from “GameController” to “WinnerLabel”.
3-2. Hide object of “YOU WIN”.
We can hide the object in several ways.
This time, we choose the way to deactivate Object.
3-3. Display Label when the game finished.
We change the status of “WinnerLabel” from inactivity to activity.
Change source code. It is as follows.
GameController.cs
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public UnityEngine.UI.Text scoreLabel;
public GameObject winnerLabelObject;
public void Update ()
{
int count = GameObject.FindGameObjectsWithTag ("Item").Length;
scoreLabel.text = count.ToString ();
if (count == 0) {
// Change status of object to active.
winnerLabelObject.SetActive (true);
}
}
}
4. Play the game
Let’s try to play the game.
Game player collect an item, and finally "YOU WIN"appears on the screen.
Reference
이 문제에 관하여(ROLL-A-BALL TUTORIAL #7), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/amex/items/7153d897f9549a1a6a88
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(ROLL-A-BALL TUTORIAL #7), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/amex/items/7153d897f9549a1a6a88텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)