入门教程:Unity 5猜数字游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/beginner-tutorial-a-unity-number-guessing-game-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3866 个词 阅读量 0入门教程:Unity 5猜数字游戏(译文)
原文地址:https://www.codeproject.com/Articles/1043237/Beginner-Tutorial-A-Unity-Number-Guessing-Game
原文作者:defwebserver
译文由本站 robot-v1.0 翻译
前言
In this tutorial, we will cover creating a simple number guessing game. The user will guess a number between 1 and 10.
在本教程中,我们将介绍创建简单的数字猜测游戏.用户将猜测一个介于1到10之间的数字.
- 下载NumberGuesser-noexe.zip-2.6 MB(Download NumberGuesser-noexe.zip - 2.6 MB)
- 下载NumberGuesser.zip-2.6 MB(Download NumberGuesser.zip - 2.6 MB)
在本教程中,我们将介绍创建简单的数字猜测游戏.用户将猜测一个介于1到10之间的数字.(In this tutorial, we will cover creating a simple number guessing game. The user will guess a number between 1 and 10.) 您可以通过以下链接实时玩游戏:(You can play the game live at the following link:) http://www.adefwebserver.com/unity/numberguesserapp/(http://www.adefwebserver.com/unity/numberguesserapp/)
计算机会告诉用户它们是否太高或太低.(The computer will tell the user if they are too high or too low.)
当用户正确猜出该数字时,计算机将通知他们,并且用户可以单击空格键再次播放.(When the user guesses the number correctly, the computer will inform them, and the user can click the spacebar to play again.)
创造游戏(Create The Game)
第一步是(The first step is to) 下载并安装Unity 5(download and install Unity 5) .(.) 打开(Open)统一(Unity)并创建一个(and create a)新项目(New Project).(.)
呼叫专案(Call the project)数字猜测者(NumberGuesser), 选择(, select)2D(2D)然后点击(and click the)建立专案(Create project)按钮.(button.)
当项目打开时,单击(When the project opens, click on the)主相机(Main Camera).然后在(. then in the)检验员(Inspector),单击背景色.(, click on the background color.) 当…的时候(When the)颜色(Color)弹出窗口打开,将点拖到调色板的左上角以将颜色更改为(popup opens, drag the dot to the upper left-hand corner of the color pallet to change the color to)白色(white).(.)
点击(Click the)关(Close)按钮关闭(button to close the)颜色(Color)弹出.(popup.)
在菜单栏中,选择(From the menu bar, select)游戏对象(GameObject)然后(then)用户界面(UI)然后(then)文本(Text).(.)
一种(A)帆布(Canvas)与(with a)文本(Text)对象和(object and an)事件系统(EventSystem)会出现.(will appear.)
持有(Hold the)Alt键(Alt)和正确的(and right)**点击拖动(click-drag)**缩小(或单击场景并使用鼠标滚轮滚动).(to zoom out (or click in the scene and scroll out using your mouse wheel).) 缩小直到可以看到(Zoom out until you can see the)文本框(Text Box).(.)
点击(Click on the)文本(Text)框(Box)并将其拖动到(and drag it until it is on the)帆布(Canvas).(.)
确保(Ensure that the)矩形(Rect)工具被选中.(tool is selected.)
选择(Select the)文本(Text)框(Box),并在(, and in the)检验员(Inspector):(:)
- 设置(Set the)宽度(Width)至(to)**400(400)**和(and the)高度(Height)至(to)50(50)
- 设置(Set the)文本(Text)至:(to:)猜一个1到10之间的数字(Guess a number between 1 and 10)
- 设置(Set the)对准(Alignment)至(to)居中(centered)
点击(Click the)玩(Play)按钮.(button.) 文本将显示.(The text will display.)
点击(Click the)玩(Play)再按一次按钮可停止程序并返回设计模式.(button again to stop the program and return to design mode.)
为游戏创建代码(Create The Code For The Game)
选择(Select the)文本框(Text Box),以及其属性(在(, and in its properties (in the)检验员(Inspector)), 选择(), select)添加组件.(Add Component.) 向下滚动到列表底部,然后选择旁边的箭头(Scroll down to the bottom of the list and select the arrow next to)新剧本(New Script).(.)
输入(Enter)TextController(TextController)为了(for the)名称(Name),确保(, ensure)夏普(C Sharp)选择语言,然后单击(is selected for the language, and click the)创建和添加(Create and Add)按钮.(button.)
的(The)TextController(TextController)脚本将被创建并显示在(script will be created and display in the)资产(Assets)夹.(folder.) 该脚本还将附加到(The script will also be attached to the)文本框(TextBox).(.) **连按两下(Double-click)**在(on the)TextController(TextController)中的脚本(script in the)资产(Assets)文件夹将其打开.(folder to open it.)
视觉工作室(Visual Studio)将启动…(will start up…)
脚本编辑器将打开.(The script editor will open.) 将所有代码更改为以下内容:(Change all the code to the following:)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextController : MonoBehaviour
{
public Text objText;
int intRandomNumber;
int intGuessedNumber;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update ()
{
}
}
基本上,我们添加了两个变量,供以后使用((Basically we are adding two variables that we will later use ()**intRandomNumber(intRandomNumber)**和(and)intGuessedNumber(intGuessedNumber))以及我们将用于与() and a public property that we will use to communicate with the)文本(Text)游戏屏幕上的元素((element on the game screen ()objText(objText)).().)
救(Save)这页纸…(the page…)
切换回(Switch back to the)统一(Unity)编辑器,然后单击(editor and click on the)文本(Text)中的对象(object in the)层次结构(Hierarchy)选择它.(to select it.) 拖动(Drag the)文本(Text)来自(from the)层次结构(Hierarchy)到旁边的盒子(to the box next to)对象文字(Obj Text)物业(property in the)文字控制器(Text Controller)附加到(that is attached to the)文本框(TextBox).(.) (这将((This sets the)文本框(Text Box)作为(as the)objText(objText)我们创建的脚本中的属性.该脚本将设置(property in the script we created. That script will set the text of the)文本框(Text Box))()) 切换回(Switch back to)视觉工作室(Visual Studio)并添加以下方法:(and add the following method:)
private void InitializeGame()
{
// Pick a random number
intRandomNumber = Random.Range(1, 10);
// Set the text to start the game
objText.text = "Guess a number between 1 and 10";
}
接下来,更新(Next, update the)开始(Start)方法(将在程序启动时运行一次),然后执行以下代码(以调用(method (that will run one time when the program is started) to the following code (to call the)**InitializeGame(InitializeGame)**我们刚刚创建的方法):(method we just created):)
void Start()
{
InitializeGame();
}
如果你有(If you have) 适用于Unity的Visual Studio工具(Visual Studio Tools for Unity) 安装后,您可以设置一个断点(通过单击代码文件左侧的灰色区域),然后选择(installed, you can set a break point (by clicking in the grey area on the left-side of the code file) and then select)附加到Unity(Attach to Unity)…(…)
切换回(Switch back to the)统一(Unity)编辑器,然后单击(editor and click the)玩(Play)按钮…(button…)
…然后您将达到断点.(…And you will hit your break point.)
你可以打(You can hit)F5(F5)继续,您将切换回(to continue, and you will be switched back to the)统一(Unity)游戏.(game.)
现在,只需选择(For now, just select)停止调试(Stop Debugging)来自(from the)除错(Debug)菜单中(menu in)视觉工作室(Visual Studio).(.) 改变(Change the)更新资料(Update)方法为以下代码:(method to the following code:)
// Update is called once per frame
void Update()
{
// Hitting the spacebar always restarts the game
if (Input.GetKeyDown(KeyCode.Space))
{
InitializeGame();
}
}
这将称为(This will call the)**InitializeGame(InitializeGame)**每当(method whenever the)空格键(space bar)被按下.(is pressed.) 该方法将为用户选择一个新的随机数.(That method will pick a new random number for the user to guess.) **加(Add)**下面的代码(the following code to the)更新资料(Update)方法:(method:)
// Detect that a keystroke was pressed
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);
}
}
}
这将检测用户按下的键并将该数字与在(This will detect what key a user presses and compare that number to the random number created in the)**InitializeGame(InitializeGame)**方法.(method.) 如果猜到了正确的数字,将提示用户按空格键以重新开始游戏.(If the correct number is guessed, the user is told to press the space bar to restart the game.)
链接(Links)
Unity 5 Hello World!(Unity 5 Hello World!)
笔记(Notes)
运行示例代码需要Unity 5(或更高版本).(Unity 5 (or higher) is required to run the sample code.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET VS2013 VS2015 Visual-Studio Design Dev 新闻 翻译