プロジェクト名:Unity02、2Dを選択
背景を白くします。
Textを生成して、Canvas内に入れておきます。
Textを Width 400, Height 50とし「1〜10の数字を推測」文字列をセンタリングしておきます。
動かすと Textに対し、TextControllerというスクリプトを作成します。
コードを開いて、
変数の追加
public Text objText;
int intRandomNumber;
int intGuessedNumber;
関数の追加
private void InitializeGame()
{
// Pick a random number
intRandomNumber = Random.Range(1, 10);
// Set the text to start the game
objText.text = "1〜10の数字を推測";
}
関数呼び出し
// Use this for initialization
void Start () {
InitializeGame ();
}
ここまでで、スクリプトとTextを紐付けし、実行
何もできないので、入力できるようにする。update関数に追加
void Update () {
if (Input.anyKeyDown)
{
// Test to see if the keystroke was a number
if (int.TryParse(Input.inputString, out intGuessedNumber))
{
if (intRandomNumber > intGuessedNumber)
{
objText.text =
string.Format("You guessed {0}. You are too low"
, intGuessedNumber);
}
if (intRandomNumber < intGuessedNumber)
{
objText.text =
string.Format("You guessed {0}. You are too high"
, intGuessedNumber);
}
if (intRandomNumber == intGuessedNumber)
{
objText.text =
string.Format("You guessed {0}. \n You are correct! \n (press spacebar to continue)"
, intGuessedNumber);
}
}
}
}
数値を入れると判定ができる
スペースを押した時に、リスタートする。 void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
InitializeGame();
}
if (Input.anyKeyDown)
{
// Test to see if the keystroke was a number
if (int.TryParse(Input.inputString, out intGuessedNumber))
ソースコード全体
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class TextController : MonoBehaviour { | |
public Text objText; | |
int intRandomNumber; | |
int intGuessedNumber; | |
// Use this for initialization | |
void Start () { | |
InitializeGame (); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
InitializeGame(); | |
} | |
if (Input.anyKeyDown) | |
{ | |
// Test to see if the keystroke was a number | |
if (int.TryParse(Input.inputString, out intGuessedNumber)) | |
{ | |
if (intRandomNumber > intGuessedNumber) | |
{ | |
objText.text = | |
string.Format("You guessed {0}. You are too low" | |
, intGuessedNumber); | |
} | |
if (intRandomNumber < intGuessedNumber) | |
{ | |
objText.text = | |
string.Format("You guessed {0}. You are too high" | |
, intGuessedNumber); | |
} | |
if (intRandomNumber == intGuessedNumber) | |
{ | |
objText.text = | |
string.Format("You guessed {0}. \n You are correct! \n (press spacebar to continue)" | |
, intGuessedNumber); | |
} | |
} | |
} | |
} | |
private void InitializeGame() | |
{ | |
// Pick a random number | |
intRandomNumber = Random.Range(1, 10); | |
// Set the text to start the game | |
objText.text = "1〜10の数字を推測"; | |
} | |
} |
参考
http://hololenshelpwebsite.com/Blog/EntryId/4/Beginner-Tutorial-A-Unity-5-Number-Guessing-Game
0 件のコメント:
コメントを投稿