點擊進入個人新博客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>  <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> </p> <p>我在學Unity3D的知識的時候,發現Unity內部涉及的重要類比Cocos2d-x要多,有點理不清的感受,因此製做了下面的設計類圖來爲像我同樣的初學者提供一個Unity的初步印象。</p> <p> </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->Project Settings->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> </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> </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("There is something in front of the object!"); // 使用Linecast Transform target; if (!Physics.Linecast(transform.position, target.position)) ProcessData.AndDoSomeCalculations(); } }</pre>
<p> </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>