如何使用unity3d模型制作制作3D版iOS游戏

随笔 - 233Unity3D首页、文档和下载 - 移动游戏软件框架 - 开源中国社区
当前访客身份:游客 [
当前位置:
Unity3D 是一个跨平台的浏览器/移动游戏软件框架,与Flash直接竞争,已经可以在iPhone上应用了
Unity3D引擎采用了和大型、专业的游戏开发引擎相同的架构方式和开发方式实现Web3D,对于Web3D行业来说,是一次大的飞跃,游戏是VR行业 的最高端,呵呵,但是现在都已经平民化了,甚至过春节在山区也能碰见搞VR的老乡,因为会用3Dmax或者VRP也算VR了,呵呵,但是Unity3D的 出现和大量应用将把Web3D拉到Game的快车道上来,让Web3D也&三高&起来,游戏行业的&高投入,高风险,高利润&众人皆知,但是第一高&高投 入&让很多Gamer不能揽Game Dev入怀,现在可以Unity3D了
Unity3D最新更新资讯,共17条&&(,)
9评/6051阅
12评/5884阅
15评/14288阅
授权协议: 未知
开发语言:
操作系统:&iPhone/iPad/iPod&
收录时间: 日
张无影 发表于5天前
张无影 发表于6天前
张无影 发表于7天前
,最后回答(6天前):
Croxx 发表于1个月前
,最后回答(1个月前):
懵懵懂懂的阿文 发表于1个月前
somebag 发表于9个月前
,最后回答(1个月前):
ProudNet 发表于4个月前
,最后回答(3个月前):
心-魔 发表于3个月前
,最后回答(3个月前):
kyle016 发表于4个月前
tt0301 发表于2年前
,最后回答(4个月前):
ProudNet 发表于5个月前
,最后回答(4个月前):
影鎏丶 发表于8个月前
,最后回答(5个月前):
菜鸟哈哈哈 发表于6个月前
菜鸟哈哈哈 发表于6个月前
toohao333 发表于6个月前
sca7 发表于6个月前
,最后回答(6个月前):
影鎏丶 发表于6个月前
,最后回答(6个月前):
描绘人生 发表于7个月前
,最后回答(7个月前):
ProudNet 发表于7个月前
刹那灬 发表于7个月前
使用 Unity3D 中的任何问题
:我只想 呵呵……
共有 228 个类似软件
欢迎来到 CodeTank 的世界,将会带你进入一个神奇而充满挑战的代码机器人的世界! ...
Cyclone2D (飓风软件)是集成的手机游戏设计工具以及开源的引擎,工具提供了强大的动...
SmartTank是一个人工智能游戏平台。他的主要内容是由AI代码控制坦克在可添加的规则...
OpenEmu 是一项开源计划,目的是将游戏模拟带入OS X,使用Cocoa、Core Animation和...
mepper 地图编辑器是一种用于通用目的的地图编辑器。界面风格类似Photoshop。它可以...
LayaAir是HTML5开源引擎 提供Canvas和Webgl同时渲染,如果Webgl不可用,则可自动切...
Freeblo,暗黑破坏神1游戏引擎的现代化重新实现。 Freeablo 是在暗黑破坏神1中使用...
QCEditor是一个简单的cocos2d-x编辑器。 Qt和Cocos-x 都能跨平台,理论上是能够跨平...
CocosBuilder 是一个用来快速开发游戏和应用的免费工具。主要用于开发 Cocos2d-Ja...
Virtjs 可以让你在 JavaScript 程序中嵌入不同的架构上的模拟器,同时提供一些开放...
无论是在你的主要游戏对象(游戏结束,重新启动,继续等)或个人角色和 NPC(AI 行...
共有 149 人关注 Unity3D用Unity3D 做 Flappy Bird 游戏 - 简书
<div class="fixed-btn note-fixed-download" data-toggle="popover" data-placement="left" data-html="true" data-trigger="hover" data-content=''>
写了2123字,被3人关注,获得了7个喜欢
用Unity3D 做 Flappy Bird 游戏
效果展示:
创建项目用到的文件夹 例如 images
scripts等,之后将图片素材拖入到images中, 如下图所示:
接着创建鸟扇动翅膀的动画 如下图所示
小鸟挥动翅膀动画的绘制
这样一个 Bird 扇动翅膀的动画就完成了
接下来呢,该为我们的小Bird 加上上下两个顶部, 如下图所示:
topandbuttom.gif
之后我们为上下两个顶部和小鸟加上碰撞检测并给小鸟加上重力,如下图所示:
加上碰撞检测和小鸟的重力
紧接着, 我们需要为小鸟能在按空格键或者鼠标右键的时候,跳一下,为此,在scripts文件夹下创建birds C# script 文件
小鸟的起跳和碰撞检测
下面解释一下代码:首先,C# 的程序入口在unity3d中被封装成了 Start() 和 Update()两个,其中 Start() 是在游戏启动后仅仅执行一次,一般完成一些初始化工作, 而Update()则是在游戏启动后逐帧执行,也即每帧执行一次Update()函数
第6行 定义了小鸟的飞行速度值, 即可以理解成没按一下按键上升多少距离第14行 检测是否触发了键盘的空格键或者是鼠标的右键,其中GetMouseButtonDown()的参数中0为左键,1为中键,2为右键,若触发则接着执行第15行的动作, 即将小鸟的y轴上的速度加上刚才设置的瞬时速度。第19行 的代码用来检测小鸟和上下顶的关系,若两者碰到则在终端打印 "Game Over" 并重新加载游戏ps: 这里要注意将上下两个顶部的名字重命名成为end
之后将上述写好的代码单击按下不要松开拖到bird上,像如下所示:
下一步先将照相机的投影模式改成正交模式, 因为我们做的是2d游戏,所以选择正交模式更加适合一些
22_07_15_09_48_16.png
然后为Flappy Bird 添加背景图片 首先和小鸟图片一样将导入的背景图片变成unity可以直接用的2d精灵图片, 之后直接拖到我们的场景中就可以了如下图所示:
分别调整背景和小鸟的layer
Paste_Image.png
这样一个基本的雏形已经完成了, 下面就该为小鸟添加障碍物
在这之前, 为了界面简洁可以将背景图片和上下顶部放到一个空的GameObject里然后命名这个GameObject为BG, 即如下图所示
22_07_15_09_47_00.png
接着将我们images文件夹中的pipe1像之前那样编程unity可用的精灵对象,之后拖到我们的场景列表中放置好位置后,复制一个相同的pipe出来 然后旋转180并拖动位置 像下图所示:
22_07_15_09_56_01.png
因为这两根柱子必须要对齐才行所以创建一个新的GameObject将两根柱子拖到其中,并命名该GameObject 为pipes
这样我们的柱子的场景就做好了 接下来 我们要做的是让柱子动起来,首先将其移出视线内,然后在scripts文件夹中创建Move c# script 文件用来控制柱子的移动
Paste_Image.png
第6行: 首先设置柱子移动的距离 这个大家需要自己算一下自己的 我的柱子是从右向左 分别x的坐标为(7)和(-12)所以移动的距离为-19第7行: 设置了柱子移动的速度值第8行: 定义了柱子的终止位置(即一个三维向量)第11行: 在Start() 函数中首先计算出endPos坐标第16行: 在Update() 函数中计算柱子实时坐标第17行: 该行用来检测柱子是否移动到了终止坐标,一旦移动到了,则自我销毁。
到了这一步,我们的鸟儿应该可以穿越柱子啦, 但是这时候小鸟碰到柱子上并不会有什么反应,然么怎么加上小鸟碰到柱子时候结束游戏呢? 最简单的办法就是修改两个柱子的名字和上下底部一样为 end 并给柱子加上碰撞检测组件即如下图所示:
Paste_Image.png
接下来我们创建更多的柱子, 为了实现这一点,要用到一个叫做预制的技术首先在images, scripts等在同一目录创建一个新的文件夹叫做 prebab。
然后如下图所示,将场景中的pipes对象拖到prefab文件夹中做成预制件:
prefab.gif
然后在scripts文件夹中创建一个新的名叫 CreatePipes 的C# script 文件先将其拖拽到Main Camera下作为其组件,并写下以下代码:
Paste_Image.png
第5行: 首先创建一个pipesPrefab的GameObject 待会用来关联之前的pipesP第8行: 重复调用 "Create"函数第12行: 实例化pipesPrefab, 即不断的制造柱子
写完后保存回到Unity3D 将prefab文件夹中的pipes和Main Camera中的 pipesPrefab 关联(即拖拽到标签为pipesPrefab 的值中)之后就可以将场景中原来的pipes 删除掉。
完成后效果如下图所示:
prefab2.gif
这样就能源源不断的出现柱子啦! :)
很显然,我们不能让柱子一直这么以一个不动的姿态出现, 下面来看看如何让柱子的高度发生些变化:完成这一步其实非常简单我们只需要自己测量下柱子在屏幕中y轴的上下临界值即可(例如pipes的y轴设置为3则上面的柱子恰好在屏幕上看不到,y轴设置为-3则下面的柱子恰好看不到)
修改CreatePipes这个脚本文件的第12行为:以下代码:
Paste_Image.png
该代码是重载了Instantiate这个函数其中第二个参数就是设置柱子出现的位置,第三个是柱子的旋转角度,这里我们像如上代码设置为恒定值即可。
如此以下 我们的柱子出现的时候位置就是随机的啦,效果如下所示:
prefab3.gif
接下来,我们为小鸟加上分数, 即每通过一个柱子,获得一点分数, 首先在场景中添加一个 Canvas(在场景列表中右键UI中选择Canvas)并在Canvas中添加Text移动到适宜的位置并修改其内容为0
Paste_Image.png
接下来我们要如何知道小鸟通过了一个柱子呢? 这里用到触发检测, 即一种特殊的碰撞检测(不影响物体的运动,仅仅检测两者有没有碰着)
将pipes从prefab中拖回到场景列表之后在pipes中新建一个Cube并用其填充两个柱子之间的空隙,如下图所示:
接着如下图做一些修改:
pipes2.gif
到这里触发体就设置好了,下面通过一个脚本来测试是否通过柱子并为其加分, 打开 之前创建的 Bird C# script 文件
Paste_Image.png
第2行: 添加UnityEngine.UI库 以便可以获得Text对象第7、8行: 分别定义scoreText对象和整型score变量第30行: 该函数用于处理触发离开事件(即小鸟飞过柱子后分数+1)
保存退出后,将Canvas中的Text和bird1对象的组件中的ScoreText关联起来
之后修改场景列表中pipes下Cube名称为score之后点即右侧的Apply然后删掉该pipes
score2.gif
最后一步, 得分时,如何加上声音呢,首先我们创建新的文件夹叫做audio并且将我们的声音资源文件添加进来, 然后打开 Bird 脚本文件 添加以下第6行和第35行代码之后保存退出:
Paste_Image.png
Paste_Image.png
然后将声音文件 关联到bird下得的coin:
最后点击bird添加AudioSource组件,如下图所示:
audioSource2.gif
到目前为止,我们的FlappyBird小游戏就告一段落了。。。
感谢各位看到这里。。。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
· 202人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:分享使用Unity 3D免费版本编写游戏的教程(三) - 推酷
分享使用Unity 3D免费版本编写游戏的教程(三)
作者:John Boardman
这系列文章的第三部分会比较有趣,因为我们将越过PC/Mac而开始支持iOS和Android!Unity4通过将这些平台的授权版本改为免费版本而实现了这种可能性。我在这一过程中学到了许多重要内容,并希望能够在此与你们进行分享。
请先阅读有关这一Unity 3D教程的
内容。你将会注意到我在此舍弃了我们之前所使用的JavaScript。因为我发现这里的语法都十分相似,所以我们不再需要从一种语言转换到另一种语言。所以我会只是用C#去执行Unity。
在第三部分教程中,我们将讨论到以下内容:
改变第一部分和第二部分的会议版本游戏
支持加速器,触屏,键盘
使用动态字体去创造具有独立分辨率的UI,即能够作用于手持设备和传统计算机
在没有键盘(虚拟或其它)时收集输入内容将发挥功效
面向iOS和Android的Unity创建设置
代码已经在GitHub上更新了,所以你可以免费下载它并基于任何方式进行改变。
游戏已经在以下平台上成功测试过了:
3.iPad(第四代)
5.Kindle Fire(第一代)
6.三星Galaxy S2
7.三星Galaxy S3
8.三星Galaxy Nexus
9.HTC Desire
改变会议版本游戏
游戏的会议版本是为了市场营销目的而创造的。因此,在用户玩游戏前,屏幕会为了获取某些信息而出现。游戏只会保存玩家第一次玩游戏的分数。但是这不通用且不能创造游戏乐趣。所以在这个版本中,我们舍弃了信息屏幕,并设定只要分数是在前10内便能够得以保存。同样的,比起所有市场营销信息,现在它只会询问首字母内容(为了让它显得更有趣我设置了一些标点字符和数字)。
支持加速器,触屏和键盘
在最初宣称手机授权的免费版本时,我希望看到游戏是否可以不做出任何改变而运行于手机上。所以我便创造了构建设置并面向Android进行编译,然后将文件复制到我的三星Galaxy S3上进行尝试。我轻敲了开始游戏按键,然后在数据输入域中输入了我的信息。到目前为止一切都很棒!
在屏幕出现“按压空格以开始”的内容前,所有一切都进展顺利。当然,手机上并没有空格键,也没有其它方法能够获得虚拟按键。那时候我以为“这非常简单”,并想着如何像按压按键那样创造一个轻拍行动。着眼于Input.touches文件以及TouchPhase.Stationary引导着我首次尝试使用碰触作为按压行动。我会使用Stationary是因为觉得“我只想要它能传达用户的手指还未移走。”
我同样也在寻找一些通用方法去明确代码是否能够运行于手机设备上。我想出的最有用的方法便是测试加速器以避免陷入检查操作系统附带条件的琐事中。尽管这可能不适用于所有手机设备,但却在我尝试的所有设备中都能够发挥功效,并能够替代像“supportsTouch”等内容。
bool touched =
bool space =
if (SystemInfo.supportsAccelerometer) {
foreach (Touch touch in Input.touches) {
if (Input.touchCount == 1 && touch.phase == TouchPhase.&&&&&&&&&&& Stationary) {
} else if (Input.GetKeyDown(KeyCode.Space)) {
if (touched || space) {
// fire weapon
这一方法在某种程度上是有用的。结果便是,Stationary代表着“面向每一帧都返回yes,与此同时用户的手指一直停留在屏幕上未移动”,这便意味着我是无意中执行了自动射击。最终我在用户获得1万个分数点时使用该方法进行升级。而为了限制“每次轻敲射击一次”,我使用了TouchPhase.began。
所以碰触是作用于射击。简单地测试Input.touchCount = 2让我能够支持打开防护物,即在“S”之前所做的那里。其它“碰触”添加是关于,如果3个手指按住主屏幕,“重设数据”按键便会出现。这替代了之前打入“key”进行呈现的方法。
现在我们便可以射击并拥有防护物了,但是我们仍然不能移动。所以是时候加入加速器了。我希望不管用户是使用倾斜还是方向键,我的转换位置都能在同个地方结束。我们可以通过首次检查去明确键盘是否正在使用(这将在之后的设置环节深入讨论)。如果未在使用,我们便可以询问Unity当前的X和Y的加速度是什么。这些值是根据屏幕基于水平和垂直轴的倾斜度而发生改变。倾斜度越明显,数值便会越高。
在经过一些实验后,我决定让默认值不提供足够的敏感度。我围绕着“容差系数”进行设置,直到10.0f变得合适。应该将这一数值呈现在Unity上并创造一个设置,从而让用户能够调整敏感度,但是在测试了10个不同设备后,所有的数值几乎都是相同的。此外,这一数值也会让我们觉得与使用键盘箭头相类似。
所以不管怎样,当X和Y的转换值都趋于标准化时,我会推动着玩家移向那个位置,锁定那个位置让用户不能离开屏幕。
// From scriptPlayer.cs lines 38-61
void Update () {
float transH = 0;
float transV = 0;
Vector3 dir = Vector3.
if (!useKeyboard) {
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
transH = dir.x * (playerSpeedH + 10.0f) * Time.deltaT
transV = dir.y * (playerSpeedV + 10.0f) * Time.deltaT
// calculate x and y movement based on time
transH = Input.GetAxis(“Horizontal”) * playerSpeedH * Time.deltaT
transV = Input.GetAxis(“Vertical”) * playerSpeedV * Time.deltaT
transform.Translate(transH, transV, 0);
float z = transform.position.z;
// ensure movement does not go outside playfield
transform.position = new Vector3(Mathf.Clamp(transform.position.x,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& playerPosHMin, playerPosHMax), Mathf.Clamp&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& (transform.position.y, playerPosVMin,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& playerPosVMax) , z);
当设置了这一代码后,玩家便能够移动,射击并使用盾牌了。这些所有的设置都是为了让这款简单的游戏具有游戏性。
Unity是用于开发游戏的强大跨平台工具。多亏了出色的文件设置以及一些谷歌搜索,我们只需要几个小时的时间便能够做出这些改变。当你被困住时,需要记住别人也会被困住,那么你便可以通过搜索去获得答案,因为虚幻的社区正在使用并分享有关Unity的信息。在固定了游戏玩法后,我将关注点转向了UI,因为这里存在着一些问题。
使用动态字体去创造具有独立分辨率的UI,即能够作用于手持设备和传统计算机
使用基本的Unity去设计UI具有一定的挑战性。Asset Store中有一些库存能够提供帮助,但是因为我们所面对的是一款简单的游戏,所以我只想使用基本的Unity,从而避免对于任何费钱的库存的依赖。当我为会议创造UI时,我只需要为一个屏幕分辨率费心。而现在我却面对着一个未知范围的分辨率。
在测试游戏时,我注意到手机设备上的字体非常小,因为在一个小小的显示器上它们具有非常高的屏幕分辨率。所以我按照往常那样基于这些情境进行设置,并开始在谷歌上搜索以明确之前是否有人做过类似的尝试。
在这种情况下,我发现面向Unity 3的分辨率并不能支持手机上的动态字体。我记得曾在Unity 4上看过有关手机的动态字体,但是我却找不到任何有关它的例子。最后,我决定尝试看看这一脚本,即使用Screen.dpi去决定特定屏幕的字体大小。我敢保证,如果Unity可以返回一个数值(除了0)进行呈现(而不是报告它们的DPI),这一方法便会很有效。我需要一些能够作用于任何平台和屏幕的内容,如此便不需要进行删减了。
相反地,我选择基于屏幕分辨率去设置字体大小,因为Screen.width总是会返回游戏当前运行的窗口的宽度,或者屏幕宽度(游戏邦注:如果它是全屏的话)。我只选择根据屏幕宽度去设置字体大小是因为我总是会在场景中运行游戏。以下两张截图便呈现出了我正在解决的问题。
第一张截图是关于没有动态字体的游戏。压缩这张图片并置于手机设备中,你便能够感受到默认字体有多小。并且按键也很小,从而导致我们很难选中正确的对象。
screenshot(from gamasutra)
第二张截图则是关于带有动态字体的屏幕。我可以适当修改框架的垂直规模,但关键在于不管你使用的是何种屏幕分辨率,现在所呈现在的内容都会是一样大。
screenshot(from gamasutra)
对于字体大小的计算是基于屏幕宽度乘以其它容差系数(这将再次呈现在Unity上并为用户控制创造一种设置)。我尝试着在笔记本电脑/台式机和手机上使用相同的代码,但是因为两者间的DPI存在具有的差异,我只能使用不同的元素。如果最终字体大小小于5,我便会想办法将其设置为5.
这些容差系数具有很大的威力,因为只要做出细微的改变就能明显地缩放UI了。你必须记住,因为DPI的差异性,你只有将其用于设备上才能看到明显的不同。
这是在我的scriptFont.cs类上的第一个方法:
public static int GetSizeFromResolution() {
int size = (int)(Screen.width * .016);
if (SystemInfo.supportsAccelerometer) {
size = (int)(Screen.width * .024);
if (size & 5) size = 5;
现在我们知道该使用怎样的字体了,但是我们该如何告诉Unity这一情况呢?根据我的经验,答案便是“看情况而定”,我将告诉你答案。Unity现在支持使用XML这类型的标签去指明有关字体的属性。
让我们着眼于scriptFont中的另一种方法,即MakeString()去明确它是如何发挥作用。这是创造标记字符串去呈现我想要看到的字体的捷径。为了节省屏幕空间,我压缩了该方法,但我也会建议你们在所有条件语句中使用括号而避免遇到一些无意识的副作用,如callMethod2()经常会被乱调用。但是因为格式化原因,我们总是很难察觉到问题,并且基于代码的执行进行追踪总是很让人沮丧。
if (blah == true)
callMethod1();
callMethod2();
do something else…
所以不要这么做好吗。不管怎样,以下方法能够帮助我们轻松地创造一个有关特定大小,特定颜色,粗体或斜体的标记字符串。我还有其它无需考虑各种参数的快捷方法。
public static string MakeString(string text, int size, string color, bool&&&& bold, bool italic) {
string newText = “”;
if (color != null) newText += “”;
if (bold) newText += “”;
if (italic) newText += “”;
newText +=
if (italic) newText += “”;
if (bold) newText += “”;
if (color != null) newText += “”;
newText += “”;
return newT
很棒!现在我们便可以创造有趣的字符串了,但是这时候有两个陷阱在等着我们!首先,在GUIStyle中有一个名为richText的实例变量。如果我们并未将其设置为true,XML便不能运行!其次,GUIStyle只能用于OnGUI内部。如果你尝试着在该方法外部使用它,Unity便会产生抱怨。
对于每种UI控制都存在一种类型。大多数情况下,我们只想要基于字体信息去增加类型。然而对于Label,我们希望创造一个全新的类型并使用它。我尝试了各种方法去使用现有的类型,但是它却都不能接受我的改变。也许这在Unity中是个漏洞,或者是我自己这环出错了。所以让我们将这些内容与其中的一个UI屏幕整合在一起,从中判断它是如何发挥功效。最简单的便是面对实际的游戏屏幕,即左上方呈现出分数和其它信息。以下便是来自scriptSceneManager.cs.:
void OnGUI() {
GUIStyle style = new GUIStyle();
style.richText =
style.normal.textColor = Color.
int size = scriptFont.GetSizeFromResolution();
GUI.Label(new Rect(size, y, size * 10, size), scriptFont.MakeString&&&&&&&&&&&&&&& (“Time&& : ” + gameTime), style);
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size), scriptFont.MakeString&&&&&&&&&&&&&&& (“Score& : ” + score), style);
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size), scriptFont.MakeString&&&&&&&&&&&&&&& (“Lives& : ” + lives), style);
if (!shieldOff) {
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size),
scriptFont.MakeString(“Shield : ” + shieldStrength),&&&&&&&&&&&&&&&&&&&&&& style);
if (score &= bonusScore) {
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size),
scriptFont.MakeString(“AUTOFIRE”, “red”, true, true),&&&&&&&&&&&&&&&&&&&&&& style);
因为背景是黑色的,所以我将文本颜色设置为白色。需要注意的是,所有UI元素的位置和大小都是由合适的字体大小和一些空间补偿所决定。拥有字体大小能够将屏幕变成一个行与列的虚拟集去替代像素。所有的字符串都是利用MakeString方法为控制创造XML类的文本,从而避免呈现出杂乱的标签。我们可以为Label创造一个新类型,但是对于Button和Box我们却必须采取不同的方法并转变内嵌类型。为什么?我也希望能够合理地回答这一问题,但是我必须通过编写代码和测试才能明确答案。这一代码是来自scriptScreenMainMenu.cs.:
void OnGUI () {
int size = scriptFont.GetSizeFromResolution();
GUIStyle boxStyle = GUI.skin.
boxStyle.richText =
boxStyle.normal.textColor = Color.
GUIStyle buttonStyle = GUI.skin.
buttonStyle.richText =
buttonStyle.normal.textColor = Color.
buttonStyle.fontSize =
GUI.skin包含内嵌类型,对此我们可以自由地检索与改变。而Box则是关于打开richText并设置textColor。Buttons比较难处理。尽管我尝试了各种方法,但是除非我能在Button类型中设置fontSize,否则它便不能运行。它似乎也总是会忽视richeText设置。
在没有键盘(虚拟或其它)时收集输入内容将发挥功效
有时候,设备或操作系统所执行的任务并没有任何意义。我便遇到过两种类似的情况,并且我也发现其他开发者也曾遇到过同样的情况。能够给你带来更多利益的一个平台是iOS,另一个便是Android设备,这也是我为何不会偏袒任何一种环境的主要原因—-我希望它们都能够有效地运行!
HTC(Desire)—-Desire拒绝将虚拟键盘递交给Unity。我所尝试的任何方法都不能呈现它。我想其它地方也会有人抱怨关于其它HTC设备的同样问题。
iOS—-与Android设备一起,你可以使用蓝牙键盘,选择键盘设置,并使用键盘而不是触屏和加速器。另一方面,iOS也拒绝向Unity传达键盘事件,所以这便没用了。我不知道这是iOS的错还是Unity的错,但不管原因是怎样的最终结果都是不可改变的—-iOS用户不能使用这一有效的功能。
在这种情况下有什么补救办法吗?iOS用户是完全使用触屏,而HTC所面临的问题则更加眼中,因为在这里甚至连虚拟键盘都不可用。对于我的高分屏幕,我希望让用户能够输入他们的首字母(游戏邦注:这是不同于要求更多信息的会议版本)。因为没有键盘,我认为按键便是更好的选择。因为这是一款怀旧游戏,所以我们必须设置怀旧风格的分数屏幕。其它依赖于键盘的游戏也许就需要探索其它的方法去应对这种局限性。
甚至基于按键选择,我也必须进行多次设计迭代,从而才能创造出可在触屏上轻松使用的UI。举个例子来说吧,不要将当前的首字母置于按键下方,否则用户便看不到它们,因为会被手指挡住!同时,如果创造出足够大的按键,用户便能够非常确定该按压哪个按键。最后,我清楚地隔开了SAVE按键,从而避免玩家会不小心按到。
button(from gamasutra)
当我意识到Android设备能够使用配对蓝牙键盘时,我便陷入窘境中。我不能在Unity中找到任何可检测“现实”键盘存在的内容,所以我不能自动操作它。很多游戏带有Setting区域去配置环境元素,如音效,音乐和敏感度等,所以我认为这是支持键盘的一种有效选择。
关于高分,我选择将这一设置储存在PlayerPrefs中。我选择“keyshot.input”去储存设置,并带有有关数值的简单“keyboard”或“touchscreen”字符串。如果你并未设置数值,那么它便会默认为“keyboard”(如果未检测到加速器的话),或者“touchscreen”。在主菜单添加了一个Settings按键后,你便能够轻松地添加另一个屏幕以及更多按键去支持这一功能。虽然这并不是最美的UI,但是我们总算完成了设置任务。
button(from gamasutra)
面向iOS和Android的Unity创建设置
我包含了有关iOS和Android的创建设置截图,因为在面向这些平台创造时需要基础一些内容。需要注意的是“使用32-bit显示缓冲器”和“使用24-bit色彩深度”是未经核对的。当我选中这些内容时,我遇到了屏幕重绘问题,并且看起来很糟糕。我设置了默认方向为朝向左边,并如下在每个脚本上进行设置:
void Start() {
Screen.orientation = ScreenOrientation.LandscapeL
inspector(from gamasutra)
接下来我选择了Android API的最低级别,如此即使是在我儿子的DroidX上也能够运行了。我也基于同样原因选择了OpenGL ES 1.x。我认为这很有趣,因为我不能禁止互联网连接,尽管我知道自己并不需要这么做。
你需要安装一个ADT(Android开发工具包)。然后你只需要在Unity询问时告诉它ADT在哪里便可。尽管Build Settings为Android提供了“创建并运行”的选择,但是它却不能找到我的设备。相反地,我将自己的Android设备作为大容量储存器,将APK复制到下载文件夹中,然后使用内嵌文件应用(三星)或已安装的ES File Explorer去找到应用并安装它。你将能够从未知的资源中安装应用并允许它安装游戏。
inspector(from gamasutra)
对于iOS,我将iPhone和iPad作为目标设备和原生分辨率,因为这些应用都支持它们。需要注意的是,面向iOS进行编译你需要使用Mac,安装最新版本的XCode,并注册你的应用和设备。
当经历了严格的考验后,你可以从Unity中选择“创建并运行”,Unity将独自处理XCode并将你的应用安装到设备上。这真的超级方便,能够帮助你节省时间。
inspector(from gamasutra)
Unity为我们面向不同平台创造游戏提供了一个很棒的环境。基于所有社区支持以及Unity优秀的文件,我们能够轻松地创造出人们所喜欢的游戏。
在过去,我需要花费数个月的时间从头开始编写游戏引擎,并伴随着许多有关游戏引擎编写的书籍,并且只有新技术才能指出它们是过时的。Unity担心是否能够跟上技术的更新,而我则担心是否能创造出最优秀的游戏。不过我真的很高兴能够与儿子一起创造一款多人游戏。我也有自信在Unity的支持下,我们能够成功地创造出深受玩家喜欢且类型多变的有趣体验。
本文为游戏邦/编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦
Writing Games With Unity 3D, Part 3 – Mobile!
by John Boardman
Part 3 of this blog series is going to be a fun one because we’re going beyond PC/Mac and beginning to support iOS and Android! Unity 4 made this possible by altering the basic version of the license for these platforms to be free (THANK YOU!). I learned a lot through this process and wanted to share it with all of you.
We will be building on Part 1 and Part 2 in this Unity 3D tutorial – so if you haven’t read them yet, head on over there first. One thing you will notice here is that I’ve dropped the JavaScript that we covered before. I found that for what I am doing, the syntax is so similar that it is trivial to convert from one language to another. So, I’m just including my language of choice for Unity which is C#.
In Part 3, we’ll cover the following items:
Changes to the game from the conference version detailed in Parts 1 & 2
Supporting accelerometers, touch screens, and keyboards
Using dynamic fonts to make resolution-independent UIs that work on handhelds and traditional computers
Gathering input when no keyboard (virtual or otherwise) will work
Unity build settings for iOS and Android
Implementing settings
The code has been updated on GitHub, so you are free to download it and alter it in any way you want to make your own game!
The game has been tested successfully on:
3.iPad (4th Gen)
4.iPhone 5
5.Kindle Fire (1st Gen)
6.Samsung Galaxy S2
7.Samsung Galaxy S3
8.Samsung Galaxy Nexus
9.HTC Desire
Changes to the game from the conference version
The conference version of the game was primarily created for marketing purposes. Therefore, before the user played the game, a screen would appear asking for information. The game would also only save the player’s score the first time they played it. Neither of those work very well for general use and game playing fun! So, in this version, the information screen is gone and as long as the score is in the top 10 it will be saved. Also, instead of all the marketing information it now just asks for initials (loosely defined, as I threw in some punctuation characters and numbers just to make it interesting).
Supporting accelerometers, touch screens, and keyboards
When the free versions of the mobile licenses were first announced, I wanted to see how close my game could come to running on mobile with no changes at all. So, I brought up the build settings and compiled for Android, then copied the file to my Samsung Galaxy S3 (SGS3) to try it out. I tapped the Start Game button, then on the entry fields and entered my information. So far so good!
Everything was fine until the screen that stated “Press Space to Start.” But of course, there was no space bar and no way to get to the virtual keyboard. “This should be easy” I thought… and went to figure out how to make a tap act like a key press. Looking at the docs for Input.touches along with TouchPhase.Stationary led me to my first attempt at using touch as a keypress. I used Stationary because I thought “yah I only want it to tell me if the user’s finger hasn’t moved.”
I also hunted around for a generic way to know if the code was running on a mobile device. The most useful thing I could come up with was to test for an accelerometer to avoid getting into checking operating system strings and getting too specific. While this may not account for 100% of the mobile devices out there, it has worked for everything I have tried, and in lieu of something like “supportsTouch” it is close enough for me.
bool touched =
bool space =
if (SystemInfo.supportsAccelerometer) {
foreach (Touch touch in Input.touches) {
if (Input.touchCount == 1 && touch.phase == TouchPhase.&&&&&&&&&&& Stationary) {
} else if (Input.GetKeyDown(KeyCode.Space)) {
if (touched || space) {
// fire weapon
This worked…sort of! It turns out that Stationary means “keep returning yes for every frame while the user’s finger is on the screen and hasn’t moved” which meant I had accidentally implemented autofire without meaning to. I ended up using that as a power up after the user scores 10,000 points. To limit the firing to “once per tap,” use TouchPhase.Began instead.
Okay, so touch is working for fire. Simply testing for Input.touchCount = 2 gave me a way to support turning on the shield where ‘S’ did before. The other “touch” addition was that if 3 fingers are held down on the main screen, the “Reset Data” button will appear. This is instead of typing “key” to show it like it did before (that still works in the keyboard version).
Now then, we can fire and have a shield, but we still can’t move. That’s where the accelerometer comes in. I wanted my transformation position to end up the same whether the user was using tilt or the arrow keys. This was accomplished by first checking to see if the keyboard is in use (discussed later in Settings). If it is not, then ask Unity what the current X and Y acceleration is. These values change based on how tilted the screen is from level on the horizontal and vertical axes. The farther the tilt, the higher the value.
After some experimentation, I determined that the default values didn’t provide enough sensitivity. I played around with the “fudge factor” until 10.0f felt about right (I’ve bolded this value in the code below). This value probably should be exposed to Unity and also made a Setting so the user can adjust the sensitivity, but after testing with 10 different devices all of them felt about the same with that value. Further, this value made it feel similar to using the keyboard arrows.
So anyway, once the X and Y transform values were normalized, I told the transform for the player to move to that position, then clamped the position to the playfield so the user couldn’t move off of the screen.
// From scriptPlayer.cs lines 38-61
void Update () {
float transH = 0;
float transV = 0;
Vector3 dir = Vector3.
if (!useKeyboard) {
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
transH = dir.x * (playerSpeedH + 10.0f) * Time.deltaT
transV = dir.y * (playerSpeedV + 10.0f) * Time.deltaT
// calculate x and y movement based on time
transH = Input.GetAxis(“Horizontal”) * playerSpeedH * Time.deltaT
transV = Input.GetAxis(“Vertical”) * playerSpeedV * Time.deltaT
transform.Translate(transH, transV, 0);
float z = transform.position.z;
// ensure movement does not go outside playfield
transform.position = new Vector3(Mathf.Clamp(transform.position.x,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& playerPosHMin, playerPosHMax), Mathf.Clamp&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& (transform.position.y, playerPosVMin,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& playerPosVMax) , z);
With this code in place, the player can now move, fire, and invoke the shield. With this simple game that’s all there is to change for the game to be playable.
Unity is an awesome cross-platform tool for game development! These changes only took a few hours to get going thanks to great documentation and a few Google searches for examples. When you get stuck, remember that someone else has too, and the answer is likely only a search or two away because of the fantastic community using and sharing information about Unity. With gameplay fixed, I next turned my focus to the UI, because there was definitely something amiss…
Using dynamic fonts to make resolution-independent UIs that work on handhelds and traditional computers
Designing a UI using basic Unity is a bit of a challenge. There are definitely libraries in the Asset Store that can help, but for this simple game I wanted to use only base Unity to remove the reliance on any libraries that would cost money. When I did the UI for the conference, I only had one screen resolution to worry about. Now, I had an unknown range of resolutions.
When testing the game, I noticed that the font was very small on the mobile devices, because they had very large screen resolutions in a very small display. So, I did what I always do in these situations, I started searching Google to see what other people had done before me.
In this case, I found solutions geared for Unity 3, which did not support dynamic fonts in mobile. I remembered reading about dynamic fonts for mobile in Unity 4, but I couldn’t find any examples of it. Finally, I tried this script which uses Screen.dpi to determine the font size for a particular screen. I’m sure it would work great if Unity could return a value other than zero for displays that do not report their DPI. I needed something that would work on any platform and any screen, so that would not cut it.
Instead, I chose to base my font size on the screen resolution, because Screen.width always returns the width of the current window the game is running in, or the screen width if it is fullscreen. I only base the font size on screen width because I always run the game in landscape. Below are two screenshots to display the problem that I am solving.
The first screenshot is how the game looked without dynamic fonts. Squeeze this picture onto a mobile device and you can get a feel for just how small the default fonts rendered on the screen. The buttons were so small it was tricky to choose the right one.
The second screenshot shows what the screen looks like with dynamic fonts. I could fix up the vertical size of the box a little more, but the point is that the display is now consistently large no matter what screen resolution is being used.
The calculation for font size is based on the width of the screen times another fudge factor (which again could be exposed to Unity and made a Setting for the user to control). I tried to use the same code for laptop/desktops and mobile, but due to the large discrepancy in DPI between the two, I ended up using different factors. If the font size ends up being less than 5, I force it to 5.
These two fudge factors have a lot of power, because changing them just a little scales the UI a lot. Be sure to try it out to see what I’m talking about. Keep in mind that only by deploying to devices can you see the dramatic difference due to the DPI differences.
This is the first method in my class scriptFont.cs:
public static int GetSizeFromResolution() {
int size = (int)(Screen.width * .016);
if (SystemInfo.supportsAccelerometer) {
size = (int)(Screen.width * .024);
if (size & 5) size = 5;
Now we know what font size to use, but how do we tell Unity about it? Well…in my experience the answer is “it depends” and I’ll show you why. Unity now supports using XML-like tags to specify properties about a font.
Let’s see how that works by looking at another method in scriptFont called MakeString(). I created this method as a shortcut to building the tagged string for displaying the font I wanted. I’ve shortened the method here to conserve screen space, but I always recommend using brackets around all if statements to keep from unintended side effects such as the example below, where callMethod2() is always called no matter what “blah” is set to. However, due to formatting, it is very difficult to see the problem and it can be very frustrating to track down depending on what the code is doing.
if (blah == true)
callMethod1();
callMethod2();
do something else…
So, don’t do it, okay?
Anyway, the following method is an easy shortcut for making a tagged string a certain size, a certain color, bold, and/or italic. I also have other shortcut methods that leave out various parameters.
public static string MakeString(string text, int size, string color, bool&&&& bold, bool italic) {
string newText = “”;
if (color != null) newText += “”;
if (bold) newText += “”;
if (italic) newText += “”;
newText +=
if (italic) newText += “”;
if (bold) newText += “”;
if (color != null) newText += “”;
newText += “”;
return newT
Cool, now we can make interesting strings, but two more gotchas are waiting! First, in GUIStyle is an instance variable called richText. If it is not set to true, the XML won’t work! Second, GUIStyle can only be used inside of OnGUI. If you try to use it outside of that method Unity will complain.
There is a style for each type of UI control. In most cases, we just want to augment the style with our font information and go with it. However, for Label we want to create a brand new style and use that. I tried a variety of things to use the existing style, but it wouldn’t accept my changes. This might be a bug in Unity or an error on my part, but when I used a brand new style it resolved the issue. So let’s bring this all together with one of the UI screens to see how it works. The easiest is the actual gameplay screen, where the score and other information are shown in the upper left corner. The following is from scriptSceneManager.cs.
void OnGUI() {
GUIStyle style = new GUIStyle();
style.richText =
style.normal.textColor = Color.
int size = scriptFont.GetSizeFromResolution();
GUI.Label(new Rect(size, y, size * 10, size), scriptFont.MakeString&&&&&&&&&&&&&&& (“Time&& : ” + gameTime), style);
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size), scriptFont.MakeString&&&&&&&&&&&&&&& (“Score& : ” + score), style);
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size), scriptFont.MakeString&&&&&&&&&&&&&&& (“Lives& : ” + lives), style);
if (!shieldOff) {
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size),
scriptFont.MakeString(“Shield : ” + shieldStrength),&&&&&&&&&&&&&&&&&&&&&& style);
if (score &= bonusScore) {
y += size + 1;
GUI.Label(new Rect(size, y, size * 10, size),
scriptFont.MakeString(“AUTOFIRE”, “red”, true, true),&&&&&&&&&&&&&&&&&&&&&& style);
I’m setting the text color to white because the background is black. Notice that the position and size of all of the UI elements are determined by using the calculated font size and a few offsets for space. Having the font size turns the screen into a virtual set of rows and columns to use instead of pixels. All strings utilize the MakeString method to build the XML-like text for the controls to use, making it so we don’t have to see all those messy tags. Creating a new style works for Label, but for Button and Box we have to take a different approach and alter the built-in style. Why? Hmm, I wish I could answer that logically, but I figured it out from coding and testing. This code is from scriptScreenMainMenu.cs.
void OnGUI () {
int size = scriptFont.GetSizeFromResolution();
GUIStyle boxStyle = GUI.skin.
boxStyle.richText =
boxStyle.normal.textColor = Color.
GUIStyle buttonStyle = GUI.skin.
buttonStyle.richText =
buttonStyle.normal.textColor = Color.
buttonStyle.fontSize =
GUI.skin contains the built-in styles, which we are free to retrieve and alter however we want. For Box, it is just a matter of turning on richText and setting the textColor. Buttons were more stubborn. Despite everything I tried, unless I set the fontSize in the Button’s style it would not work. It always seemed to ignore the richText setting.
Gathering input when no keyboard (virtual or otherwise) will work
Sometimes, devices or operating systems do things that make no sense at all. I ran into two situations like that and I found other developers that have run into these as well. Just to spread the wealth, one was iOS and the other an Android device, so understand that I’m not favoring one environment over another – I would just like them all to work equally well!
HTC (Desire) – The Desire refuses to lend Unity its virtual keyboard. Nothing I tried would display it. Others have reported the same problem on other HTC devices here and here and I’m sure other places.
iOS – with Android devices, you can pair a bluetooth keyboard (I used a Zagg), choose the keyboard setting (see the next section) and play using the keyboard instead of the touch screen & accelerometer. iOS on the other hand refuses to relay the keyboard events to Unity, so this does not work. I don’t know if it is iOS or Unity’s fault, but the end result is the same – iOS users are locked out of this useful feature.
So, in these cases, what recourse is there? For iOS the user is relegated to just using touch, but the HTC problem is more severe because even the virtual keyboard doesn’t work. For my high score screen I wanted to allow the user to enter their initials (this is a change from the conference version which asked for more information). With no keyboard, I decided that buttons would be a better option. Since this was a retro-style game it worked to have a retro-style high score screen. Other games that rely more on the keyboard may need to explore other options for getting around this limitation.
Even with the choice of using buttons, I still have to make several iterations over the design in order to create a UI that was easy to use with touch. For example, don’t put the current initials below the buttons, or the user won’t be able to see them because their finger will be in the way! Also make the buttons big enough that the user can feel confident about which button they are pressing. Finally, I spaced the SAVE button down far enough that it wouldn’t accidentally be hit.
Implementing Settings
When I realized that there was an option for Android devices to use a paired bluetooth keyboard, I was left with a quandary. I couldn’t find anything in Unity for detecting the presence of a “real” keyboard, so I couldn’t automate it. Many (if not all) games have a Settings area for configuring environmental factors such as sound effects, music, and sensitivity, so I decided this would be a good option for supporting the keyboard.
As with the high scores, PlayerPrefs is the place I chose to store this setting. I chose the key “keyshot.input” to store the setting, with simple “keyboard” or “touchscreen” strings for the value. If nothing has been set for the value, the default is “keyboard” if no accelerometer is detected, or “touchscreen” if one is. After adding a Settings button to the main menu, it was simple to add another screen and a few more buttons to support this functionality. It isn’t the prettiest UI, but it gets the job done
Unity Build Settings for iOS and Android
I’m including screenshots of my build settings for iOS and Android, because there are a few things to keep in mind when building for these platforms. Notice the “Use 32-bit Display Buffer” and “Use 24-bit Depth Buffer” are unchecked. When I had these checked, I had screen redraw issues and it looked terrible. I set the default orientation to landscape left and also set this on each of my scripts like this:
void Start() {
Screen.orientation = ScreenOrientation.LandscapeL
Next, I chose the lowest level of the Android API I could so that devices like my son’s DroidX would work. I also chose OpenGL ES 1.x for the same reason. I thought it was interesting that I could not disable Internet Access even though I knew I didn’t need it.
You will need to install an ADT (Android Development Toolkit) to build with. I used the one here and followed the instructions. Then you just tell Unity where the ADT is when it asks. Even though Build Settings has a “Build and Run” option for Android, it never seemed to be able to find my devices. Instead, I connected my Androids as mass storage devices, copied the APK to the download folder, then used either the built-in file app (Samsung) or installed ES File Explorer to find the app and install it. You will have to enable installing apps from unknown sources to allow installing the game.
For iOS, I chose iPhone + iPad for the Target device and the native resolution since this app supports them all. Note that to compile for iOS you have to use a Mac, have the latest version of XCode installed, be an Apple developer, and have your app and device registered.
Once that ordeal is finished, from Unity you can choose “Build and Run” and Unity handles XCode all by itself and installs your app onto the device. This is super convenient and saves time switching to XCode to finish the builds.
Conclusion
Unity provides a wonderful environment to build games (and many other applications!) for every major platform out there, with more coming all the time. With all of the community support, along with Unity’s excellent documentation, it is possible to achieve the goal of creating games for people to enjoy.
In the past, I spent months writing game engines from scratch (one was a 90 degree turning dungeon crawler on a Commodore Amiga, another was a Doom-style ray caster in Windows 95 with a Pentium 60), along with a bookshelf full of books on writing game engines, only to have new technology render them obsolete. Unity provides a platform in which they worry about keeping up with technology and I worry about creating the best game I can. I am truly excited about moving on to create a multiplayer game with my son that we have designed together. I am confident that with Unity having our back we will be successful in implementing a genre-changing fun experience that many people will enjoy.
Thank you so much for following along with my blogging. If you have questions please contact me. I’m learning along with everyone else but I will help in any way I can!(
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 unity3d游戏制作 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信