[Unity Game to Windows Store] Compile error- missing namespaces/classes

如以下,在 Unity 中 Switch Platform 至 Windows Store Apps 後 “Build”:

image

出現的錯誤訊息為: “error CS0103: The name ‘File’ does not exist in the current context”。

Unity 上開發的遊戲 porting 至 W8/WP 平台時,第一個也是最常遇到的問題,即是有某些 namespaces 及 classes 在 Mono run-time 中可以編譯,但卻因不存在於 W8/WP 的 run-time 中,而造成 Compile error 的情形。

我們先直接來解決它,之後會有較詳細的說明。

如何解決?

[以下環境以Unity 4.3.3 + Windows 8.1 + Visual Studio 2013 作說明,解法適用於 Windows Store App 及 Windows Phone 8]

步驟1: 請至https://aka.ms/UnityPortingSamples 下載位於 GitHub 中的 UnityPorting (C#) 專案 (Download ZIP),這個專案實作了許多 missing namespaces/classes,只要在 Unity 中的參考加入此專案建制出來的 .dll 檔即可解決這個常見的問題。

1-1. 解壓縮後,雙點位於 UnityPorting-masterUnityPorting-masterPlatformerPlugin 目錄下的 PlatformerPlugin.sln (會以 Visual Studio 2013 開啟)。

1-2. 刪除方案中的 Facebook 資料夾;「只」重新建置 MyPluginUnity 這個專案,因為我們僅需要其 MyPlugin.dll 檔! (建置後會在 UnityPorting-masterUnityPorting-masterPlatformerPluginMyPluginUnitybinDebug 目錄中)

imageimage

步驟2: 將 MyPlugin.dll 檔加入 Unity 的參考 (references) 中:

[2014/2/24更新] 用滑鼠把 MyPlugin.dll 檔拖拉至 Plugins 資料夾中即可:

image

回到 Unity ,點擊錯誤訊息,於發生錯誤的 .cs 檔案加入以下 Using directives,同時並記得將既有的 Using System.IO 移除。

#if UNITY_METRO_8_1 && !UNITY_EDITOR
using LegacySystem.IO;
#else
using System.IO;
#endif

image

請各位以所缺少的 classes 所在的 namespace 作相對應的修改;以上是以 System.IO 中所缺少的 classes 為例,以下則是缺少 System.Net 中的 Sockets 之例:

“error CS0234: The type or namespace name ‘Sockets’ does not exist in the namespace ‘System.Net’ (are you missing an assembly reference?)”

image

存檔後回到 Unity 重新 Build 即可發現錯誤已解決;若無其他錯誤的話即會發現在所選資料夾中已成功編譯出一個 Windows Store App 方案,點擊其 .sln 檔即可以 Visual Studio 開啟編譯。

原因及說明:

此問題發生的原因其實很簡單,Mono .NET 及 W8/WP .NET 這兩個 run-time 雖然都是屬於整體 .NET run-time 的子集合,但是兩個子集合圈圈之中,會有 namespaces/classes 不在交集內的情形 (藍色半月型區域):

image

MyPlugin.dll 這個檔案即是我們在以上步驟中的重點,在其中已實作了許多出現在 Mono run-time ,但卻未出現在 W8/WP run-time 的 namespaces/classes。 因此 Unity 開發者只要直接 Using 進來,即可在不更動既有的 classes 名稱下,編譯出相容於 W8/WP run-time 的遊戲;而又由於所有的 classes 名稱都沒被更動,因此不會影響遊戲在其他平台 (iOS/Android 等) 的相容性。

詳細來說,於步驟1所建置出來的 MyPlugin.dll 之中,至少實作了以下的 namespaces/classes:

– System.Collections 之中的: Hashtable, ArrayList, OrderedDictionary, SortedList, Queue, Stack and a few others.

– System.IO.File, System.IO.StreamReader, System.IO.Directory, etc.

– System.Net.TCPClient using Windows.Networking.Sockets WinRT namespace:

– System.Threading.Tread

– (optional) Facebook plugin

亦即若您的 Compile error 是因為找不到以上的 classes,那以上步驟都可以解決您的問題。

以上步驟的詳細說明請見這份 Unity3D/Microsoft 的官方文件Porting tips for Windows Store (813 KB) 中的第4 ~ 6 頁。

另,線上課程 (英文) Porting Unity games to Windows 8.1 & Windows Phone 中的 Module 3 就特別討論到 Duel Runtime environment ,建議一聽。

更完整 porting 至 W8/WP 平台的文件可見: http://unity3d.com/pages/windows/porting 

我常看哪些部落格?–Windows Store App 相關網路資源分享

首先,Windows Store App 在 Facebook 有中文討論社團喔! 這裡提供了互相討論的自由空間,同時也能即時獲得免費課程、最新技術變化等資訊,歡迎已在開發或有興趣接觸的夥伴加入: https://www.facebook.com/groups/331827086897287/ 

image

   

中文部落格:

.Net 海角點部落– 人稱 Bill 淑,MVP,也是 Windows Store App 獲利模式探討的主講人,同時亦專精於 Windows Phone App 開發。

開發者之魂– 人稱上官神人,在 Windows Azure, Windows Store App 及 Windows Phone App 皆有專精,如精通 Windows Store App / Windows Phone App 的 ListView / ListBox 控制項

Blogs of Han– 作者目前任職於中國微軟,已集結許多較具深度的文章,如Windows 8.1下ListView和GridView的数据分步显示 & 在MVVM架构下实现将基于ListViewBase控件的显示项滚动到可视范围内(Windows 8.1)

Windows Store apps development support– 作者亦任職於中國微軟,好文如 如何正确使用Background Task的几点建议 & How to use new controls in Windows 8.1

Mr.BIG– 曾擔任微軟學生大使,同時在 「2012 Windows 開發嘉年華」比賽中勇奪冠軍的優秀學生,如果想快速了解 Windows 8.1 的新功能,他的兩篇文章可以作為快速的入門: 初探WINDOWS RUNTIME FOR WINDOWS 8.1 & XAML FOR WINDOWS 8.1的求新求變

Frank– 還是個學生喔! 但已成功發佈許多 Windows Store Apps 在市集上了,文章教學性高,同時也非常勤於分享,代表文章如: [Windows 8 App]上架流程全紀錄

科技來自於人性– 同樣還是個學生! 已有許多 Windows Store 相關文章,著重在開發上的小技巧及發現,如 Windows 8.1 拍照與攝影—CameraCaptureUI 類別

亂亂寫 Blog– 文章著重在開發工具 (Visual Studio)、開發流程控管及企業導入,文章 Native Web App 的登入三兩事…討論在 WebView 中確保登入狀態的方法,是企業實作 App 時很常見的情況。

小朱® 的技術隨手寫– Windows Azure 大師,可深入了解在 Windows Store App 中使用微軟雲端運算。

黃忠成– 微軟專業講師及 MVP,Tech Days 2013 – Azure Mobile ServicesMSDN – Windows 8 非同步開發

No.18 – HTML5 & JavaScript程式開發實戰」的共同作者,JavaScript/WebAPI 高手,文章如: HTML5 & JavaScript New Feature In Windows 8.1

Bruce Chen– 於台灣微軟擔任企業技術支援,有如何使用 Live SDK 的文章。

 

英文部落格:

Windows App Builder Blog: 微軟官方部落格,主要在重要訊息的發佈,如 BUILD 2014 將於 2014/4/2~4/4 舉行。

Project Siena– Siena 是微軟官方開發出來的 App,可以讓非開發人員拖拖拉拉即作出自己的 Windows Store App: Project Siena: 非開發者也能打造 Windows 8.1 App

Ibrahim KIVANC – DPE Technical Evangelist: 有 Windows 8.1 App 開發的一系列教學文。

Jerry Nixon– 常常會出現許多有趣的範例及文章,如讓你的 XAML 下雪 Make it snow with XAML 及處理平版轉向的兩個方法: The two ways to handle orientation in your Windows 8.1 app

Windows Store Developer Solutions– 是個共筆 blog, 集結了許多微軟內部技術人員的文章,比如 Ten Things You Need to Know About WebView (雖然Windows 8.1 在 WebView 上已作了許多改變) 及 Accessibility Gotchas 1: Xaml ListView speaks in tongues

Jeremy Likness’ Blog– 有許多 AngularJs 及 Windows Store App 等相關文章。

中文教學影片:

Channel9 Taiwan 第九頻道– 包含開發工具、Web Design、Windows Azure、Windows Phone 及 Windows Store App 等中文影片。

Unity3D 轉成 Windows Phone App 或 Windows Store App:

– 在 Untiy 的官網中有4份 PDF 文件 (http://unity3d.com/pages/windows/porting),若您要 porting Unity Games 至 Windows 平台的話的必讀! 

– 另,MVA 中也有課程 (英文): Porting Unity games to Windows 8.1 & Windows Phone

[Unity] 開發 Windows Store App 相關資源整理

[你需要什麼?]

Windows 8 或以上版本:

Free 90 days eval, or

– 若符合學生資格或成立五年以下新創公司,可分別透過 DreamSparkBizSpark 取得免費高階版本

– MSDN 訂戶可免費下載

Unity 4.2 或以上版本:

Free 30 days trial (Pro included)

Visual Studio 2012 或以上版本:

Free Express version, or

– 若符合學生資格或成立五年以下新創公司,可分別透過 DreamSparkBizSpark 取得免費高階版本

– MSDN 訂戶可免費下載

Windows 市集開發人員帳戶 (上架 Windows Store 時需要)

開立 Windows 市集開發人員帳戶 (個人帳戶: US$19,公司帳戶: US$99), or

– 若符合學生資格或成立五年以下新創公司,可分別透過 DreamSparkBizSpark 取得一年免費帳戶

– MSDN 訂戶亦有一年免費帳戶

[有正式文件或線上資源可參考嗎?]

– Unity3D/Microsoft 的官方文件請見 Porting tips for Windows Store (813 KB)

– 線上課程 (英文) Porting Unity games to Windows 8.1 & Windows Phone 中的 Module 2~3 特別討論到注意事項及 Duel Runtime environment ,建議一聽。

[開發 Windows Store 或 Windows Phone App 的流程?]

可以簡單分為三步驟:

image 

其中與 iOS 最大的不同是第二點,也就是需要透過 Visual Studio 2012 或以上的版本來進行編繹。

Unity 專案在 “Build And Run” 之後會產生一個完整的 Visual Studio solution,其中的 .sln 檔可以用 Visual Studio 開啟。

imageimage

開發者可以直接編譯成可上傳 Store 的 packages,或是透過 Visual Studio 進行進一步的修改。

Visual Studio 提供完整的模擬器進行測試,不一定要將 App 裝在實體機器上測試。

image

[我遇到 “error CS0103: The name ‘xxxx’ does not exist in the current context”]

其中的 ‘xxxx’ 可能是 File, FileStream, HashTable, Dictionary 等等。解法及原因說明請參考 [Unity Game to Windows Store] Compile error- missing namespaces/classes (中文)

[我遇到 DEP0700 編譯錯誤怎麼辦?]

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

[我可以在 Visual Studio 上 debug 嗎?]

目前支援對 C# scripts 作偵錯。詳細步驟請見: http://docs.unity3d.com/Documentation/Manual/windowsstore-debugging.html

[那我是用 JavaScript 或 Boo 怎麼辦?]

這是許多既有 Unity 專案會遇到的問題,各位可參考這篇文章Creating a Unity Game for Windows 8中的留言,也就是可以善加使用 ILSpy (http://ilspy.net/) 這個工具。

“The major challenge for existing apps is porting the code from JavaScript to C#. Most of the examples and existing code base (plugin’s etc.) are JavaScript. However there is a very good and free tool called ILSpy that will examine the compiled assembly and decompile it into C#. Private variables will lose their names, some of the code will be hard to read, however it does work and in about 1-3 hours of work you should be able to port your existing Unity Android/iOS game to Windows Phone or Store. This is by far easier than writing a new game in Visual Studio.

Of course the reverse is also true, write your game in Unity for WP8 or W8 and easily (in minutes) port it over to Android iOS”

[有支援我常用的 Plug-ins 嗎?]

以很受歡迎的 Prime31 為例,目前已有 Windows 8 及 Windows Phone 8 的版本,可以持續使用。

image

[可以教我怎麼註冊 Windows Store 及 Windows Phone 市集嗎? ]

以下兩份文件包含註冊以及上架你的 App 的詳細步驟,以及可能遇到的問題說明:

[其他]

Unity 官方網站亦整理了許多 Windows Store 及 Windows Phone 8 的 FAQ 等相關文件如以下:

延伸閱讀: 我的第一個 Unity3D Windows Store App

(Last update: 2014/1/17)

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

Cocos2d 也是一個廣為許多遊戲開發者使用的遊戲引擎,在這個版本中除了開始支援 Windows Store App 及 Windows Phone App 之外,也將支援第一個穩定的 CocoStudio v1.0 版本:

image

來源: http://www.cocos2d-x.org/wiki/Release_Notes_for_Cocos2d-x_v22

下載 cocos2d-x-2.2.0.zip

相關內容: 我的第一個 Unity3D Windows Store App

我的第一個 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 了!