【Unity3D基础概念】给初学者看的Unity概览(一):GameObject,Compoent,Time,Input,Physics

点击进入个人新博客html

<p>做者:<a href="http://weibo.com/wangxuanyihaha">王选易</a>,出处:<a title="http://www.cnblogs.com/neverdie/" href="http://www.cnblogs.com/neverdie/">http://www.cnblogs.com/neverdie/</a>&#160; <strong>欢迎转载</strong>,也请保留这段声明。若是你喜欢这篇文章,请点<strong>推荐</strong>。谢谢!</p> <p><a href="http://static.oschina.net/uploads/img/201405/29221015_jmh7.jpg"><img title="QQ20140529123319_thumb1" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="QQ20140529123319_thumb1" src="http://static.oschina.net/uploads/img/201405/29221016_GCZW.jpg" width="260" height="133" /></a></p> <h3><font style="font-weight: bold">Unity3D重要模块的类图</font></h3> <p>最近刚刚完成了一个我我的比较满意的小项目:<a href="http://www.cnblogs.com/neverdie/p/3754931.html">【深刻Cocos2d-x】使用MVC架构搭建游戏Four</a>,在这个游戏中,我使用了本身搭建的MVC架构来制做一个游戏,作到了比较好的SoC(关注点分离)。可是苦于Cocos2d-x没有一个比较完善的编辑器,因此我开始学习另外一个很是流行的游戏引擎-Unity3D。</p> <p>Unity3D是一个Component-Based的游戏引擎,而且为GamePlay Progrmmer提供了不少游戏性层上的支持。好比能够在图形界面上设计动画状态转换的Animator。好比能够直接在场景编辑器中方便进行调整的Collider。好比能够动态调整动画曲线的Animation。总的来讲,Unity是一个架构比Cocos2d-x精巧许多的游戏引擎。</p> <p>可是很遗憾的是,Unity自己并不开源,还好,Unity在不开源的状况下却作了比较详尽的文档支持。同时,Unity的社区也是很友好的,stackoverflow也有许多值得一看的问题。</p> <p>顺便推荐几个学习Unity的网站:</p> <p><a href="http://game.ceeger.com/">Unity圣典</a></p> <p><a href="http://unity3d.com/learn/">Unity的官方文档</a></p> <p><a href="http://www.zhihu.com/topic/19568806">Unity的知乎话题</a></p> <p>&#160;</p> <p>我在学Unity3D的知识的时候,发现Unity内部涉及的重要类比Cocos2d-x要多,有点理不清的感受,因此制做了下面的设计类图来为像我同样的初学者提供一个Unity的初步印象。</p> <p>&#160;</p> <p><a href="http://static.oschina.net/uploads/img/201405/29221016_hc21.png"><img title="Components_thumb2" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Components_thumb2" src="http://static.oschina.net/uploads/img/201405/29221017_zf0i.png" width="632" height="500" /></a></p> <h3><font style="font-weight: bold">GameObject和Component</font></h3> <p>因为Unity是一个Component-Based的游戏引擎,因此游戏中全部的物体都是一个GameObject,为了给这个GameObject附加上各类各样的属性,因此咱们引入了Component这个概念。</p> <p>GameObject是由Component组合成的,Component的生命周期和GameObject息息相关。一旦GameObject的Destroy方法,它的子对象和对应的全部Component都会被销毁,同时,咱们也能够一次只销毁一个单独的Component。</p> <p>Component有以下这些种类,我制做了一张表格来记录它们的用途:</p> <p><a href="http://static.oschina.net/uploads/img/201405/29221025_FV10.png"><img title="QQ20140528004158_thumb3" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="QQ20140528004158_thumb3" src="http://static.oschina.net/uploads/img/201405/29221028_srPl.png" width="745" height="885" /></a></p> <p>组件附属于游戏物体.把一个 <a href="http://game.ceeger.com/Script/Renderer/Renderer.html">Renderer </a>(渲染器)组件附到游戏对象,可使游戏对象显示到场景,附一个 <a href="http://game.ceeger.com/Script/Camera/Camera.html">Camera </a>(摄像机)能够把物体变成一个摄像机物体.全部脚本都是组件,所以都能附到游戏对象上.</p> <p>经常使用的组件能够经过简单的成员变量取得:</p> <p>附在游戏对象上的组件或脚本能够经过GetComponent获取.以下代码示例:</p> <pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">using UnityEngine; using System.Collections;架构

public class example : MonoBehaviour { void Awake() { transform.Translate(0, 1, 0); GetComponent<Transform>().Translate(0, 1, 0); } }</pre>app

<h3><font style="font-weight: bold">Input和InputManager</font></h3>编辑器

<p>关于Input的深刻解读请参考这篇文章:<a href="http://game.ceeger.com/Manual/Input.html">Input 输入</a></p>ide

<p>Unity支持,键盘,操纵杆和游戏手柄输入。</p>函数

<p>在<strong>输入管理器(Input Manager)</strong>能够建立虚拟轴和按钮,并终端用户能够在屏幕配置对话框配置键盘输入。</p>工具

<p>若是想添加新的虚拟轴,选择菜单Edit-&gt;Project Settings-&gt;Input menu。这里能够改变每一个轴的设置。便可进入Input Manager的配置界面。</p>学习

<p><a href="http://static.oschina.net/uploads/img/201405/29221029_iS01.jpg"><img title="Android-Input-2_thumb" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="Android-Input-2_thumb" src="http://static.oschina.net/uploads/img/201405/29221030_Dsnn.jpg" width="262" height="552" /></a></p>动画

<p>从脚本,全部虚拟轴经过它们的<strong>名字(name)</strong>来访问。 </p>网站

<p>当建立时,每一个项目都具备下面的<strong>默认输入轴</strong>: </p>

<ul> <li><strong>Horizontal</strong> and <strong>Vertical</strong> are mapped to w, a, s, d and the arrow keys.

<br />水平和垂直被映射到w, a, s, d键和方向键 </li>

<li><strong>Fire1</strong>, <strong>Fire2</strong>, <strong>Fire3</strong> are mapped to Control, Option (Alt), and Command, respectively.

<br />Fire1, Fire2, Fire3被分别映射到Ctrl,Option(Alt)和Command键 </li>

<li><strong>Mouse X</strong> and <strong>Mouse Y</strong> are mapped to the delta of mouse movement.

<br />Mouse X 和 Mouse Y被映射到鼠标移动增量 </li>

<li><strong>Window Shake X</strong> and <strong>Window Shake Y</strong> is mapped to the movement of the window.

<br />Window Shake X 和 Window Shake Y 被映射到窗口的移动 </li>

</ul>

<p>&#160;</p>

<h3><font style="font-weight: bold">Time</font></h3>

<p>Time类是Unity中的一个全局变量,它记载了和游戏相关的时间,帧数等数据。</p>

<p><a href="http://game.ceeger.com/Script/Time/Time.html">Time </a>类包含一个很是重要的变量叫deltaTime.这个变量包含从上次调用Update 或FixedUpdate到如今的时间(根据你是放在Update函数仍是FixedUpdate函数中).(另注: Update每帧调用一次)</p>

<p>依照上面的例子,使得物体在一个匀速的速度下旋转,不依赖帧的速率,以下:</p>

<pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Update() { transform.Rotate(0, 5 * Time.deltaTime, 0); } }</pre>

固然了,在使用Time这个类的时候,咱们也要记住使用各类各样的Lerp函数来减小本身的工做量,在Unity3D中,Vector3,Vector2,Color等类都提供了相应的Lerp函数给咱们调用。

<h3><font style="font-weight: bold">Physics和Transform</font></h3>

<p>Physics类是Unity重的一个工具函数类,它主要提供了Linecast和Raycast两种射线投射方式。</p>

<p>其中Linecast是以投射的起始位置和<strong>终止位置</strong>为参数,来判断这个投射有没有和某个Collider发生了碰撞。</p>

<p>而Raycast则是以投射的起始位置和<strong>投射方向</strong>为参数,来判断这个投射有没有和某个Collider发生了碰撞。</p>

<p>相应的实例能够看下面的这一段程序:</p>

<p>&#160;</p>

<pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">using UnityEngine; using System.Collections; public class Example : MonoBehaviour { void Update() { // 使用Raycast Vector3 fwd = transform.TransformDirection(Vector3.forward); if (Physics.Raycast(transform.position, fwd, 10)) print(&quot;There is something in front of the object!&quot;); // 使用Linecast Transform target; if (!Physics.Linecast(transform.position, target.position)) ProcessData.AndDoSomeCalculations(); } }</pre>

<p>&#160;</p>

<p>在Physics这个模块中,有三个Component是相当重要的,分别是RigidBody,Collision,Joint。在新的版本中,又引入了RigidBody2D,Collision2D,Joint2D这些Component来处理2D中的Physics事件。</p>

<p>这三个类都是处理物理相关的事件的,那么它们有什么区别呢?</p>

<p>RgidBody是做为一个受力物体而存在的,因此能够向一个RigidBody施加Force(力),Drag(阻力)。同时RigidBody还有 velocity (速度),mass(质量),position(位置),旋转(rotation)等等。</p>

<p>Collider是为了处理物理中的碰撞事件而出现的类,就像上面表格中所说的,若是没有Collider,两个RigidBody之间是没法发生碰撞的。对同一个GameObject能够绑定多个Collider构建更加复杂的碰撞体结构。Collider另一个很值得注意的就是咱们能够为Collider设置material,即Collider的物理材质。 物理材质用于调整摩擦力和碰撞单位之间的反弹效果。</p>

<p>当发生碰撞时,会触发毁掉函数OnCollisionEnter,OnCollisionStay,OnCollisionExit等等。这几个函数与OnTriggerXXX的区别会在接下来的博客中提到。</p>

<p>Joint用于链接两个RigidBody,当Joint断掉的时候会触发OnJointBreak的回调函数。</p>

<h3></h3>

<h3><font style="font-weight: bold">总结</font></h3>

<p>原本想把类图重的各类类的用法都说一说,但限于篇幅有限,今天就只写了这几个类,那么请期待个人下一篇博客。</p>

相关文章
相关标签/搜索