我的第一個 Unity3D Windows Store App

Unity 自4.2版起即開始正式支援 Windows Store App 及 Windows Phone 8,而且這兩個 add-ons 不但在 Unity Pro 上是免費的,同時在free version 的 Unity 上也是內建的免費功能!
image
========================= 分隔線 =========================
以下我即以 free version 的 Unity,一步一步建制一個簡單 Windows Store App 讓大家參考。
首先在 Unity 中新增專案,不要匯入任何的packages。
image
先在 Hierarchy 中建立一個 Cube,
image
然後在 Inspector 中改變其 Rotation 及 Scale 如以下:
image
現在這個 Cube 看起來是黑黑的一團,所以我們加點光給它: 到 Hierarchy 中建立一個 Directional Light:
image
接下來我們寫一行 C# code 讓這個 Cube 會轉。先到 Hierarchy 中選擇 Cube 物件後,在其 Inspector 畫面中往下拉,選 Add Component –> New Script –> CSharp 後,按下 Create and Add:
image
這時在 Project 中的 Assets 中會出現剛建立好的 C# script:
雙點這個 script 會開啟 MonoDevelop 來編輯, 在 Update() 方法中加一行:
// Update is called once per frame
void Update () {
transform.Rotate(Vector3.up, Time.fixedDeltaTime * 200.0f);
}
存檔後回到 Unity,按下上方的 Play 符號就會看到 Cube 旋轉的樣子。
接下來我們想在旋轉的 Cube 上新增一些花樣: 至 Project 的 Assets 中按右鍵: Import Packages –> Particles, 然後直接按 import
imageimage
完成後點開 Assets –> Standard Assets –> Particles,然後選擇 Smoke,把 Fluffy Smoke 拖拉到 Hierarchy 下,就會看到 Cube 在冒煙。
image
如以上步驟,再選擇 Fire 後,把 Flame 拖拉到 Hierarchy 下,就會看到 Cube 冒出火來;你也可以 Play 看看效果。
image
接下來,要把它建制成 Windows Store App了!
在 File –> Build Settings…中,選擇 Windows Store App –> XAML C# Solution,然後 Build And Run 後即可。
image
建制完畢後會在資料夾中看到完整的 Windows Store App solution:
image
我們以 Visual Studio 2012 開啟 .sln 檔,若直接編譯的話會看到以下錯誤:
image
錯誤訊息為:
Error: DEP0700: Registration of the app failed. Windows cannot install package “你的專案名” because the package requires architecture ARM, but this computer has architecture x64 (0x80073cf3).
其原因是 Unity 預設將它的 package 包成可在 ARM, x86 及 x64 不同平台執行的版本,而您的 Visual Studio 顯然並不是在 ARM 機器上跑的,所以會產生這個錯誤。
解決方法是,至 Visual Studio 的 Build –> Configuration Manager 中選擇 x86 即可。
image
這是順利執行之後的預設 splash screen:
image
跑起來的樣子如下
image

延伸閱讀: 繼 Unity 之後,Cocos2d-x 也正式支援 Windows 8 及 Windows Phone App 了!