unity5.0视频教程摄像机ongui无显示

unity3D游戏开发十二之猖獗的小球_Windows上架设svn服务器方法指导_一个简略的Listener,可以自己动手实现__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
unity3D游戏开发十二之猖獗的小球
Windows上架设svn服务器方法指导
一个简略的Listener,可以自己动手实现
unity3D游戏开发十二之猖獗的小球
unity3D游戏开发十二之疯狂的小球下面我们通过一个具体的实例来了解如何使用物理引擎,该实例通过第三人称视角控制游戏中的小球对象,游戏通过是否与钻石碰撞来界定是否寻找到钻石并获得积分,获得积分满10分后,赢得游戏,当小球冲出跑道时,游戏失败,并提示是否重新开始游戏。
依次打开文件夹Assets-》object,在object文件夹下找到Runway.fbx文件,拖进场景中,如下图:
选择Runway游戏对象,修改Transform组件的Position(88,48.5,57),Rotation(270,350,0),Scale(1,1,1),然后选择Main Camera对象,修改Transform组件的Position(88,60,45),Rotation(55,0,0),Scale(1,1,1),如下图:
然后下面我们添加一个平行光源,依次打开菜单栏中的GameObject-&Create Other-&Directional light,修改该对象的Transform组件的Position(87,57,58),Rotation(48,-29,1),Scale(1,1,1),如下图:
然后我们添加一个球体,依次打开菜单栏GameObject-&Create Other-&Sphere,并命名为Ball,设置它的position(84,50,48),Rotation(0,0,0),Scale(1,1,1),然后打开菜单栏Component-&Physics-&Rigidbody,给Ball对象添加刚体组件,如下图:
下面给小球添加材质文件,如下图:
现在小球无法移动,所以我们得给小球绑定一个脚本,新建一个文件夹,命名为Scripts,在Scripts文件夹里新建一个C#文件,并命名为BallControl.cs,代码如下:
using UnityE
using System.C
public class BallControl : MonoBehaviour {
//小球运动的速率
public float movementSpeed=6.0f;
//小球的水平运动
private Vector3 horizontalM
//小球的前后运动
private Vector3 verticalM
// Use this for initialization
void Start ()
// Update is called once per frame
void Update ()
//Vector3.right x zhou
horizontalMovement=Input.GetAxis("Horizontal")*Vector3.right*movementS
verticalMovement=Input.GetAxis("Vertical")*Vector3.forward*movementS
//小球的运动
Vector3 movement=horizontalMovement+verticalM
//为小球施加力
rigidbody.AddForce(movement,ForceMode.Force);
}然后绑定到Ball对象上,如下图:
点击Play按钮,我们发现可以用键盘控制小球移动了,如下图:
下面我们添加钻石游戏对象,将Gem.fbx文件拖进场景中,复制10个这样的对象,然后依次命名Gem1--Gem10,依次打开菜单栏创建一个空游戏对象,命名Gems,然后把Gem1-10都拖进Gems对象中,如下图:
依次设置Gem1-Gem10对象的Transform组件属性,Gem1的Position(78,50,55),Rotation(-90,0,0),Scale(0.27,0.27,0.27),并再Mesh Collider组件下勾选Is Trigger,设置tag为Pickup,依次设置剩下的gem对象,设好后如下图:
我们修改下BallControl.cs脚本,当小球与钻石碰撞时,让钻石消失,添加代码如下:
void OnTriggerEnter(Collider other)
//判断小球是否与钻石对象碰撞
if (other.tag == "Pickup")
//销毁对象
Destroy(other.gameObject);
接下来,我们创建一个平面,依次打开菜单栏GameObject-&Create Other-&Cune,命名为GameOverTrigger,该对象用于判断小球是否与其碰撞,如果是,表示小球冲出了跑道,游戏失败,我们设置Transform组件属性,Position(90,40,55),Rotation(0,0,0),Scale(165,1,118),如下图:
勾选Box Collider下的Is Trigger复选框,并取消勾选Mesh Renderer组件,创建一个c#脚本,叫GameOverTrigger.cs,代码如下:
using UnityE
using System.C
public class GameOverTrigger : MonoBehaviour {
void OnTriggerEnter()
Debug.Log("over");
把脚本绑定到GameOverTrigger对象上,预览游戏,当小球冲出跑道后,我们打印over字样。接下来我们在新建一个脚本,叫CrayBallManager.cs,用来处理一些输赢得分之类的逻辑,代码如下:
using UnityE
using System.C
public enum CrazyBallState { playing, won, lost };
public class CrazyBallManager : MonoBehaviour
public static CrazyBallManager CB;
public GUIStyle buttonS
public GUIStyle labelS
//钻石总共的数量
private int totalG
//找到钻石的数量
private int foundG
//游戏的状态
private CrazyBallState gameS
void Awake()
foundGems = 0;
gameSate = CrazyBallState.
totalGems = GameObject.FindGameObjectsWithTag ("Pickup").L
//开始游戏
Time.timeScale = 1.0;
void OnGUI()
GUI.skin.label = labelS
GUI.skin.button = buttonS
//显示找到的钻石数量
GUILayout.Label ("Found gems:"+foundGems+"/"+totalGems);
if (gameSate == CrazyBallState.lost)
//提示游戏失败
GUILayout.Label("You Lost");
if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,113,84),"Play again"))
//重新加载场景
Application.LoadLevel(Application.loadedLevel);
else if(gameSate==CrazyBallState.won)
GUILayout.Label("You won");
if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,113,84),"Play again"))
//重新加载场景
Application.LoadLevel(Application.loadedLevel);
public void FoundGem()
foundGems++;
if(foundGems&=totalGems)
WonGame();
public void WonGame()
//暂停游戏
Time.timeScale = 0.0;
gameSate = CrazyBallState.
public void SetGameOver()
//暂停游戏
Time.timeScale = 0.0;
gameSate = CrazyBallState.
CrayBallManager脚本文件中,每调用一次FoundGem()函数,对FoundGems变量累加一次,所以我们在BallControl.cs中修改如下代码:
//判断小球是否与钻石对象碰撞
if (other.tag == "Pickup")
CrazyBallManager.CB.FoundGem();
//销毁对象
Destroy(other.gameObject);
GameOverTrigger.cs修改如下:
void OnTriggerEnter()
CrazyBallManager.CB.SetGameOver ();
将CrayBallManager.cs脚本绑定到Main Camera对象上,然后我们单击Main Camera对象,单击ButtonStyle属性,设置Background属性,放入一张图片,设置FontSize为18,Alignment为Middle Center,设置Fixed Width为113,Height为84,如下图:
运行游戏,如下图:
游戏中的摄像机并没有跟随小球,造成了游戏难以操作的现象,所以我新建一个脚本,叫BallCamera.cs,代码如下:
using UnityE
using System.C
public class BallCamera : MonoBehaviour
//跟随的目标物体
//与目标物体的相对高度
public float relativeHeigth = 10.0f;
//与目标物体的相对距离
public float zDistance = 5.0f;
//阻尼速度
public float dampSpeed = 2;
void Update()
Vector3 newPos = target.position + new Vector3(0, relativeHeigth, -zDistance);
//像弹簧一样跟随目标物体
transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * dampSpeed);
}绑定到Main Camera对象上,target为ball对象,运行游戏,如下图:
Windows上架设svn服务器方法指导
Windows下架设svn服务器方法指导
主要有七个步骤,在这里和大家分享一下,欢迎大家一起来学习Windows下架设svn服务器的方法。下面是具体的介绍。传统的Subversion服务器程序:一、准备工作1、获取Subversion服务器程序到官方网站(http://subversion.tigris.org/)下载最新的服务器安装程序。目前最新的是1.5版本,具体下载地址在:http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=8100&expandFolder=8100&folderID=912、获取TortoiseSVN客户端程序从官方网站http://tortoisesvn.net/downloads获取最新的TortoiseSVN。TortoiseSVN是一个客户端程序,用来与subvers服务器端通讯。Subversion自带一个客户端程序svn.exe,但TortoiseSVN更好操作,提高效率。
二、安装服务器端和客户端安装Subversion(以下简称SVN)的服务器端和客户端。下载下来的服务器端是个zip压缩包,直接解压缩即可,比如我解压到E:\subversion。客户端安装文件是个exe可执行文件,直接运行按提示安装即可,客户端安装完成后提示重启。Windows下架设svn服务器过程中安装服务器端和客户端完成以后需要建立版本库。
三、建立版本库(Repository)运行Subversion服务器需要首先要建立一个版本库(Repository)。版本库可以看作是服务器上集中存放和管理数据的地方。开始建立版本库。首先建立e:\svn空文件夹作为所有版本库的根目录。然后,进入命令行并切换到subversion的bin目录。输入如下命令:svnadmincreateE:\svn\repos1此命令在E:\svn下建立一个版本库repos1。repos1下面会自动生成一些文件夹和文件。我们也可以使用TortoiseSVN图形化的完成这一步:先建立空目录E:\svn\repos1,注意一定是要空的。然后在repos1文件夹上“右键-&TortoiseSVN-&CreateRepositoryhere...”,然后可以选择版本库模式,这里使用默认的FSFS即可,然后就创建了一系列文件夹和文件,同命令行建立的一样。
四、运行独立服务器此时subversion服务还没有开始,只是通过它的命令建立了版本库。继续在刚才的命令窗口输入:svnserve.exe--daemonsvnserve将会在端口3690等待请求,--daemon(两个短横线)选项告诉svnserve以守护进程方式运行,这样在手动终止之前不会退出。注意不要关闭命令行窗口,关闭窗口会把svnserve停止。为了验证svnserve正常工作,使用TortoiseSVN-&Repo-browser来查看版本库。在弹出的URL对话框中输入:svn://localhost/svn/repos1点OK按钮后就可以看见repos1版本库的目录树结构了,只不过这时repos1是个空库。你也可以使用--root选项设置根位置来限制服务器的访问目录,从而增加安全性和节约输入svnserveURL的时间:svnserve.exe--daemon--rootdrive:\path\to\repository以前面的测试作为例,svnserve将会运行为:svnserve.exe--daemon--roote:\svn然后TortoiseSVN中的版本库浏览器URL缩减为:svn://localhost/repos1 你可能会问这样岂不是每次启动SVN服务器都要输入一次命令?怎样才能让SVN服务器在开机的时候自动启动呢?答案就是将SVN服务注册到Windows系统服务,在Windows命令提示符里执行命令sccreateSVNbinpath="\"d:\subversion\bin\svnserve.exe\"--service-rF:\SVN"displayname="SVNService"depend=Tcpipstart=auto,这个命令是不是有点长?其实很容易理解,sc是Windows自带的系统服务注册工具,微软支持中心提供了详细的说明,这里我就不再复述了,参数binpath="\"d:\subversion\bin\svnserve.exe\"--service-rF:\SVN"和我们前面手动启动SVN服务器的命令很像吧?只是把-d改成了--service,这不难理解,因为现在是服务嘛。好了,现在已经大功告成了,重起一下试试吧。PS:如果想从系统服务里删除刚才注册的SVN服务可以使用scdeleteSVN命令。我们再来看一下Windows下架设svn服务器过程中如何配置用户和权限。
五、配置用户和权限用文本编辑器打开E:\svn\repos1\conf目录,修改svnserve.conf:将:#password-db=passwd改为:password-db=passwd即去掉前面的#注释符,注意前面不能有空格。然后修改同目录的passwd文件,增加一个帐号:将:[users]#harry=harryssecret#sally=sallyssecret增加帐号:[users]#harry=harryssecret#sally=sallyssecrettest=test
六、初始化导入Windows下架设svn服务器如何进行初始化导入。下面就是将我们的数据(项目)导入到这个版本库,以后就由版本库管理我们的数据。我们的任何改动都回被版本库记录下来,甚至我们自己丢失、改错数据时版本库也能帮我们找回数据。比如,我在d:\wwwroot下有个guestbook文件夹,里面存放的是我编写的留言簿程序。在此文件夹上“右键-&TortoiseSVN-&Import...”,在弹出对话框的“URLofrepository”输入“svn://localhost/repos1/guestbook”。在“Importmessage”输入“导入整个留言簿”作为注释。点OK后要求输入帐号。我们在用户名和密码处都输入test。完成后guestbook中的内容全部导入到了svn://localhost/svn/repos1/guestbook。我们看到在e:\svn\repos1没有任何变化,连个guestbook文件夹都没有建立,唯一的变化就是e:\svn\repos1容量变大了。实际上我们源guestbook中的内容已经导入repos1版本库了,源guestbook文件夹可以删除了。需要注意的是,这一步操作可以完全在另一台安装了TortoiseSVN的客户机上进行。例如运行svnserve的主机的IP是133.96.121.22,则URL部分输入的内容就是“svn://133.96.121.22”。
七、基本操作流程1、取出(checkout)取出版本库到一个工作拷贝:来到任意空目录下,比如在f分区建立一个空文件夹f:\work。“右键-&SVNCheckout”。在“URLofrepository”中输入“svn://localhost/svn/repos1/guestbook”,这样我们就得到了一份guestbook中内容的工作拷贝。2、存入(checkin)/提交(commit)在工作拷贝中作出修改并提交:在guestbook工作拷贝中随便打开一个文件,作出修改,然后“右键-&SVNCommit...”。这样我们就把修改提交到了版本库,版本库根据情况存储我们提交的数据。在修改过的文件上“右键-&TortoiseSVN-&ShowLog”,可以看到对这个文件所有的提交。在不同的revision条目上“右键-&Comparewithworkingcopy”,我们可以比较工作拷贝的文件和所选revision版本的区别。
一个简略的Listener,可以自己动手实现
一个简单的Listener,可以自己动手实现
Java事件由事件类和监听接口组成,自定义一个事件前,必须提供一个事件的监听接口以及一个事件类。事件监听是观察者模式的一种实现。我们一起来看看这到底是怎么一回事。1、首先定义一个Event类 存放事件中存在的属性和方法
public class MyEvent {
public void setValue(int value){
this.value =
public int getValue(){
return this.
2、然后定义一个Listener接口,用于监听Event事件
public interface Listener {
void valueChanged(MyEvent e);
3、最后再来一个Source类,这是是我们要监听的对象
import java.util.ArrayL
import java.util.L
public class MySource {
private List&Listener& listeners = new ArrayList&Listener&();
// public Methods
public void setValue(int value){
this.value =
notifyAllListener();
public void addListener(Listener listener){
listeners.add(listener);
public void removeListener(Listener listener){
listeners.remove(listener);
// private Methods
private void notifyAllListener() {
MyEvent e = new MyEvent();
e.setValue( value);
for(int i = 0; i & listeners.size(); i++){
Listener l = listeners.get(i);
l.valueChanged(e);
一切准备就绪,现在我们可以来看看有什么效果了。
public class Main {
public static void main(String[] args) {
MySource source = new MySource();
System. out.println("----------------- Before Add Listener ---------------");
System. out.println("changing value..." );
source.setValue(5);
System. out.println("----------------- After Add Listener ----------------");
Listener myListener = new Listener() {
public void valueChanged(MyEvent e) {
System. out.println("MySource value changed to:" + e.getValue());
source.addListener(myListener);
System. out.println("changing value..." );
source.setValue(10);
System. out.println("----------------- After Remove Listener -------------");
source.removeListener(myListener);
System. out.println("changing value..." );
source.setValue(1000);
结果:----------------- Before Add Listener ---------------changing value...----------------- After Add Listener ----------------changing value...MySource value changed to:10----------------- After Remove Listener -------------changing value...
从结果可以看出,我们已经实现了事件监听效果。
不过,这只是一个定制版的监听器,我们只能根据被监听的对象来定制事件中所包含的属性。
如果要实现通用版的Listener,我们又该怎么做呢。那就要使用Java为我们提供的监听接口(继承java.util.EventListener)和事件类(继承java.util.EventObject)。
接下来我们就实现这个需求。
1、相同的,首先我们还是需要一个Event
import java.util.EventO
public class MyEvent extends EventObject {
// 这里我们添加了2个自定义属性
private String changedPropertyN
private Object changedV
public Object getChangedValue() {
return changedV
public void setChangedValue(Object changedValue) {
this .changedValue = changedV
public String getChangedPropertyName() {
return changedPropertyN
public void setChangedPropertyName(String changedPropertyName) {
this .changedPropertyName = changedPropertyN
public MyEvent(Object source) {
super (source);
public Object getSource() {
return super .getSource();
2、然后是Listener
import java.util.EventL
public interface Listener extends EventListener{
public void changed(MyEvent e);
3、最后是我们的监听对象
import java.util.ArrayL
import java.util.L
public class MySource {
private List&Listener& listeners = new ArrayList&Listener&();
public MySource(){
public MySource(String name, int value){
this .name =
this .value =
public int getValue() {
public void setValue( int value) {
this .value =
notifyAllListener( "value" ,value);
public String getName() {
public void setName(String name) {
this .name =
notifyAllListener( "name" ,name);
public void addListener(Listener listener){
listeners .add(listener);
public void removeListener(Listener listener){
listeners .remove(listener);
public void notifyAllListener(String changedPropertyName,Object changedValue){
MyEvent e = new MyEvent( this);
e.setChangedPropertyName(changedPropertyName);
e.setChangedValue(changedValue);
for (int i=0; i & listeners.size(); i++){
Listener listener = listeners .get(i);
listener.changed(e);
最后测试一下
public class Main {
public static void main(String[] args) {
MySource source = new MySource("I dont have a name" ,0);
print(source);
System. out .println("------------------- Before Add Listener ----------------");
System. out .println("changing name..." );
source.setName( "Test Listener" );
print(source);
System. out .println("changing value..." );
source.setValue(5);
print(source);
System. out .println("------------------- After Add Listener -----------------");
Listener myListener = new Listener() {
public void changed(MyEvent e) {
System. out .println("Mysource属性" + e.getChangedPropertyName() + "的值发生了变化,值为:" + e.getChangedValue() +"." );
MySource source = (MySource) e.getSource();
print(source);
source.addListener(myListener);
System. out .println("changing name..." );
source.setName( "My Listener" );
System. out .println("changing value..." );
source.setValue(10);
System. out .println("------------------ After Remove Listener ---------------");
source.removeListener(myListener);
System. out .println("changing name..." );
source.setName( "No Listener" );
print(source);
System. out .println("changing value..." );
source.setValue(100);
print(source);
public static void print(MySource source){
System. out .println("Name:" + source.getName());
System. out .println("Value:" + source.getValue());
System. out .println();
测试结果:Name:I dont have a nameValue:0------------------- Before Add Listener ----------------changing name...Name:Test ListenerValue:0changing value...Name:Test ListenerValue:5------------------- After Add Listener -----------------changing name...Mysource属性name的值发生了变化,值为:My Listener.Name:My ListenerValue:5changing value...Mysource属性value的值发生了变化,值为:10.Name:My ListenerValue:10------------------ After Remove Listener ---------------changing name...Name:No ListenerValue:10changing value...Name:No ListenerValue:100好了,结果还是和我们想象的一样。最后说一句,如果我们想获取到变化之前的值和变化之后的值,那么我们就要加入备忘录模式了,这些就留给你们自己去实现了。Thanks for reading!
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:教程:如何使用Unity制作2.5D游戏(第2部分)
发布时间: 16:16:25
Tags:,,,,
作者:Marin Todorov
这是关于使用Unity开发一款简单的2.5D游戏教程的第二部分内容。
在中我们解析了Unity的使用基础并基于C#语言编写了脚本。我们创造了一款简单的游戏,即你控制着一架能够来回穿梭的飞机并向鲨鱼投射炸弹,以此保护小丑鱼。
在本篇教程的这最后一部分中我们将进一步扩展游戏并完成最后的润色。即我们将添加一些音效和音乐,整合游戏逻辑,并添加更多不同的游戏场景。
如果你未拥有之前的内容,你可以使用我们在教程1中所完成的项目,并在Unity中打开这一内容。你点击Scenes\LevelScene条目,便能够打开之前的场景。
接下来让我们开始更深入地学习Unity并进一步完善游戏!
为游戏添加视觉效果
unity_game(from raywenderlich)
你可能已经发现,当炸弹击中鲨鱼时,它只会安静地消失,而这会让你感到“并不有趣!”
所以我们便决定创造水中爆破场景!
在菜单中选择“GameObject/Create other/Particle System”,屏幕上便会出现一个颗粒系统。在“等级”面板上将名称从“颗粒系统”改为“爆炸”,并将爆炸位置设为(1,1,8)。
现在你如果已经是颗粒系统专家,可以打开“检查器”自己进行设置,或者你也可以遵循我们的方法轻松地进行设置。将以下数值复制在“检查器”中:
unity_particle_emitter(from raywenderlich)
在这里最重要的属性便是“One shot”,当你在这一选项中打勾时,系统将只会发射出一次粒子,也就是说只会出现一次爆炸。接下来让我们设置动画中的数值,即尝试着去匹配下图中的颜色(游戏邦注:如果你没有执行这个步骤也没关系):
unity_particle_animation(from raywenderlich)
在这里,“Autodestruct”是一个很重要的属性,当我们再也看不到活跃的粒子时,被选中的颗粒系统将从场景中消失。而这也是我们想要创造出的效果,就像自动垃圾回收一样。
现在你便创造了一个小型的爆破场景,随后你就需要像之前在创造炸弹时那样,制作一个预制件(并在需要时实例化),让它在完成场景后自动毁灭。
右击“项目”面板的“预制件”文件夹,选择“Create/Prefab”,并将其重命名为“ExplosionPrefab”。从“等级”中将“爆炸”对象拖到新的“ExplosionPrefab”文件夹中。右击“等级”中的“爆炸”并选择“删除”。
右击“项目”面板并选择“Sync MonoDevelop Project”以打开MonoDevelop。加载编辑器BombClass.cs并添加以下代码:
//right under definition of “ySpeed”
public GameObject explosionP
//inside OnTriggerEnter, right after Destroy(this.gameObject)
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
然后再回到Unity,在“项目”面板中选择“BombPrefab”,这时候你将能够在“检查器”中看到新属性“ExplosionPrefab”。从“项目”中将“ExplosionPrefab”拖到新属性区域中你便算完成了设置。
现在点击播放,你便能够看到炸弹撞击鲨鱼时的爆破。
explosions(from raywenderlich)
在游戏中添加音效
光有视觉效果还不够,我们还需要一些音效!
一款游戏怎么能够缺少背景音乐?!这时候我们需要提到另外一个人——Kevin Macleod,他是一名伟大的作曲家,在CC license发布了许多电影音乐和游戏音乐。所以如果你想使用他的作品,就必须明确标示出处。
打开网站:/m/c/royalty-free/index.html?keywords=the%20cannery
将《The Cannery》下载到你的唱片中,并将“The Cannery.mp3”拖到“项目”面板中的“音频”文件夹中。
我们同样也希望能够循环播放音乐,而这时候我们需要将音乐附加到何种对象上?
让我们将其附加到摄像机上!摄像机也是一种游戏对象,也能够带有其它附加组件。
从“项目”面板中将“The Cannery”拖到“等级”面板中的“主摄像机”中。在“等级”中选择“主摄像机”并在“检查器”中找到音源带,在“循环”的选择框中打勾并将音量设置为“0.20”。
这个步骤非常简单,现在你可以边看游戏场景并享受背景音乐了。
使用Unity创造图形用户界面(GUI)
让我们开始研究GUI。Unity能够给你提供一些标准标签和按钮,但是这些却不是该工具的最大优势。不过我们将使用标签去呈现当前的分数变化;而首先我们将执行分数逻辑。
切换到MonoDevelop。打开PlayerClass.cs并添加一个新属性:
public static int score = 0;
又出现了一个新的属性——“静态”。当我们加载了一个类并且不管是否会出现类实体该属性都将保持不变时,我们便创造出静态属性。除此之外,我们还能够从其它类中获得这一属性——这也是我们为何始终将在静态属性中计分的原因。
接下来添加以下函数以观察分数的更新(也许现在做来并没有意义,但是还是照做吧)——观察我们是如何通过类名称获得分数属性:
public void updateScoreBy(int deltaScore) {
PlayerClass.score += deltaS
在PlayerClass中再添加以下函数,从而让分数计算能够呈现在屏幕上:
void OnGUI() {
GUIStyle style = new GUIStyle();
style.fontSize = 20;
GUI.Label(new Rect(10,10,100,20), “Score:”+PlayerClass.score, style );
我们将在GUI图层的每一帧中调用“OnGUI”这一事件处理程序。所以你可以在此画上你在GUI中所需要的一切内容。
GUIStyle便是一种模拟CSS的类,所以你也可以在此使用CSS中的字号,左边距宽等功能,否则你仅仅只能局限于字体大小的设置中。GUI.Label() 是由3个参数构成的函数:标签界限的矩形,绘画线以及文本类型。
这时候我们的任务便是在玩家击中或遗漏时更新分数!打开BombClass.cs并进行如下修改:
//add a new property
public PlayerC
//replace the existing OnTriggerMethod with
void OnTriggerEnter(Collider obj) {
if (obj.gameObject.name == “Shark”) {
//reset shark
obj.gameObject.transform.rotation = Quaternion.
obj.gameObject.transform.position = new Vector3(20f, -3f, 8f);
player.updateScoreBy(+1);
Destroy(gameObject);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
if (obj.gameObject.name == “ClownFish”) {
//reset fish
obj.gameObject.transform.rotation = Quaternion.
obj.gameObject.transform.position = new Vector3(-20f, -1f, 8f);
player.updateScoreBy(-1);
Destroy(gameObject);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
虽然这与我们之前所做的相类似,但是我们拥有一个新的属性,即“玩家”,并且当我们要更新分数时我们将会调用player.updateScoreBy()函数。
同时为了让游戏更加有趣,游戏玩家在击中鲨鱼时能够获得分数,但是如果击中的是小丑鱼就会因此被扣分。如此游戏变得更加复杂了!
最后,我们需要根据炸弹设置玩家属性。不过因为炸弹是动态的,所以我们不能再像以前那样进行设置。但是幸运的是炸弹是由玩家所创造的,所他在创造炸弹的同时也明确了玩家属性。
让我们打开PlayerClass.cs并在“bombObject.transform.position = this.gameObject.transform.position”下面添加以下代码:
BombClass bomb = (BombClass)bombObject.GetComponent(“BombClass”);
bomb.player =
这里再次出现了一个新属性。bombObject是一种GameObject,所以我们在此调用“GetComponent”函数,如此我们便能够访问所有游戏对象中的附加组件(我们将结果归为BombClass),最后我们便获得附加于游戏对象中的C#分类的参考内容。这样我们便设置了“玩家”属性(以PlayerClass函数为例)。
这时候我们便能够在游戏场景中看到分数计算。
ScoreLabel(from raywenderlich)
Unity对象和组件
这时候我们已经使用Unity创造出了游戏对象模式。但是你肯定希望能够更好地理解自己所创造的内容,所以让我们创造一个“短途游程”,观察游戏对象与附加组件之间的关系。
我们看到的所有“条带”内容(游戏邦注:也就是附加于游戏对象上的所有组件)都被添加到“检查器”面板上。即使是一个空白的游戏对象也拥有自己的转换带,即位置,旋转和缩放。而其它内容则是附加于游戏中的组件。
打开BombPrefab,你会发现许多不同的组件:
转换:就像上述描写到的,主要是提供位置,旋转和缩放
网格过滤器:提供可视对象的几何图形
网格渲染器:渲染几何图形
刚体:控制物理属性
音源:播放音频
脚本:更新对象程序
但是这些内容还只是关于少数能够附加于对象上的组件。为了帮助理解,我们可以看看以下图表:
unity_diagram(from raywenderlich)
所以,从脚本组件的角度来看,我们现在更加明确为何需要调用这一代码了:
Destroy(this.gameObject);
即为了破坏与对象相关的所有内容。而基于gameObject属性我们也能够访问其它组件,所以我们便能够调整物理效果或音量等内容。
添加更多场景
现在我们的游戏变得越来越完整了,但是却玩家却还不能面对胜利或失败。
所以让我们在游戏中添加“你赢了”图像,即当玩家分数超过3分时便会看到这个画面。
在菜单中选择“新建场景”,然后“保存场景”,选择文件夹[your project's directory]/Assets/Scenes,并将场景文件重命名为“WinScene”。
选择“等级”中的“主摄像机”,并设置:位置为(0,0,0),投射为“直线”,规格为“10”,近距离为“0.5”而远距离为“22”。在菜单中选择“GameObject/Create Other/Directional Light”并在检查器中设置位置(0,0,0)。
我们希望在这个场景中设置一架飞机(就像游戏关卡中的背景),并在飞机上方播放“你赢了”的影像,所以让我们再次重复教程第一部分中的内容:打开菜单中的“GameObject/Create Other/Plane”,并在检查器中设置:位置(0,0,8),旋转(90,180,0),缩放(3,1,2)。
下载并将Vicki Wenderlich(独立美术家兼插画家)创作的图像保存在你的磁盘中:
gameover_youwin(from raywenderlich)
将“gameover_youwin.png”拖到“项目”面板上的“纹理”文件夹中。在输入纹理后你会发现这时候的画面不是很好看,主要是因为压缩的关系,所以你应该选择“项目”中的“gameover_youwin”纹理,然后在“检查器”中找到“格式”,将其改为“16bits”,并点击“运用”。现在从“项目”面板中将“gameover_youwin”拖到“等级”中的“飞机”。这时候你将能够在“游戏”面板中看到“你赢了”画面——以及Vicki所绘制的一只浮动在下方的邪恶鲨鱼。
我们希望此时的玩家能够再次开始游戏:右击“项目”面板,并在“分类”文件夹中选择“Create/C Sharp Script”,将其重命名为“GameOverClass”。右击并选择“Sync MonoDevelop Project”。在MonoDevelop中打开新的GameOverClass.cs,并用以下代码取代原有的内容:
using UnityE
using System.C
public class GameOverClass : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.anyKeyDown) {
PlayerClass.score = 0;
Application.LoadLevel(“LevelScene”);
此时,当玩家轻拍屏幕时,分数将会进行重置,并会重新载入游戏玩法。只要在Application.LoadLevel() 函数中加载场景名称便可,多简单。
再次回到Unity:从“项目”面板中的“分类”文件夹将“GameOverClass”脚本拖到“主摄像机”中。
现在我们只要选择“File/Build Settings”并在弹出窗口中点击“添加current”按钮,关闭窗口,便能够将这一场景整合到项目中。
同时让我们快速添加“你输了”场景!与之前一样,创建一个“新场景”,然后“保存场景”并在场景文件夹中将其重命名为“LooseScene”。
选择“等级“中的“主摄像机”并设置:位置为(0,0,0),投射为“直线”,规格为“10”,近距离为“0.5”,远距离为“22”。打开菜单中的“GameObject/Create Other/Directional Light”并在检查器中设置:位置为(0,0,0)。再选择菜单中的“GameObject/Create Other/Plane”然后在检查器中设置位置为(0,0,8),旋转为(90,180,0),缩放为(3,1,2)。
下载“你输了”影像并将其保存在磁盘中:
gameover_youlose(from raywenderlich)
为了完成场景设置,我们需要像之前那样进行以下几个步骤:
将“gameover_youlose.png”拖到“纹理”文件夹的“项目”面板中。
在“项目”中选择“gameover_youlose”纹理,然后在“检查器”中找到“格式”并将其改为“16bits”,并点击“运用”。
从“项目”面板中将“gameover_youlose”拖到“等级”中的“飞机”。
从“项目”面板中的“分类”文件夹将“GameOverClass”拖到“主摄像机”中。
在主菜单中选择“File/Build Settings”并在弹出窗口点击“添加current”按钮,关闭窗口。
在这里你拥有3个场景,而你需要将它们连接起来!
通过在“项目”面板中双击“LevelScene”加载LevelScene场景。切换到MonoDevelop并打开PlayerClass.cs。我们将修改updateScoreBy函数以检查玩家的分数是否超过3分或者低于3分。
//replace the updateScoreBy method with this code
public void updateScoreBy(int deltaScore) {
PlayerClass.score += deltaS
if (PlayerClass.score&3) {
Application.LoadLevel(“WinScene”);
} else if (PlayerClass.score&-3) {
Application.LoadLevel(“LooseScene”);
现在我们便将场景流程设置好了。你可以在Unity中点击播放进行观看。当然了,你也可以点击“File/Build&Run”或者在弹出的Xcode中点击“运行”,甚至你也可以在自己的iPhone上尝试这款游戏了。
最后!2.5D的尝试
是时候该上重头戏了,也就是这系列教程中最关键的部分—&#D!
而我们将在此添加一些内容让你能够创造出一款接近于3D的游戏。
迄今为止我们都将摄像机的投射设置为“正交”,但是如此会让场景看起来就像是平面的2D游戏,所以让我们停止这么做!
在你的LevelScene场景中选择“主摄像机”,并在检查器中将投射变为“透视”,而“视野”为“100”(以弥补透视)。现在你便可以点击播放按钮观看你的2.5D游戏了!很酷吧!
unity _2_5d(from raywenderlich)
但是我们的工作还未结束。
我们希望游戏能够变得更加复杂,并演示出摄像机的旋转与移动,每次当玩家分数提高时我们将会基于一个圆弧路径去移动摄像机,并改变其视角。根据这一理念,玩家如果更加深入游戏,便能够以一种更加奇特的视角去看待游戏世界并尝试着炸毁鲨鱼。
切换到MonoDevelop并改变PlayerClass.cs函数:
//add the properties
public GameObject mainC
public GameObject gameB
public float nextZ = 0;
//at the end of the updateScoreBy method
if (PlayerClass.score&0) {
nextZ = PlayerClass.score*2.5f;
//at the end of the Update method
if (nextZ & mainCamera.transform.position.z) {
mainCamera.gameObject.transform.Translate(
3* Mathf.Sin(transform.position.z/2 ) * Time.deltaTime,
-Mathf.Sin(transform.position.z /2 ) * Time.deltaTime *0.3f
mainCamera.gameObject.transform.RotateAroundLocal( Vector3.up, Time.deltaTime*0.1f );
gameBackground.gameObject.transform.RotateAroundLocal( Vector3.up, Time.deltaTime*0.1f );
虽然看起来是密密麻麻的代码,但是却都是我们所需要的内容。让我们一点一点理解这些内容。
首先我们需要明确主摄像机和背景飞机的属性。我们将移动并旋转摄像机,同时我们也将旋转背景。
摄像机将从当前的Z轴0点的位置移向7.5Z的位置。所以当玩家每次获得分数时Z轴的设置将被设定为2.5,5.0然后7.5(如此上升)——从这些数值中我们可以看到,主摄像机的视角将转变成反正弦函数中的弧形。
通过Mathf分类我们便能够获得这些数学函数,如Mathf.Sin()函数。同样地我们也可以通过调用transform.RotateAroundLocal旋转摄像机,或者通过一个轴线或角度进行旋转。我们将同时旋转摄像机和背景,如此摄像机就能够始终面对着背景了(也就是说背景永远不会脱离屏幕范围)。
除此之外,我们还需要连接新的公共属性。让我们再次切换到Unity,在“等级”面板中选择“玩家”对象。从“等级”中将“主摄像机”拖到检查器中的新的“主摄像机”属性;从“等级”中将“背景”对象拖到“检查器”中新的“游戏背景”属性。
恭喜,你终于完成了所有的工作!现在点击File\Build并点击运行,你便能够在自己的iPhone上运行完成后的游戏,尽情享受从奇特的角度炸毁鲨鱼的乐趣吧!
unity_final(from raywenderlich)
使用Unity除错
当你继续自己开发游戏时,你将会遇到一些问题,并且也需要自己去移除这些问题。所以你就必须牢记以下内容:
1.不要忘记在工具栏中有一个暂停键,如果你需要中止游戏的执行并检查所有对象属性,你只需要按压这个按键便能够浏览游戏场景,并在“检查器”面板中检查各个数值。
2.如果你不确定你的函数是否正确,在控制台上(就像Xcode)输入一段信息。调用Debug.Log(“信息”),并将此信息输入控制台中。你可以选择菜单中的“Window/Console”打开控制台窗口。
3.培养这种习惯,即当你在MonoDevelop中编程时记得回到Unity编辑器中检查Unity的状态栏(游戏邦注:在应用窗口的最底部)——因为如果你编写了一个无效的错误代码,你一打开Unity便能够立刻发现红色标注的错误信息。
4.如果你的对象不能移动,你就需要反复检查你的脚本是否附加到对象上!
5.当你在运行游戏时你也可以在检查器中改变各种数值。当你停止游戏时,检查器中的所有数值都会被重新设置为你之前运行的游戏那样。因此如果你忘记停止游戏并做出改变,你便会遗失那些重要数值。所以你必须确保在完成测试后停止游戏,然后才能继续开发工作。
游戏邦注:原文发表于日,所涉事件和数据均以当时为准。
(本文为游戏邦/编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦)
How To Make a 2.5D Game With Unity Tutorial: Part 2
Marin Todorov
11 AUGUST 2011
This is the second part of a two part tutorial series on how to make a simple 2.5D game for the iPhone with the Unity game development tool.
In the first part of the tutorial series, we covered the basics of using Unity and writing scripts with C#. We created a simple game where a plane could fly back and forth bombing sharks while protecting the clown fish!
In this second and final part of the tutorial series, we’re going to extend the game to add some finishing touches. We’ll add some sound effects and music, wrap up the game logic, and add multiple scenes to the game!
If you don’t have it already, grab the project where we left it off in the last tutorial, and open it up in Unity. Make sure to click the Scenes\LevelScene item to make it visible if it doesn’t load on startup.
Allright, so let’s learn some more about Unity and wrap up this game!
Adding iCandy to the game
You’ve probably noticed that when a bomb hits a shark, it just quietly disappears, and thought to yourself, “meh, that’s not very cool!”
Well, be prepared to be blown away – we’re going to add a cool underwater explosion instead!
From the menu select “GameObject/Create other/Particle System”, and you’ll see a particle system appear on the scene. Rename the “Particle System” to “Explosion” in “Hierarchy” panel, and set the Explosion’s position to [1, 1, 8].
Now if you are a Particle System specialist – head to the “Inspector” and pimp it up yourself, and if you are not – just follow my lead it’s pretty easy. Copy over these values in the “Inspector”:
Here the most important property is “One shot” – when you check it the system will emit particles only once – as an explosion does. Now let’s setup also the animation values – just try to more or less match the colors below (not important if you don’t):
Here the one important property is “Autodestruct” – when checked the Particle System will remove itself from the scene when there are no more living particles. This is exactly what we want – it’s like automatic garbage collection.
Now you have a nice small explosion and you need to do exactly the same you did before with the bomb – make a prefab, instantiate it when needed, leave it to autodestroy itself when it’s done on the scene.
Right-click “Project” panel “Prefabs” folder, choose “Create/Prefab”, rename it to “ExplosionPrefab”. Drag the “Explosion” object from “Hierarchy” onto the new “ExplosionPrefab”. Right-click “Explosion” in “Hierarchy” and choose “Delete”.
Right-click “Project” panel and choose “Sync MonoDevelop Project” to open MonoDevelop. Load in the editor BombClass.cs and add this code:
Now switch back to Unity and select “BombPrefab” in “Project” panel – in the “Inspector” you see the new property “ExplosionPrefab”. Just drag “ExplosionPrefab” from “Project” onto this new property field and you’re set to go.
That’s it – press play and see the explosions when you hit sharks. Sweet!
Adding earCandy to the Game
Don’t tell Steve, but iCandy wasn’t enough for us – we need some earCandy too!
How could we have a game be without background music?! We’re going to visit another great guy – Kevin Macleod, who is a great composer releasing film&game music under CC license. So if you use his stuff don’t forget to attribute.
Open this url: /m/c/royalty-free/index.html?keywords=the%20cannery
Once you’re there, download “The Cannery” track somewhere to your disc, and drag “The Cannery.mp3″ onto the folder “Audio” in the “Project” panel.
We would like to have the music loop all the time… but which object should we attach it to?
Well, let’s attach it to the camera! The camera is a game object too, and can have other components attached.
Drag “The Cannery” from “Project” panel onto “Main Camera” in the “Hierarchy” panel. Select “Main Camera” in “Hierarchy” and in the “Inspector” find the Audio Source strip – check the “Loop” checkbox and set Volume to “0.20″.
It’s as easy as that – run the scene and enjoy the new background music!
Creating a GUI with Unity
Let’s dive into yet another new area – GUI. Unity provides you with some standard labels and buttons, but all in all it’s not the biggest strength of Unity. However we’re going to use a label to sh so first we’ll need to implement the score logic.
Switch to MonoDevelop. Open up PlayerClass.cs and add a new property:
Aha! There’s again something new – “static”. This property will be created when the class is loaded and will persist no matter whether there are instances of the class. Additionally this property is accessible from all other classes – this is why we’re keeping the score count in a static property.
Next add this method which will take care of updating the score (not much sense in it now, but just go with it) – see how the score property is accessed via the class name:
Add one more method to PlayerClass – this one will draw the score count on the screen:
This event handler “OnGUI” is getting called on every frame on the GUI layer. So, you draw everything you need on the GUI in here.
GUIStyle is a class, which mimics a CSS class – so you can use fontSize, marginLeft and such if you are familiar with CSS, otherwise just keep with the font size for now. GUI.Label() is a method taking 3 arguments: a rect for the bounds of the label, the string to draw and the text style. That’s all to it.
The only task left is: update the score when we have hits or misses! Open up BombClass.cs and make the following modifications:
It’s pretty similar to what we had before, but we’re having a new property called “player” and when we need to update the score we call player.updateScoreBy().
Also to make the game more interesting if you hit a shark you’ll get a point, if you hit a clownfish (remember? clownfish – gooood, shark – baaahhd) then you will loose a point. Now that’s shaping as a difficult game!
There’s one last thing – to set the player property on the bomb. Now we can’t do it like before because bombs are dynamically created, but fortunately the bombs are created by the player himself, so he can set the player property at creation time.
Let’s do that – open up PlayerClass.cs and just under the line “bombObject.transform.position = this.gameObject.transform.” add this code:
There’s something new again! Let’s discuss: bombObject is a GameObject instance (returned by Instantiate), so we call “GetComponent” on it and this way we can access all attached components to the game object – the result we cast to a BombClass – so in the end we got the reference to the C# class attached to the game object. Next we just set the “player” property to this (the PlayerClass instance).
Run the scene and you’ll see the score counter!
Unity Objects and Components
At this point you already have enough practice with Unity to be introduced to Unity’s game object model. Better understand what you are actually doing, right? Let’s make a really short de-tour to have a look at how game objects relate to the components attached to them.
All those strips we see adding up in the “Inspector” panel – these are components that get attached to the game object. An empty game object has just its transform strip – position, rotation, scale. That’s all – everything else are components attached.
If you look at our BombPrefab – you can see many different components:
Transform: provides position, rotation, and scale, as described above.
Mesh Filter: provides the geometry of the visible object.
Mesh Renderer: renders the geometry.
Rigidbody: handles physics.
Audio Source: plays audio.
Script: can programmatically update the object.
These are just few of the possible components you can attach to an object. To even better understand let’s have a look at this diagram:
So, take the point-of-view of the Script component. It should now be a bit clearer why we had to call:
in order to destroy everything related to the object instance. From the gameObject property we can also access all other components, so we can adjust physics or audio volume, etc .etc.
Adding more scenes
Right now our game is getting good, but there’s no way to win or lose!
So let’s add a “you win” screen when the player makes more than 3 points.
From the menu choose “New Scene”, then again from the menu “Save Scene”, select the folder [your project's directory]/Assets/Scenes and save the scene as “WinScene”.
Select “Main Camera” in “Hierarchy” and set: Position [0, 0, 0], Projection to “Orthographic”, Size to “10″, Near to “0.5″ and Far to “22″. From the menu choose “GameObject/Create Other/Directional Light” and in the Inspector: Position [0, 0, 0].
All we want in the scene is to put a plane (like the background in our game level) and put an image on it saying “you win”, so let do as before in Part 1: From the menu “GameObject/Create Other/Plane” and in the Inspector: Position [0, 0, 8], Rotation to [90, 180, 0], Scale to [3, 1, 2].
Next download and save to your disc the image prepared by Vicki Wenderlich (click for full resolution):
Drag “gameover_youwin.png” onto “Project” panel in “Textures” folder. After the texture is imported it looks kind of dirty – it’s because of the compression, just select “gameover_youwin” texture in “Project” and then in the “Inspector” find “Format” change it to “16bits” and click “Apply”. Now drag “gameover_youwin” from “Project” panel onto “Plane” in “Hierarchy”. In your “Game” panel you should see the “You win” – where Vicki painted the evil shark floating bely up.
We just need to make it alive – when the scene is tapped the game should restart: Right-click “Project” panel in “Class” folder, choose “Create/C Sharp Script” and rename it to “GameOverClass”. Right-click and choose “Sync MonoDevelop Project”. In MonoDevelop open up the new GameOverClass.cs and replace the contents with this code:
When the player taps the screen, the score is reset and gameplay scene is loaded. Application.LoadLevel() just takes the name of the scene to load – pretty easy
Back to Unity: drag “GameOverClass” script from “Project” panel “Class” folder onto the “Main Camera”.
Now to include this scene in the project choose “File/Build Settings” and in the popup window click “Add current” button and close the window. You’ve added the scene to the project build.
Let’s quickly add the “You loose” screen too! Like last time, make a “New scene”, then “Save Scene” as “LooseScene” in the Scenes folder.
Select “Main Camera” in “Hierarchy” and set: Position [0, 0, 0], Projection to “Orthographic”, Size to “10″, Near to “0.5″ and Far to “22″. From the menu “GameObject/Create Other/Directional Light” and in the Inspector: Position [0, 0, 0]. From the menu “GameObject/Create Other/Plane” and in the Inspector: Position [0, 0, 8], Rotation to [90, 180, 0], Scale to [3, 1, 2].
Download this “You Lose” image and save to your disc (click for full resolution):
To wrap up this scene, take the following steps just like last time:
Drag “gameover_youlose.png” onto “Project” panel in “Textures” folder.
Select “gameover_youlose” texture in “Project” and then in the “Inspector” find “Format” change it to “16bits” and click “Apply”.
Drag “gameover_youlose” from “Project” panel onto “Plane” in “Hierarchy”.
Drag “GameOverClass” from “Project” panel “Class” folder onto the “Main Camera”.
From the main menu choose “File/Build Settings” and in the popup window click “Add current” button and close the window.
There – you have 3 scenes, but you need to connect them!
Load the LevelScene scene – by double clicking “LevelScene” in the “Project” panel. Switch to MonoDevelop and open up PlayerClass.cs. We’re going to modify the updateScoreBy method to check whether you made more than 3 points or sank under -3 points.
Now your scene workflow is set. You can give the game a try by hitting Play in Unity. Actually – why don’t you hit “File/Build&Run”, and when Xcode pops up – hit “Run” there as well and give the game a try on your iPhone?
Going 2.5D – at last!
Yes – it’s time. The epic technique you’ve been expecting for 2 long parts of this tutorial series – 2.5D!
And I’ll tell you in advance a secret – we’re going to spice it up and make it almost 3D for your developing pleasure!
Up to now we’ve been setting the cameras’ projection to “Orthographic”, which made the scene look like a plain 2D game – well, that ends here!
In your LevelScene scene select “Main Camera” and in the Inspector change Projection to “Perspective” and “Field of View” to “100″ (to compensate for the perspective). That’s it – hit the Play button and see your game in 2.5D! Ain’t that cool?
But we’re not going to stop here.
Here’s the plan – to make the game more difficult and demonstrate how to rotate and move the camera, every time the score increases we’ll move the camera onto an arc path and change it’s angle. This way, the more you progress in the game the weirder the angle you’ll have to look at the characters and try to bomb the sharks!
Switch to MonoDevelop and make the following changes to PlayerClass.cs:
Alright – lot of code, but that’s about everything we need. Let’s go over it bit by bit.
First, we declare properties to hold references to the Main Camera and the Background plane. We’ll be moving and rotating the camera, and we will be rotating the background as well.
The Camera is moving from it’s current Z position at 0 towards the background to about 7.5Z. So every time the player makes a score, the nextZ is set to 2.5, then 5.0, then 7.5 – and from these values the Main Camera is translated onto something like an arc using a sin function.
All math functions by the way are accessible via the Mathf class – as for the sin is Mathf.Sin(). Also we rotate the camera using transform.RotateAroundLocal and we pass it an axis to rotate around (Vector3.up) and the angle to rotate by. We rotate the camera and the background together, so camera faces always the background (i.e. the background doesn’t run out of the screen).
One more thing – let’s connect the new public properties. Switch to Unity and select “Player” object in “Hierarchy” panel. Drag “Main Camera” from “Hierarchy” onto the new “Main Camera” property in the “Inspector”; drag the “Background” object from “Hierarchy” onto the new “Game Background” property in the “Inspector”.
Congrats, you’re finally done! Hit File\Build and Run and run the finished game on your iPhone, and enjoy bombing sharks at weird angles
Debugging with Unity
I’d like to shortly touch on few topics, because when you go on developing by yourselves you are going to run into trouble where you need to debug your game. So few simple things to keep in mind:
Don’t forget there’s a Pause button in the toolbar, so if you need to stop the execution of the game and check all object’s properties – just hit Pause and then browse around your scene and lookup the values in the “Inspector” panel.
If you’re not sure whether your method fires up, print a message in the console (much like in Xcode). Use: Debug.Log(“message”); to print messages to the Console. You can bring up the Console window by choosing from the menu “Window/Console”.
Develop this habit: When you’re done coding in MonoDevelop and you switch back to Unity editor look at Unity’s status bar (at the bottom of the application window) – if you wrote bad code, which won’t validate – you’ll get an error message in red in there, immediately after you bring Unity up.
If your objects don’t move – triple check if your script is attached to your object!
When you run your game you can change values in the Inspector just to try out different values, etc. etc. When you stop the game: NB all values in the inspector are reset to the ones you had before running the game. Thus effectively if you forgot to stop the game and you made changes, they’ll be lost, so be sure to stop the game after your finished testing and then continue developing.
Where To Go From Here?
Here is the complete sample project that we developed in the above tutorial series.
If you want to learn more about Unity you can look around their web- they have great Support section with examples, forum, etc: /support/. Also have a look at the Unity C# Reference.
If you want to practice more, you can extend Shark Bomber further by adding some of the following features to the app:
Add explosion sounds, game over scene music/effects
Have 2 different explosions and randomly show them
Have different levels
This is a very very simple introduction to Unity – we barely touched on reading iPhone input, but I think you have now a solid understanding how thing in Unity work and that’s more important to get you started!
If you think about it – you already know everything you need to know to also create 3D games! You’ve been translating objects around, rotating them to weird angles, you’ve been moving around the camera and basically nothing stops you from getting wild in the 3 dimensions. Just choose the right models, build some terrain (usually using a plane is a good idea) and you can pretty much do anything you want. So this was also your quick start into making 3D games with Unity!()
CopyRight Since 2010 GamerBoom All rights reserved &&闽ICP备&号-1

我要回帖

更多关于 unity5.0视频教程 的文章

 

随机推荐