HoloLens (第1代) 和 Azure 308:跨裝置通知HoloLens (1st gen) and Azure 308: Cross-device notifications


注意

混合實境學院教學課程的設計是以 HoloLens (第 1 代) 和混合實境沉浸式頭戴裝置為準。The Mixed Reality Academy tutorials were designed with HoloLens (1st gen) and Mixed Reality Immersive Headsets in mind. 因此,對於仍在尋找這些裝置開發指引的開發人員而言,我們覺得這些教學課程很重要。As such, we feel it is important to leave these tutorials in place for developers who are still looking for guidance in developing for those devices. 這些教學課程 不會 使用用於 HoloLens 2 的最新工具組或互動進行更新。These tutorials will not be updated with the latest toolsets or interactions being used for HoloLens 2. 系統會保留這些資訊,以繼續在支援的裝置上運作。They will be maintained to continue working on the supported devices. 未來將會有一系列新的教學課程,將會示範如何針對 HoloLens 2 進行開發。There will be a new series of tutorials that will be posted in the future that will demonstrate how to develop for HoloLens 2. 當張貼這些教學課程時,將會使用這些教學課程的連結來更新此通知。This notice will be updated with a link to those tutorials when they are posted.


最終產品-開始

在此課程中,您將瞭解如何使用 Azure 通知中樞、Azure 資料表和 Azure Functions,將通知中樞功能新增至混合現實應用程式。In this course, you will learn how to add Notification Hubs capabilities to a mixed reality application using Azure Notification Hubs, Azure Tables, and Azure Functions.

Azure 通知中樞 是一項 Microsoft 服務,可讓開發人員將目標和個人化的推播通知傳送至任何平臺,這些都是在雲端中提供。Azure Notification Hubs is a Microsoft service, which allows developers to send targeted and personalized push notifications to any platform, all powered within the cloud. 這可以有效地讓開發人員與終端使用者進行通訊,甚至在不同的應用程式之間進行通訊,視案例而定。This can effectively allow developers to communicate with end users, or even communicate between various applications, depending on the scenario. 如需詳細資訊,請造訪 Azure 通知中樞頁面For more information, visit the Azure Notification Hubs page.

Azure Functions 是一項 Microsoft 服務,可讓開發人員在 Azure 中執行一小段程式碼,也就是「函數」。Azure Functions is a Microsoft service, which allows developers to run small pieces of code, 'functions', in Azure. 這可提供將工作委派給雲端的方法,而不是您的本機應用程式,這可能有許多優點。This provides a way to delegate work to the cloud, rather than your local application, which can have many benefits. Azure Functions 支援數種開發語言,包括 C # 、F # 、Node.js、JAVA 和 PHP。Azure Functions supports several development languages, including C#, F#, Node.js, Java, and PHP. 如需詳細資訊,請造訪 Azure Functions 頁面For more information, visit the Azure Functions page.

Azure 資料表 是一項 Microsoft 雲端服務,可讓開發人員將結構化的非 SQL 資料儲存在雲端中,方便隨處存取。Azure Tables is a Microsoft cloud service, which allows developers to store structured non-SQL data in the cloud, making it easily accessible anywhere. 此服務具有無架構設計,可讓您視需要進行資料表演進,因此非常有彈性。The service boasts a schemaless design, allowing for the evolution of tables as needed, and thus is very flexible. 如需詳細資訊,請造訪 Azure 資料表頁面For more information, visit the Azure Tables page

完成本課程之後,您將會有一個混合現實的沉浸式耳機應用程式,以及一個可執行下列動作的桌上型電腦應用程式:Having completed this course, you will have a mixed reality immersive headset application, and a Desktop PC application, which will be able to do the following:

  1. 桌上型電腦應用程式可讓使用者使用滑鼠移動2D 空間 (X 和 Y) 中的物件。The Desktop PC app will allow the user to move an object in 2D space (X and Y), using the mouse.

  2. 電腦應用程式中的物件移動將會使用 JSON 傳送至雲端,其格式為字串,其中包含物件識別碼、類型和轉換資訊 (X 和 Y 座標) 。The movement of objects within the PC app will be sent to the cloud using JSON, which will be in the form of a string, containing an object ID, type, and transform information (X and Y coordinates).

  3. 在桌面應用程式中具有相同場景的混合現實應用程式,將會收到有關物件移動的通知,從「桌上型電腦」應用程式) 剛更新的「通知中樞」服務 (。The mixed reality app, which has an identical scene to the desktop app, will receive notifications regarding object movement, from the Notification Hubs service (which has just been updated by the Desktop PC app).

  4. 當收到通知(其中包含物件識別碼、類型和轉換資訊)時,mixed reality 應用程式會將接收到的資訊套用至本身的場景。Upon receiving a notification, which will contain the object ID, type, and transform information, the mixed reality app will apply the received information to its own scene.

在您的應用程式中,您可以自行決定如何將結果與您的設計整合。In your application, it is up to you as to how you will integrate the results with your design. 本課程旨在告訴您如何整合 Azure 服務與您的 Unity 專案。This course is designed to teach you how to integrate an Azure Service with your Unity Project. 您的工作是使用您在本課程中所得到的知識,來增強您的混合現實應用程式。It is your job to use the knowledge you gain from this course to enhance your mixed reality application. 本課程是獨立的教學課程,不會直接牽涉到任何其他混合的現實實驗室。This course is a self-contained tutorial, which does not directly involve any other Mixed Reality Labs.

裝置支援Device support

課程Course HoloLensHoloLens 沉浸式頭戴裝置Immersive headsets
MR 和 Azure 308:跨裝置通知MR and Azure 308: Cross-device notifications ✔️✔️ ✔️✔️

注意

雖然本課程主要著重于 Windows Mixed Reality 沉浸式 (VR) 耳機,您也可以將在本課程中學到的內容套用至 Microsoft HoloLens。While this course primarily focuses on Windows Mixed Reality immersive (VR) headsets, you can also apply what you learn in this course to Microsoft HoloLens. 當您依照課程的指示進行時,您將會看到有關您可能需要採用以支援 HoloLens 的任何變更的注意事項。As you follow along with the course, you will see notes on any changes you might need to employ to support HoloLens. 使用 HoloLens 時,您可能會注意到語音捕捉期間的一些回應。When using HoloLens, you may notice some echo during voice capture.

PrerequisitesPrerequisites

注意

本教學課程是專為擁有 Unity 和 c # 基本經驗的開發人員所設計。This tutorial is designed for developers who have basic experience with Unity and C#. 另外也請注意,本檔中的必要條件和撰寫的指示,代表在撰寫 (可能是 2018) 時經過測試和驗證的內容。Please also be aware that the prerequisites and written instructions within this document represent what has been tested and verified at the time of writing (May 2018). You are free to use the latest software, as listed within the install the tools article, though it should not be assumed that the information in this course will perfectly match what you'll find in newer software than what's listed below.You are free to use the latest software, as listed within the install the tools article, though it should not be assumed that the information in this course will perfectly match what you'll find in newer software than what's listed below.

本課程建議您採用下列硬體和軟體:We recommend the following hardware and software for this course:

在您開始使用 Intune 之前Before you start

  • 為了避免在建立此專案時發生問題,強烈建議您在根或近端根資料夾中,建立本教學課程中所述的專案 (長的資料夾路徑可能會在組建階段) 時發生問題。To avoid encountering issues building this project, it is strongly suggested that you create the project mentioned in this tutorial in a root or near-root folder (long folder paths can cause issues at build-time).
  • 您必須是 Microsoft 開發人員入口網站和應用程式註冊入口網站的擁有者,否則您將不具有在 第2章中存取應用程式的許可權。You must be the owner of your Microsoft Developer Portal and your Application Registration Portal, otherwise you will not have permission to access the app in Chapter 2.

第1章-在 Microsoft 開發人員入口網站上建立應用程式Chapter 1 - Create an application on the Microsoft Developer Portal

若要使用 Azure 通知中樞 服務,您必須在 Microsoft 開發人員入口網站上建立應用程式,因為您的應用程式將需要註冊,才能傳送和接收通知。To use the Azure Notification Hubs Service, you will need to create an Application on the Microsoft Developer Portal, as your application will need to be registered, so that it can send and receive notifications.

  1. 登入 Microsoft 開發人員入口網站Log in to the Microsoft Developer Portal.

    您必須登入您的 Microsoft 帳戶。You will need to log in to your Microsoft Account.

  2. 從儀表板中,按一下 [ 建立新的應用程式]。From the Dashboard, click Create a new app.

    建立應用程式

  3. 快顯視窗隨即出現,您需要為新的應用程式保留名稱。A popup will appear, wherein you need to reserve a name for your new app. 在文字方塊中,插入適當的名稱;如果選擇的名稱可供使用,則會在文字方塊右側顯示一個刻度。In the textbox, insert an appropriate name; if the chosen name is available, a tick will appear to the right of the textbox. 一旦插入可用名稱之後,請按一下快顯視窗左下角的 [ 保留產品名稱 ] 按鈕。Once you have an available name inserted, click the Reserve product name button to the bottom left of the popup.

    反轉名稱

  4. 現在已建立應用程式,您已準備好移至下一章。With the app now created, you are ready to move to the next Chapter.

第2章-取得新的應用程式認證Chapter 2 - Retrieve your new apps credentials

登入應用程式註冊入口網站,其中會列出您的新應用程式,並取得將用來在 Azure 入口網站 中設定 通知中樞服務 的認證。Log into the Application Registration Portal, where your new app will be listed, and retrieve the credentials which will be used to setup the Notification Hubs Service within the Azure Portal.

  1. 流覽至 應用程式註冊入口網站Navigate to the Application Registration Portal.

    應用程式註冊入口網站

    警告

    您必須使用您的 Microsoft 帳戶登入。You will need to use your Microsoft Account to Login.
    必須 是您在上一 中使用的 Microsoft 帳戶,以及 Windows Store 開發人員入口網站。This must be the Microsoft Account which you used in the previous Chapter, with the Windows Store Developer portal.

  2. 您會在 [ 我的應用程式 ] 區段下找到您的應用程式。You will find your app under the My applications section. 找到之後,請按一下該頁面,您會進入新的頁面,其中包含應用程式名稱加上 註冊Once you have found it, click on it and you will be taken to a new page which has the app name plus Registration.

    新註冊的應用程式

  3. 向下滾動註冊頁面,以找出 應用程式 的 [秘密] 區段和 [ 套件 SID ]。Scroll down the registration page to find your Application Secrets section and the Package SID for your app. 在下一章中,複製兩者以用於設定 Azure 通知中樞服務Copy both for use with setting up the Azure Notification Hubs Service in the next Chapter.

    應用程式秘密

第3章-設定 Azure 入口網站:建立通知中樞服務Chapter 3 - Setup Azure Portal: create Notification Hubs Service

抓取您的應用程式認證之後,您將需要前往 Azure 入口網站,您將在其中建立 Azure 通知中樞服務。With your apps credentials retrieved, you will need to go to the Azure Portal, where you will create an Azure Notification Hubs Service.

  1. 登入 Azure 入口網站Log into the Azure Portal.

    注意

    如果您還沒有 Azure 帳戶,您將需要建立一個帳戶。If you do not already have an Azure account, you will need to create one. 如果您在課堂或實驗室的情況下進行本教學課程,請洽詢講師或其中一個 proctors,協助您設定新的帳戶。If you are following this tutorial in a classroom or lab situation, ask your instructor or one of the proctors for help setting up your new account.

  2. 登入後,按一下左上角的 [ 新增 ],並搜尋 [ 通知中樞],然後按一下 [ 輸入]。Once you are logged in, click on New in the top left corner, and search for Notification Hub, and click Enter.

    搜尋通知中樞

    注意

    在較新的入口網站中,「新增」這個字可能已取代為 _ * 建立資源 * *。The word *New _ may have been replaced with _*Create a resource**, in newer portals.

  3. 新頁面將提供 通知中樞 服務的描述。The new page will provide a description of the Notification Hubs service. 在此提示的左下方,選取 [ 建立 ] 按鈕,以建立與此服務的關聯。At the bottom left of this prompt, select the Create button, to create an association with this service.

    建立通知中樞實例

  4. 當您按一下 [ 建立] 之後:Once you have clicked on Create:

    1. 為此服務實例插入您想要的名稱。Insert your desired name for this service instance.

    2. 提供您可以與此應用程式建立關聯的 命名空間Provide a namespace which you will be able to associate with this app.

    3. 選取 位置。Select a Location.

    4. 選擇資源群組,或建立一個新的 資源群組Choose a Resource Group or create a new one. 資源群組提供一種方式來監視、控制存取、布建及管理 Azure 資產集合的計費。A resource group provides a way to monitor, control access, provision and manage billing for a collection of Azure assets. 建議您保留與單一專案相關聯的所有 Azure 服務 (例如,) 一般資源群組下的實驗室,) 。It is recommended to keep all the Azure services associated with a single project (e.g. such as these labs) under a common resource group).

      如果您想要深入瞭解 Azure 資源群組,請遵循此 連結,以瞭解如何管理資源群組If you wish to read more about Azure Resource Groups, please follow this link on how to manage a Resource Group.

    5. 選取適當的 用帳戶。Select an appropriate Subscription.

    6. 您也必須確認您已瞭解套用到此服務的條款及條件。You will also need to confirm that you have understood the Terms and Conditions applied to this Service.

    7. 選取 [建立] 。Select Create.

      填寫服務詳細資料

  5. 當您按一下 [ 建立] 之後,就必須等待服務建立完成,這可能需要一分鐘的時間。Once you have clicked on Create, you will have to wait for the service to be created, this might take a minute.

  6. 建立服務實例之後,入口網站中就會出現通知。A notification will appear in the portal once the Service instance is created.

    通知

  7. 按一下通知中的 [ 移至資源 ] 按鈕,以流覽您的新服務實例。Click the Go to resource button in the notification to explore your new Service instance. 您將會進入新的 通知中樞 服務實例。You will be taken to your new Notification Hub service instance.

    移至資源

  8. 從頁面的 [總覽] 頁面中,按一下 [ Windows (WNS) 。From the overview page, halfway down the page, click Windows (WNS). 右側的面板將會變更,以顯示兩個文字欄位,這些欄位需要您先前設定的應用程式中的 套件 SID安全性金鑰The panel on the right will change to show two text fields, which require your Package SID and Security Key, from the app you set up previously.

    新建立的中樞服務

  9. 當您將詳細資料複製到正確的欄位之後,請按一下 [ 儲存],當通知中樞已成功更新時,您就會收到通知。Once you have copied the details into the correct fields, click Save, and you will receive a notification when the Notification Hub has been successfully updated.

    複製安全性詳細資料

第4章-設定 Azure 入口網站:建立表格服務Chapter 4 - Setup Azure Portal: create Table Service

建立您的通知中樞服務實例之後,請流覽回到您的 Azure 入口網站,您將藉由建立儲存體資源來建立 Azure 資料表服務。After creating your Notification Hubs Service instance, navigate back to your Azure Portal, where you will create an Azure Tables Service by creating a Storage Resource.

  1. 如果還沒有登入,請登入 Azure 入口網站If not already signed in, log into the Azure Portal.

  2. 登入後,按一下左上角的 [ 新增 ],並搜尋 [ 儲存體帳戶],然後按一下 [ 輸入]。Once logged in, click on New in the top left corner, and search for Storage account, and click Enter.

    注意

    在較新的入口網站中,「新增」這個字可能已取代為 _ * 建立資源 * *。The word *New _ may have been replaced with _*Create a resource**, in newer portals.

  3. 從清單中選取 [ 儲存體帳戶]-blob、檔案、資料表、佇列Select Storage account - blob, file, table, queue from the list.

    搜尋儲存體帳戶

  4. 新頁面將提供 儲存體帳戶 服務的描述。The new page will provide a description of the Storage account service. 在此提示的左下方,選取 [ 建立 ] 按鈕以建立此服務的實例。At the bottom left of this prompt, select the Create button, to create an instance of this service.

    建立儲存體實例

  5. 一旦您按一下 [ 建立],將會出現一個面板:Once you have clicked on Create, a panel will appear:

    1. 為此服務實例插入您想要的 名稱 (必須全部都是小寫) 。Insert your desired Name for this service instance (must be all lowercase).

    2. 若為 部署模型,請按一下 [ Resource manager]。For Deployment model, click Resource manager.

    3. 針對 帳戶類型,使用下拉式功能表選取 [儲存體 (一般用途 v1)For Account kind, using the dropdown menu, select Storage (general purpose v1).

    4. 選取適當的 位置Select an appropriate Location.

    5. 在 [ 複寫] 下拉式功能表中,選取 [讀取權限-異地-多餘儲存體] (GRS])For the Replication dropdown menu, select Read-access-geo-redundant storage (RA-GRS).

    6. 針對 [ 效能],請按一下 [ 標準]。For Performance, click Standard.

    7. 在 [ 需要安全傳輸 ] 區段中,選取 [ 已停用]。Within the Secure transfer required section, select Disabled.

    8. 從 [ 用帳戶] 下拉式功能表中,選取適當的訂用帳戶。From the Subscription dropdown menu, select an appropriate subscription.

    9. 選擇資源群組,或建立一個新的 資源群組Choose a Resource Group or create a new one. 資源群組提供一種方式來監視、控制存取、布建及管理 Azure 資產集合的計費。A resource group provides a way to monitor, control access, provision and manage billing for a collection of Azure assets. 建議您保留與單一專案相關聯的所有 Azure 服務 (例如,) 一般資源群組下的實驗室,) 。It is recommended to keep all the Azure services associated with a single project (e.g. such as these labs) under a common resource group).

      如果您想要深入瞭解 Azure 資源群組,請遵循此 連結,以瞭解如何管理資源群組If you wish to read more about Azure Resource Groups, please follow this link on how to manage a Resource Group.

    10. 如果這是您可以選擇的選項,請將 虛擬網路 保留為 已停用Leave Virtual networks as Disabled if this is an option for you.

    11. 按一下頁面底部的 [新增] 。Click Create.

      填入儲存體詳細資料

  6. 當您按一下 [ 建立] 之後,就必須等待服務建立完成,這可能需要一分鐘的時間。Once you have clicked on Create, you will have to wait for the service to be created, this might take a minute.

  7. 建立服務實例之後,入口網站中就會出現通知。A notification will appear in the portal once the Service instance is created. 按一下通知以探索新的服務實例。Click on the notifications to explore your new Service instance.

    新儲存體通知

  8. 按一下通知中的 [ 移至資源 ] 按鈕,以流覽您的新服務實例。Click the Go to resource button in the notification to explore your new Service instance. 您將會進入新的儲存體服務實例總覽頁面。You will be taken to your new Storage Service instance overview page.

    移至資源

  9. 從 [總覽] 頁面的右側,按一下 [ 資料表]From the overview page, to the right-hand side, click Tables.

  10. 右側的面板將會變更,以顯示 資料表服務 資訊,您需要在其中加入新的資料表。The panel on the right will change to show the Table service information, wherein you need to add a new table. 若要這麼做,請按一下左上角的 [ + 資料表 ] 按鈕。Do this by clicking the + Table button to the top-left corner.

    開啟資料表

  11. 將會顯示新的頁面,您必須在其中輸入 資料表名稱A new page will be shown, wherein you need to enter a Table name. 這是您將在稍後的章節中用來參考應用程式中資料的名稱。This is the name you will use to refer to the data in your application in later Chapters. 插入適當的名稱,然後按一下 [確定]Insert an appropriate name and click OK.

    建立新的資料表

  12. 建立新的資料表之後,您將能夠在底部) 的 [ 表格服務 ] 頁面 (中看到它。Once the new table has been created, you will be able to see it within the Table service page (at the bottom).

    已建立新的資料表

第5章-在 Visual Studio 中完成 Azure 資料表Chapter 5 - Completing the Azure Table in Visual Studio

現在您已設定 表格服務 儲存體帳戶,接下來可以將資料新增至其中,以用來儲存和取出資訊。Now that your Table service storage account has been setup, it is time to add data to it, which will be used to store and retrieve information. 您可以透過 Visual Studio 來編輯資料表。The editing of your Tables can be done through Visual Studio.

  1. 開啟 Visual StudioOpen Visual Studio.

  2. 從功能表中,按一下 [ View > Cloud Explorer]。From the menu, click View > Cloud Explorer.

    開啟 cloud explorer

  3. Cloud Explorer 會以停駐的專案開啟 (請耐心等候,因為載入可能需要一些時間) 。The Cloud Explorer will open as a docked item (be patient, as loading may take time).

    注意

    如果看不到您用來建立 儲存體帳戶 的訂用帳戶,請確定您有:If the Subscription you used to create your Storage Accounts is not visible, ensure that you have:

    • 登入與您用於 Azure 入口網站的帳戶相同的帳戶。Logged in to the same account as the one you used for the Azure Portal.

    • 從 [帳戶管理] 頁面中選取您的訂用帳戶 (您可能需要從帳戶設定套用篩選) :Selected your Subscription from the Account Management Page (you may need to apply a filter from your account settings):

      尋找訂用帳戶

  4. 將會顯示您的 Azure 雲端服務。Your Azure cloud services will be shown. 尋找 儲存體帳戶 ,然後按一下左邊的箭號以展開您的帳戶。Find Storage Accounts and click the arrow to the left of that to expand your accounts.

    開啟儲存體帳戶

  5. 展開之後,您就可以使用新建立的 儲存體帳戶Once expanded, your newly created Storage account should be available. 按一下儲存體左邊的箭號,然後展開 [尋找 資料表 ],然後按一下旁邊的箭號,以顯示您在上一章所建立的 資料表Click the arrow to the left of your storage, and then once that is expanded, find Tables and click the arrow next to that, to reveal the Table you created in the last Chapter. 按兩下您的 資料表Double click your Table.

    開啟場景物件資料表

  6. 您的資料表將會在 Visual Studio 視窗的中央開啟。Your table will be opened in the center of your Visual Studio window. 按一下包含 + (加) 的資料表圖示。Click the table icon with the + (plus) on it.

    加入新的資料表

  7. 系統會出現一個視窗,提示您 加入實體A window will appear prompting for you to Add Entity. 您將會總共建立三個實體,每個實體都有數個屬性。You will create three entities in total, each with several properties. 您將會注意到已提供 PartitionKeyRowKey ,因為資料表會使用這些資料來尋找您的資料。You will notice that PartitionKey and RowKey are already provided, as these are used by the table to find your data.

    分割區和資料列索引鍵

  8. 更新 PartitionKeyRowKey,如下所示 (記得針對您加入的每個資料列屬性執行這項作業,但每次都要遞增 RowKey) :Update the Value of the PartitionKey and RowKey as follows (remember to do this for each row property you add, though increment the RowKey each time):

    新增正確的值

  9. 按一下 [ 加入屬性 ] 以新增額外的資料列。Click Add property to add extra rows of data. 讓您的第一個空白資料表符合下表。Make your first empty table match the below table.

  10. 當您完成時,請按一下 [確定]Click OK when you are finished.

    完成時按一下 [確定]

    警告

    確定您已將 XYZ 專案的 類型 變更為 DoubleEnsure that you have changed the Type of the X, Y, and Z, entries to Double.

  11. 您會注意到您的資料表現在有一個資料列。You will notice your table now has a row of data. 再按一下 + (加號) 圖示,以新增另一個實體。Click the + (plus) icon again to add another entity.

    第一個資料列

  12. 建立額外的屬性,然後將新實體的值設定為符合如下所示的值。Create an additional property, and then set the values of the new entity to match those shown below.

    加入 cube

  13. 重複最後一個步驟來新增另一個實體。Repeat the last step to add another entity. 將此實體的值設定為如下所示的值。Set the values for this entity to those shown below.

    新增圓柱圖

  14. 您的資料表現在看起來應該如下所示。Your table should now look like the one below.

    資料表完成

  15. 您已完成這一章。You have completed this Chapter. 請務必儲存。Make sure to save.

第6章-建立 Azure 函數應用程式Chapter 6 - Create an Azure Function App

建立 Azure 函式應用程式,讓傳統型應用程式呼叫此應用程式來更新 表格 服務,並透過 通知中樞 傳送通知。Create an Azure Function App, which will be called by the Desktop application to update the Table service and send a notification through the Notification Hub.

首先,您必須建立可讓您的 Azure 函式載入所需程式庫的檔案。First, you need to create a file that will allow your Azure Function to load the libraries you need.

  1. 開啟 [ 記事本 ] (按下 Windows 鍵,然後輸入 Notepad) 。Open Notepad (press Windows Key and type notepad).

    開啟 [記事本]

  2. 開啟 [記事本],將下列 JSON 結構插入其中。With Notepad open, insert the JSON structure below into it. 完成之後,請將它儲存在您的桌面上,作為 project.jsOnce you have done that, save it on your desktop as project.json. 命名正確是很重要的:請確定它沒有副檔名 .txtIt is important that the naming is correct: ensure it does NOT have a .txt file extension. 此檔案會定義您的函式將使用的程式庫,如果您已使用 NuGet,則會很熟悉。This file defines the libraries your function will use, if you have used NuGet it will look familiar.

    {
    "frameworks": {
        "net46":{
        "dependencies": {
            "WindowsAzure.Storage": "7.0.0",
            "Microsoft.Azure.NotificationHubs" : "1.0.9",
            "Microsoft.Azure.WebJobs.Extensions.NotificationHubs" :"1.1.0"
        }
        }
    }
    }
    
  3. 登入 Azure 入口網站Log in to the Azure Portal.

  4. 登入後,按一下左上角的 [ 新增 ],然後搜尋函式 應用程式,按 enterOnce you are logged in, click on New in the top left corner, and search for Function App, press Enter.

    搜尋函數應用程式

    注意

    在較新的入口網站中,[建立資源] 可能已取代 的文字。The word New may have been replaced with Create a resource, in newer portals.

  5. 新的頁面會提供 函數應用程式 服務的描述。The new page will provide a description of the Function App service. 在此提示的左下方,選取 [ 建立 ] 按鈕,以建立與此服務的關聯。At the bottom left of this prompt, select the Create button, to create an association with this service.

    函數應用程式實例

  6. 按一下 [ 建立] 之後,請填寫下列內容:Once you have clicked on Create, fill in the following:

    1. 針對 [ 應用程式名稱],插入您想要的此服務實例名稱。For App name, insert your desired name for this service instance.

    2. 選取 [訂用帳戶] 。Select a Subscription.

    3. 選取適合您的定價層,如果這是您第一次建立函式 App Service,則應可使用免費層。Select the pricing tier appropriate for you, if this is the first time creating a Function App Service, a free tier should be available to you.

    4. 選擇資源群組,或建立一個新的 資源群組Choose a Resource Group or create a new one. 資源群組提供一種方式來監視、控制存取、布建及管理 Azure 資產集合的計費。A resource group provides a way to monitor, control access, provision and manage billing for a collection of Azure assets. 建議您保留與單一專案相關聯的所有 Azure 服務 (例如,) 一般資源群組下的實驗室,) 。It is recommended to keep all the Azure services associated with a single project (e.g. such as these labs) under a common resource group).

      如果您想要深入瞭解 Azure 資源群組,請遵循此 連結,以瞭解如何管理資源群組If you wish to read more about Azure Resource Groups, please follow this link on how to manage a Resource Group.

    5. 針對 OS,請按一下 [Windows],因為這是所需的平臺。For OS, click Windows, as that is the intended platform.

    6. (本教學課程使用取用 方案,請選取 主控方案Select a Hosting Plan (this tutorial is using a Consumption Plan.

    7. 選取 位置 (選擇與您在上一個步驟中建立的儲存體相同的位置)Select a Location (choose the same location as the storage you have built in the previous step)

    8. 在 [ 儲存體 ] 區段中, 您必須選取您在上一個步驟中建立的儲存體服務For the Storage section, you must select the Storage Service you created in the previous step.

    9. 您將不需要在此應用程式中 Application Insights ,因此請隨時將 其保留You will not need Application Insights in this app, so feel free to leave it Off.

    10. 按一下頁面底部的 [新增] 。Click Create.

      建立新的實例

  7. 當您按一下 [ 建立 ] 之後,就必須等待服務建立完成,這可能需要一分鐘的時間。Once you have clicked on Create you will have to wait for the service to be created, this might take a minute.

  8. 建立服務實例之後,入口網站中就會出現通知。A notification will appear in the portal once the Service instance is created.

    新通知

  9. 按一下通知以探索新的服務實例。Click on the notifications to explore your new Service instance.

  10. 按一下通知中的 [ 移至資源 ] 按鈕,以流覽您的新服務實例。Click the Go to resource button in the notification to explore your new Service instance.

    移至資源

  11. 按一下 [函式 + ] 旁的 (加號) 圖示,以 建立新 的。Click the + (plus) icon next to Functions, to Create new.

    新增函數

  12. 在中央面板中,會出現 [ 函數 建立] 視窗。Within the central panel, the Function creation window will appear. 略過面板上半部的資訊,然後按一下 [ 自訂 函式] (位於藍色區域的底部 (附近),如下所示) 。Ignore the information in the upper half of the panel, and click Custom function, which is located near the bottom (in the blue area, as below).

    自訂函式

  13. 視窗內的新頁面將會顯示各種不同的函數類型。The new page within the window will show various function types. 向下滾動以查看紫色類型,然後按一下 [ HTTP PUT 元素]。Scroll down to view the purple types, and click HTTP PUT element.

    HTTP put 連結

    重要

    您可能必須向下 (向下移動頁面,而且此映射看起來可能不完全相同,如果 Azure 入口網站更新發生) ,您就會尋找稱為 HTTP PUT 的元素。You may have to scroll further the down the page (and this image may not look exactly the same, if Azure Portal updates have taken place), however, you are looking for an element called HTTP PUT.

  14. [ HTTP PUT ] 視窗隨即出現,您需要在其中設定函數 (請參閱下方的影像) 。The HTTP PUT window will appear, where you need to configure the function (see below for image).

    1. 針對 [ 語言], 使用下拉式功能表,選取 [C] # 。For Language, using the dropdown menu, select C#.

    2. 針對 [ 名稱], 輸入適當的名稱。For Name, input an appropriate name.

    3. 在 [ 驗證層級 ] 下拉式功能表中,選取 [ 函數]。In the Authentication level dropdown menu, select Function.

    4. 針對 [ 資料表名稱 ] 區段,您必須使用您先前用來建立 表格 服務的完整名稱, (包含相同的字母大小寫) 。For the Table name section, you need to use the exact name you used to create your Table service previously (including the same letter case).

    5. 在 [ 儲存體帳戶連接 ] 區段中,使用下拉式功能表,然後從該處選取您的儲存體帳戶。Within the Storage account connection section, use the dropdown menu, and select your storage account from there. 如果不存在,請按一下區段標題旁的 超連結,以顯示您的儲存體帳戶應列在其中的另一個面板。If it is not there, click the New hyperlink alongside the section title, to show another panel, where your storage account should be listed.

      新儲存體

  15. 按一下 [ 建立 ],您將會收到通知,指出已成功更新您的設定。Click Create and you will receive a notification that your settings have been updated successfully.

    create 函式

  16. 按一下 [ 建立] 之後,系統會將您重新導向至函數編輯器。After clicking Create, you will be redirected to the function editor.

    更新函式程式碼

  17. 在函式編輯器中插入下列程式碼, (取代函式中的程式碼) :Insert the following code into the function editor (replacing the code in the function):

    #r "Microsoft.WindowsAzure.Storage"
    
    using System;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    using Microsoft.Azure.NotificationHubs;
    using Newtonsoft.Json;
    
    public static async Task Run(UnityGameObject gameObj, CloudTable table, IAsyncCollector<Notification> notification, TraceWriter log)
    {
        //RowKey of the table object to be changed
        string rowKey = gameObj.RowKey;
    
        //Retrieve the table object by its RowKey
        TableOperation operation = TableOperation.Retrieve<UnityGameObject>("UnityPartitionKey", rowKey); 
    
        TableResult result = table.Execute(operation);
    
        //Create a UnityGameObject so to set its parameters
        UnityGameObject existingGameObj = (UnityGameObject)result.Result; 
    
        existingGameObj.RowKey = rowKey;
        existingGameObj.X = gameObj.X;
        existingGameObj.Y = gameObj.Y;
        existingGameObj.Z = gameObj.Z;
    
        //Replace the table appropriate table Entity with the value of the UnityGameObject
        operation = TableOperation.Replace(existingGameObj); 
    
        table.Execute(operation);
    
        log.Verbose($"Updated object position");
    
        //Serialize the UnityGameObject
        string wnsNotificationPayload = JsonConvert.SerializeObject(existingGameObj);
    
        log.Info($"{wnsNotificationPayload}");
    
        var headers = new Dictionary<string, string>();
    
        headers["X-WNS-Type"] = @"wns/raw";
    
        //Send the raw notification to subscribed devices
        await notification.AddAsync(new WindowsNotification(wnsNotificationPayload, headers)); 
    
        log.Verbose($"Sent notification");
    }
    
    // This UnityGameObject represent a Table Entity
    public class UnityGameObject : TableEntity
    {
        public string Type { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }
        public string RowKey { get; set; }
    }
    

    注意

    使用包含的程式庫,函式會接收在 Unity 場景中移動 (為 c # 物件的物件名稱和位置,稱為 UnityGameObject) 。Using the included libraries, the function receives the name and location of the object which was moved in the Unity scene (as a C# object, called UnityGameObject). 然後,這個物件會用來更新所建立資料表內的物件參數。This object is then used to update the object parameters within the created table. 之後,此函式會呼叫您所建立的通知中樞服務,以通知所有已訂閱的應用程式。Following this, the function makes a call to your created Notification Hub service, which notifies all subscribed applications.

  18. 在程式碼就緒後,按一下 [ 儲存]。With the code in place, click Save.

  19. 接著,按一下 < 頁面右手邊的 (箭號) 圖示。Next, click the < (arrow) icon, on the right-hand side of the page.

    開啟上傳面板

  20. 面板將會從右側滑入。A panel will slide in from the right. 在該面板中,按一下 [上傳],[檔案瀏覽器] 隨即出現。In that panel, click Upload, and a File Browser will appear.

  21. 流覽至您先前在 [記事本] 中建立的 project.json file,然後按一下 [開啟] 按鈕。Navigate to, and click, the project.json file, which you created in Notepad previously, and then click the Open button. 此檔案會定義您的函式將使用的程式庫。This file defines the libraries that your function will use.

    上傳 json

  22. 檔案上傳後,就會出現在右側面板中。When the file has uploaded, it will appear in the panel on the right. 按一下它會在 函數 編輯器中開啟它。Clicking it will open it within the Function editor. 它必須與下一個影像的外觀 完全 相同 (下一個步驟 23) 。It must look exactly the same as the next image (below step 23).

  23. 然後,在左側面板的 [函式] 底下, 按一下 [ 整合 ] 連結。Then, in the panel on the left, beneath Functions, click the Integrate link.

    整合函式

  24. 在下一個頁面的右上角,按一下 [ Advanced editor (,如下所示) 。On the next page, in the top right corner, click Advanced editor (as below).

    開啟進階編輯器

  25. 檔案 上的function.js 將會在中央面板中開啟,需要以下列程式碼片段取代。A function.json file will be opened in the center panel, which needs to be replaced with the following code snippet. 這會定義您正在建立的函式,以及傳遞至函式的參數。This defines the function you are building and the parameters passed into the function.

    {
    "bindings": [
        {
        "authLevel": "function",
        "type": "httpTrigger",
        "methods": [
            "get",
            "post"
        ],
        "name": "gameObj",
        "direction": "in"
        },
        {
        "type": "table",
        "name": "table",
        "tableName": "SceneObjectsTable",
        "connection": "mrnothubstorage_STORAGE",
        "direction": "in"
        },
        {
        "type": "notificationHub",
        "direction": "out",
        "name": "notification",
        "hubName": "MR_NotHub_ServiceInstance",
        "connection": "MRNotHubNS_DefaultFullSharedAccessSignature_NH",
        "platform": "wns"
        }
    ]
    }
    
  26. 您的編輯器現在看起來應該如下圖所示:Your editor should now look like the image below:

    回到標準編輯器

  27. 您可能會注意到您剛剛插入的輸入參數可能不符合您的資料表和儲存體詳細資料,因此需要使用您的資訊加以更新。You may notice the input parameters that you have just inserted might not match your table and storage details and therefore will need to be updated with your information. 請勿在 此進行,因為接下來會討論到。Do not do this here, as it is covered next. 只要按一下頁面右上角的 [ 標準編輯器 ] 連結,即可返回。Simply click the Standard editor link, in the top-right corner of the page, to go back.

  28. 回到 標準編輯器 中,按一下 [輸入] 下的 [ Azure 資料表儲存體 (資料表)Back in the Standard editor, click Azure Table Storage (table), under Inputs.

    資料表輸入

  29. 請確定下列各項 的資訊相符 ,因為它們可能會不同 () 下列步驟的映射:Ensure the following match to your information, as they may be different (there is an image below the following steps):

    1. 資料表名稱:您在 Azure 儲存體表服務內建立的資料表名稱。Table name: the name of the table you created within your Azure Storage, Tables service.

    2. 儲存體帳戶連接: 按一下 [ 新增],這會出現在下拉式功能表旁,而面板會顯示在視窗的右側。Storage account connection: click new, which appears alongside the dropdown menu, and a panel will appear to the right of the window.

      新儲存體

      1. 選取您先前建立用來裝載 函數應用程式儲存體帳戶Select your Storage Account, which you created previously to host the Function Apps.

      2. 您會發現已建立 儲存體帳戶 連接值。You will notice that the Storage Account connection value has been created.

      3. 完成之後,請務必按下 [ 儲存 ]。Make sure to press Save once you are done.

    3. [ 輸入 ] 頁面現在應該會顯示 的資訊,如下所示。The Inputs page should now match the below, showing your information.

      輸入完成

  30. 接著,按一下 [ Azure 通知中樞 (通知) -在 [ 輸出] 下。Next, click Azure Notification Hub (notification) - under Outputs. 請確認下列各項符合 的資訊,因為它們可能會不同 () 下列步驟的映射:Ensure the following are matched to your information, as they may be different (there is an image below the following steps):

    1. 通知中樞名稱:這是您先前建立的 通知中樞 服務實例名稱。Notification Hub Name: this is the name of your Notification Hub service instance, which you created previously.

    2. 通知中樞命名空間連接:按一下 [ 新增],這會出現在下拉式功能表旁。Notification Hubs namespace connection: click new, which appears alongside the dropdown menu.

      檢查輸出

    3. [連線] 快顯視窗會顯示 (請參閱下圖) ,您必須在此選取您先前設定的 通知中樞****命名空間The Connection popup will appear (see image below), where you need to select the Namespace of the Notification Hub, which you set up previously.

    4. 從中間的下拉式功能表中選取您的 通知中樞 名稱。Select your Notification Hub name from the middle dropdown menu.

    5. 將 [ 原則 ] 下拉式功能表設定為 [ >defaultfullsharedaccesssignature]。Set the Policy dropdown menu to DefaultFullSharedAccessSignature.

    6. 按一下 [ 選取 ] 按鈕返回。Click the Select button to go back.

      輸出更新

  31. 輸出 頁面現在應該會符合下列內容,但會改為使用 的資訊。The Outputs page should now match the below, but with your information instead. 請務必按下 [ 儲存]。Make sure to press Save.

警告

請勿直接編輯通知中樞名稱 (如此一來,如果您已正確遵循先前的步驟,就應該使用 進階編輯器 來完成此動作。Do not edit the Notification Hub name directly (this should all be done using the Advanced Editor, provided you followed the previous steps correctly.

輸出完成

  1. 此時,您應該測試函式,以確保其正常運作。At this point, you should test the function, to ensure it is working. 若要這樣做:To do this:

    1. 再次流覽至函式頁面:Navigate to the function page once more:

      輸出完成

    2. 回到 [函式] 頁面,按一下頁面最右邊的 [ 測試 ] 索引標籤,以開啟 [ 測試 ] 分頁:Back on the function page, click the Test tab on the far right side of the page, to open the Test blade:

      輸出完成

    3. 在分頁的 [ 要求主體 ] 文字方塊中,貼上下列程式碼:Within the Request body textbox of the blade, paste the below code:

      {  
          "Type":null,
          "X":3,
          "Y":0,
          "Z":1,
          "PartitionKey":null,
          "RowKey":"Obj2",
          "Timestamp":"0001-01-01T00:00:00+00:00",
          "ETag":null
      }
      
    4. 測試程式碼備妥後,按一下右下方的 [ 執行 ] 按鈕,將會執行測試。With the test code in place, click the Run button at the bottom right, and the test will be run. 測試的輸出記錄將會出現在您的函式程式碼下方的主控台區域中。The output logs of the test will appear in the console area, below your function code.

      輸出完成

    警告

    如果上述測試失敗,您必須再次確認您已遵循上述步驟,尤其是 整合面板 內的設定。If the above test fails, you will need to double check that you have followed the above steps exactly, particularly the settings within the integrate panel.

第7章-設定 Desktop Unity 專案Chapter 7 - Set up Desktop Unity Project

重要

您現在建立的桌面應用程式 將無法 在 Unity 編輯器中運作。The Desktop application which you are now creating, will not work in the Unity Editor. 您必須在應用程式建立之後,使用 Visual Studio (或部署的應用程式) ,在編輯器之外執行它。It needs to be run outside of the Editor, following the Building of the application, using Visual Studio (or the deployed application).

以下是使用 Unity 和混合式開發進行開發的一般設定,因此,它是適用于其他專案的絕佳範本。The following is a typical set up for developing with Unity and mixed reality, and as such, is a good template for other projects.

設定及測試您的混合現實沉浸式耳機。Set up and test your mixed reality immersive headset.

注意

在此課程中,您將 需要移動控制器。You will not require Motion Controllers for this course. 如果您需要設定沉浸式耳機的支援,請遵循此 連結,瞭解如何設定 Windows Mixed RealityIf you need support setting up the immersive headset, please follow this link on how to set up Windows Mixed Reality.

  1. 開啟 Unity ,然後按一下 [ 新增]。Open Unity and click New.

    新增 unity 專案

  2. 您必須提供 Unity 專案名稱,請插入 UnityDesktopNotifHubYou need to provide a Unity Project name, insert UnityDesktopNotifHub. 請確定專案類型設定為 3dMake sure the project type is set to 3D. 位置 設定為適合您 (記住,較接近根目錄的) 。Set the Location to somewhere appropriate for you (remember, closer to root directories is better). 然後,按一下 [ 建立專案]。Then, click Create project.

    建立專案

  3. 在 Unity 開啟的情況下,值得檢查預設 腳本編輯器 是否設定為 Visual StudioWith Unity open, it is worth checking the default Script Editor is set to Visual Studio. 移至 [編輯 > 喜好 設定],然後在新視窗中,流覽至 [外部工具]。Go to Edit > Preferences and then from the new window, navigate to External Tools. 外部腳本編輯器 變更為 Visual Studio 2017Change External Script Editor to Visual Studio 2017. 關閉 [ 喜好 設定] 視窗。Close the Preferences window.

    設定外部 VS 工具

  4. 接下來,移 至 [ 檔案 > 組建設定] 並選取 [通用 Windows 平臺],然後按一下 [切換平臺] 按鈕以套用您的選取專案。Next, go to File > Build Settings and select Universal Windows Platform, then click on the Switch Platform button to apply your selection.

    切換平臺

  5. 當您仍 在檔案 > 組建設定 中時,請確定:While still in File > Build Settings, make sure that:

    1. 目標裝置 已設定為 任何裝置Target Device is set to Any Device

      此應用程式將適用于您的桌面,所以必須是 任何裝置This Application will be for your desktop, so must be Any Device

    2. 組建類型 設定為 D3DBuild Type is set to D3D

    3. SDK 已設定為 最新安裝SDK is set to Latest installed

    4. Visual Studio 版本 設定為 最新安裝Visual Studio Version is set to Latest installed

    5. 組建並執行 設定為 本機電腦Build and Run is set to Local Machine

    6. 在這裡,您可以儲存場景,並將其新增至組建。While here, it is worth saving the scene, and adding it to the build.

      1. 若要這麼做,請選取 [ 新增開啟的場景]。Do this by selecting Add Open Scenes. [儲存] 視窗隨即出現。A save window will appear.

        新增開啟的場景

      2. 為此和任何未來的場景建立新的資料夾,然後選取 [ 新增資料夾 ] 按鈕,建立新的資料夾,將它命名為 場景Create a new folder for this, and any future, scene, then select the New folder button, to create a new folder, name it Scenes.

        新的場景資料夾

      3. 開啟新建立的 場景 資料夾,然後在 [ 檔案名: 文字] 欄位中輸入 NH _ Desktop _ 場景,再按 [ 儲存]。Open your newly created Scenes folder, and then in the File name: text field, type NH_Desktop_Scene, then press Save.

        新增 NH_Desktop_Scene

    7. [ 組建設定] 中的其餘設定,現在應該保持為預設值。The remaining settings, in Build Settings, should be left as default for now.

  6. 在相同的視窗中,按一下 [ 播放程式設定 ] 按鈕,這會開啟偵測 所在空間中的相關面板。In the same window click on the Player Settings button, this will open the related panel in the space where the Inspector is located.

  7. 在此面板中,需要驗證幾個設定:In this panel, a few settings need to be verified:

    1. 在 [ 其他設定 ] 索引標籤中:In the Other Settings tab:

      1. 腳本執行階段版本 應該是 實驗 ( .Net 4.6 對等)Scripting Runtime Version should be Experimental (.NET 4.6 Equivalent)

      2. 腳本後端 應該是 .netScripting Backend should be .NET

      3. API 相容性層級 應為 .net 4.6API Compatibility Level should be .NET 4.6

        4.6 net 版本

    2. 在 [ 發行設定 ] 索引標籤的 [ 功能] 下,選取:Within the Publishing Settings tab, under Capabilities, check:

      • InternetClientInternetClient

        刻度網際網路用戶端

  8. 回到 組建設定Unity C # 專案 不再呈現灰色; 勾選此方塊旁邊的核取方塊。Back in Build Settings Unity C# Projects is no longer greyed out; tick the checkbox next to this.

  9. 關閉 [組建設定] 視窗。Close the Build Settings window.

  10. 儲存場景和專案 > 儲存場景/ 檔案 > 儲存專案Save your Scene and Project File > Save Scene / File > Save Project.

    重要

    如果您想要略過此專案的 Unity 設定 元件 (傳統型應用程式) ,並直接繼續進行程式碼,請隨意 下載 unitypackage,並將其匯入您的專案中做為 自訂套件,然後繼續進行第 9 章If you wish to skip the Unity Set up component for this project (Desktop App), and continue straight into code, feel free to download this .unitypackage, import it into your project as a Custom Package, and then continue from Chapter 9. 您仍然需要新增腳本元件。You will still need to add the script components.

第8章-匯入 Unity 中的 DllChapter 8 - Importing the DLLs in Unity

您將使用適用于 Unity (的 Azure 儲存體,其本身會利用 .Net SDK for Azure) 。You will be using Azure Storage for Unity (which itself leverages the .Net SDK for Azure). 如需詳細資訊,請遵循此 連結,瞭解 Unity 的 Azure 儲存體For more information follow this link about Azure Storage for Unity.

Unity 中目前有已知的問題,需要在匯入之後重新設定外掛程式。There is currently a known issue in Unity which requires plugins to be reconfigured after import. 在解決 bug 之後,將不再需要這一節中的這些步驟 (4-7) 。These steps (4 - 7 in this section) will no longer be required after the bug has been resolved.

若要將 SDK 匯入您自己的專案中,請確定您已從 GitHub 下載最新的 unitypackageTo import the SDK into your own project, make sure you have downloaded the latest .unitypackage from GitHub. 然後執行下列動作:Then, do the following:

  1. 使用 [資產匯 > 入封裝 > 自訂套件] 功能表選項,將 unitypackage 新增至 Unity。Add the .unitypackage to Unity by using the Assets > Import Package > Custom Package menu option.

  2. 在快顯的 [匯 入 Unity 套件 ] 方塊中,您可以選取 [外掛程式 > * 存放裝置] 下的所有專案。In the Import Unity Package box that pops up, you can select everything under **Plugin > *Storage***. 取消核取其他所有專案,因為此課程不需要。Uncheck everything else, as it is not needed for this course.

    匯入至封裝

  3. 按一下 [匯 ] 按鈕,將專案新增至您的專案。Click the Import button to add the items to your project.

  4. 移至專案視圖中 [外掛程式] 下的 [儲存體] 資料夾,並 選取下列外掛程式:Go to the Storage folder under Plugins in the Project view and select the following plugins only:

    • Microsoft.Data.EdmMicrosoft.Data.Edm
    • Microsoft.Data.ODataMicrosoft.Data.OData
    • Microsoft.WindowsAzure.StorageMicrosoft.WindowsAzure.Storage
    • Newtonsoft.JsonNewtonsoft.Json
    • System.SpatialSystem.Spatial

取消核取任何平臺

  1. 選取 這些特定的外掛程式 之後, 取消 核取 任何平臺 ,然後 取消 核取 WSAPlayer然後按一下 [ 套用]。With these specific plugins selected, uncheck Any Platform and uncheck WSAPlayer then click Apply.

    套用平臺 dll

    注意

    我們會將這些特定外掛程式標示為僅用於 Unity 編輯器。We are marking these particular plugins to only be used in the Unity Editor. 這是因為在 [WSA] 資料夾中有不同版本的相同外掛程式,在從 Unity 匯出專案之後,將會使用這些相同的外掛程式。This is because there are different versions of the same plugins in the WSA folder that will be used after the project is exported from Unity.

  2. 在 [ 儲存體 外掛程式] 資料夾中,只選取:In the Storage plugin folder, select only:

    • Microsoft.Data.Services.ClientMicrosoft.Data.Services.Client

      set 不要處理 dll

  3. 勾選 [平臺設定] 下的 [不處理] 方塊,然後按一下 [ 套用]。Check the Don't Process box under Platform Settings and click Apply.

    套用無處理

    注意

    因為 Unity 元件 patcher 在處理此外掛程式時遇到困難,所以我們將此外掛程式標示為「不要處理」。We are marking this plugin "Don't process", because the Unity assembly patcher has difficulty processing this plugin. 雖然外掛程式不會進行處理,但仍可運作。The plugin will still work even though it is not processed.

第9章-在 Desktop Unity 專案中建立 TableToScene 類別Chapter 9 - Create the TableToScene class in the Desktop Unity project

您現在需要建立包含程式碼的腳本,以執行此應用程式。You now need to create the scripts containing the code to run this application.

您需要建立的第一個腳本是 TableToScene,其負責:The first script you need to create is TableToScene, which is responsible for:

  • 讀取 Azure 資料表中的實體。Reading entities within the Azure Table.
  • 使用資料表資料,判斷要產生的物件,以及在哪個位置。Using the Table data, determine which objects to spawn, and in which position.

您需要建立的第二個腳本是 CloudScene,其負責:The second script you need to create is CloudScene, which is responsible for:

  • 註冊滑鼠左鍵,讓使用者可以拖曳場景周圍的物件。Registering the left-click event, to allow the user to drag objects around the scene.
  • 從這個 Unity 場景序列化物件資料,並將其傳送至 Azure 函數應用程式。Serializing the object data from this Unity scene, and sending it to the Azure Function App.

若要建立此類別:To create this class:

  1. 以滑鼠右鍵按一下位於 [專案] 面板中的 [資產] 資料夾,然後 建立 > 資料夾Right-click in the Asset Folder located in the Project Panel, Create > Folder. 將資料夾命名為 腳本Name the folder Scripts.

    建立腳本資料夾

    建立腳本資料夾2

  2. 按兩下剛才建立的資料夾,將它開啟。Double click on the folder just created, to open it.

  3. 在 [腳本] 資料夾內按一下滑鼠右鍵,然後按一下 [建立 > c # 腳本]。Right-click inside the Scripts folder, click Create > C# Script. 將腳本命名為 TableToSceneName the script TableToScene.

    新的 c # 腳本  TableToScene 重新命名new c# script TableToScene rename

  4. 按兩下腳本,在 Visual Studio 2017 中開啟。Double-click on the script to open it in Visual Studio 2017.

  5. 新增下列命名空間:Add the following namespaces:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Table;
    using UnityEngine;
    
  6. 在類別內,插入下列變數:Within the class, insert the following variables:

        /// <summary>    
        /// allows this class to behave like a singleton
        /// </summary>    
        public static TableToScene instance;
    
        /// <summary>    
        /// Insert here you Azure Storage name     
        /// </summary>    
        private string accountName = " -- Insert your Azure Storage name -- ";
    
        /// <summary>    
        /// Insert here you Azure Storage key    
        /// </summary>    
        private string accountKey = " -- Insert your Azure Storage key -- ";
    

    注意

    在 Azure 入口網站中,以 Azure 儲存體服務中找到的金鑰值取代 accountName Azure 儲存體值和 accountKey 值, (參閱下圖) 。Substitute the accountName value with your Azure Storage Service name and accountKey value with the key value found in the Azure Storage Service, in the Azure Portal (See Image below).

    提取帳戶金鑰

  7. 現在新增 [ 開始 () ] 和 [ 喚醒] () 方法,以初始化類別。Now add the Start() and Awake() methods to initialize the class.

        /// <summary>
        /// Triggers before initialization
        /// </summary>
        void Awake()
        {
            // static instance of this class
            instance = this;
        }
    
        /// <summary>
        /// Use this for initialization
        /// </summary>
        void Start()
        {  
            // Call method to populate the scene with new objects as 
            // pecified in the Azure Table
            PopulateSceneFromTableAsync();
        }
    
  8. TableToScene 類別中,新增將從 Azure 資料表取出值的方法,並使用這些值在場景中產生適當的基本專案。Within the TableToScene class, add the method that will retrieve the values from the Azure Table and use them to spawn the appropriate primitives in the scene.

        /// <summary>    
        /// Populate the scene with new objects as specified in the Azure Table    
        /// </summary>    
        private async void PopulateSceneFromTableAsync()
        {
            // Obtain credentials for the Azure Storage
            StorageCredentials creds = new StorageCredentials(accountName, accountKey);
    
            // Storage account
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
    
            // Storage client
            CloudTableClient client = account.CreateCloudTableClient(); 
    
            // Table reference
            CloudTable table = client.GetTableReference("SceneObjectsTable");
    
            TableContinuationToken token = null;
    
            // Query the table for every existing Entity
            do
            {
                // Queries the whole table by breaking it into segments
                // (would happen only if the table had huge number of Entities)
                TableQuerySegment<AzureTableEntity> queryResult = await table.ExecuteQuerySegmentedAsync(new TableQuery<AzureTableEntity>(), token); 
    
                foreach (AzureTableEntity entity in queryResult.Results)
                {
                    GameObject newSceneGameObject = null;
                    Color newColor;
    
                    // check for the Entity Type and spawn in the scene the appropriate Primitive
                    switch (entity.Type)
                    {
                        case "Cube":
                            // Create a Cube in the scene
                            newSceneGameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
                            newColor = Color.blue;
                            break;
    
                        case "Sphere":
                            // Create a Sphere in the scene
                            newSceneGameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                            newColor = Color.red;
                            break;
    
                        case "Cylinder":
                            // Create a Cylinder in the scene
                            newSceneGameObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
                            newColor = Color.yellow;
                            break;
                        default:
                            newColor = Color.white;
                            break;
                    }
    
                    newSceneGameObject.name = entity.RowKey;
    
                    newSceneGameObject.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Diffuse"))
                    {
                        color = newColor
                    };
    
                    //check for the Entity X,Y,Z and move the Primitive at those coordinates
                    newSceneGameObject.transform.position = new Vector3((float)entity.X, (float)entity.Y, (float)entity.Z);
                }
    
                // if the token is null, it means there are no more segments left to query
                token = queryResult.ContinuationToken;
            }
    
            while (token != null);
        }
    
  9. TableToScene 類別之外,您必須定義應用程式用來將 資料表實體 序列化和還原序列化的類別。Outside the TableToScene class, you need to define the class used by the application to serialize and deserialize the Table Entities.

        /// <summary>
        /// This objects is used to serialize and deserialize the Azure Table Entity
        /// </summary>
        [System.Serializable]
        public class AzureTableEntity : TableEntity
        {
            public AzureTableEntity(string partitionKey, string rowKey)
                : base(partitionKey, rowKey) { }
    
            public AzureTableEntity() { }
            public string Type { get; set; }
            public double X { get; set; }
            public double Y { get; set; }
            public double Z { get; set; }
        }
    
  10. 請先確定您已 儲存 ,然後再返回 Unity 編輯器。Make sure you Save before going back to the Unity Editor.

  11. 按一下 [階層 ] 面板中 的 [主要攝影機],使其屬性出現在 [偵測 ] 中。Click the Main Camera from the Hierarchy panel, so that its properties appear in the Inspector.

  12. 開啟 [ 腳本 ] 資料夾,選取腳本 TableToScene 檔,並將它拖曳到 主要攝影機 上。With the Scripts folder open, select the script TableToScene file and drag it onto the Main Camera. 結果應如下所示:The result should be as below:

    將腳本新增至主要攝影機

第10章-在 Desktop Unity 專案中建立 CloudScene 類別Chapter 10 - Create the CloudScene class in the Desktop Unity Project

您需要建立的第二個腳本是 CloudScene,其負責:The second script you need to create is CloudScene, which is responsible for:

  • 註冊滑鼠左鍵,讓使用者可以拖曳場景周圍的物件。Registering the left-click event, to allow the user to drag objects around the scene.

  • 從這個 Unity 場景序列化物件資料,並將其傳送至 Azure 函數應用程式。Serializing the object data from this Unity scene, and sending it to the Azure Function App.

若要建立第二個腳本:To create the second script:

  1. 在 [ 腳本 ] 資料夾內按一下滑鼠右鍵,按一下 [ 建立]、[ C # 腳本]。Right-click inside the Scripts folder, click Create, C# Script. 將腳本命名為 CloudSceneName the script CloudScene

    新的 c # 腳本  重新命名 CloudScenenew c# script rename CloudScene

  2. 新增下列命名空間:Add the following namespaces:

    using Newtonsoft.Json;
    using System.Collections;
    using System.Text;
    using System.Threading.Tasks;
    using UnityEngine;
    using UnityEngine.Networking;
    
  3. 插入下列變數:Insert the following variables:

        /// <summary>
        /// Allows this class to behave like a singleton
        /// </summary>
        public static CloudScene instance;
    
        /// <summary>
        /// Insert here you Azure Function Url
        /// </summary>
        private string azureFunctionEndpoint = "--Insert here you Azure Function Endpoint--";
    
        /// <summary>
        /// Flag for object being moved
        /// </summary>
        private bool gameObjHasMoved;
    
        /// <summary>
        /// Transform of the object being dragged by the mouse
        /// </summary>
        private Transform gameObjHeld;
    
        /// <summary>
        /// Class hosted in the TableToScene script
        /// </summary>
        private AzureTableEntity azureTableEntity;
    
  4. 請在 azure 入口網站中的 azure 函式 App Service 中找到您的 Azure 函式應用程式 URL 來取代 azureFunctionEndpoint 值,如下圖所示:Substitute the azureFunctionEndpoint value with your Azure Function App URL found in the Azure Function App Service, in the Azure Portal, as shown in the image below:

    取得函式 URL

  5. 現在新增 [ 開始 () ] 和 [ 喚醒] () 方法,以初始化類別。Now add the Start() and Awake() methods to initialize the class.

        /// <summary>
        /// Triggers before initialization
        /// </summary>
        void Awake()
        {
            // static instance of this class
            instance = this;
        }
    
        /// <summary>
        /// Use this for initialization
        /// </summary>
        void Start()
        {
            // initialise an AzureTableEntity
            azureTableEntity = new AzureTableEntity();
        }
    
  6. Update () 方法中,新增下列程式碼,以偵測滑鼠輸入並拖曳,這會接著在場景中移動 gameobject。Within the Update() method, add the following code that will detect the mouse input and drag, which will in turn move GameObjects in the scene. 如果使用者已拖曳並卸載物件,則會將物件的名稱和座標傳遞給 UpdateCloudScene () 的方法,此方法會呼叫 Azure 函數應用程式服務,這會更新 azure 資料表並觸發通知。If the user has dragged and dropped an object, it will pass the name and coordinates of the object to the method UpdateCloudScene(), which will call the Azure Function App service, which will update the Azure table and trigger the notification.

        /// <summary>
        /// Update is called once per frame
        /// </summary>
        void Update()
        {
            //Enable Drag if button is held down
            if (Input.GetMouseButton(0))
            {
                // Get the mouse position
                Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
    
                Vector3 objPos = Camera.main.ScreenToWorldPoint(mousePosition);
    
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
                RaycastHit hit;
    
                // Raycast from the current mouse position to the object overlapped by the mouse
                if (Physics.Raycast(ray, out hit))
                {
                    // update the position of the object "hit" by the mouse
                    hit.transform.position = objPos;
    
                    gameObjHasMoved = true;
    
                    gameObjHeld = hit.transform;
                }
            }
    
            // check if the left button mouse is released while holding an object
            if (Input.GetMouseButtonUp(0) && gameObjHasMoved)
            {
                gameObjHasMoved = false;
    
                // Call the Azure Function that will update the appropriate Entity in the Azure Table
                // and send a Notification to all subscribed Apps
                Debug.Log("Calling Azure Function");
    
                StartCoroutine(UpdateCloudScene(gameObjHeld.name, gameObjHeld.position.x, gameObjHeld.position.y, gameObjHeld.position.z));
            }
        }
    
  7. 現在新增 UpdateCloudScene () 方法,如下所示:Now add the UpdateCloudScene() method, as below:

        private IEnumerator UpdateCloudScene(string objName, double xPos, double yPos, double zPos)
        {
            WWWForm form = new WWWForm();
    
            // set the properties of the AzureTableEntity
            azureTableEntity.RowKey = objName;
    
            azureTableEntity.X = xPos;
    
            azureTableEntity.Y = yPos;
    
            azureTableEntity.Z = zPos;
    
            // Serialize the AzureTableEntity object to be sent to Azure
            string jsonObject = JsonConvert.SerializeObject(azureTableEntity);
    
            using (UnityWebRequest www = UnityWebRequest.Post(azureFunctionEndpoint, jsonObject))
            {
                byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonObject);
    
                www.uploadHandler = new UploadHandlerRaw(jsonToSend);
    
                www.uploadHandler.contentType = "application/json";
    
                www.downloadHandler = new DownloadHandlerBuffer();
    
                www.SetRequestHeader("Content-Type", "application/json");
    
                yield return www.SendWebRequest();
    
                string response = www.responseCode.ToString();
            }
        }
    
  8. 儲存程式碼並返回 UnitySave the code and return to Unity

  9. CloudScene 腳本拖曳到 主要攝影機 上。Drag the CloudScene script onto the Main Camera.

    1. 按一下 [階層 ] 面板中 的 [主要攝影機],使其屬性出現在 [偵測 ] 中。Click the Main Camera from the Hierarchy panel, so that its properties appear in the Inspector.

    2. 開啟 [ 腳本 ] 資料夾,選取 CloudScene 腳本,並將它拖曳到 主要攝影機 上。With the Scripts folder open, select the CloudScene script and drag it onto the Main Camera. 結果應如下所示:The result should be as below:

      將雲端腳本拖曳至主要攝影機

第11章-將桌面專案建立到 UWPChapter 11 - Build the Desktop Project to UWP

現在已完成此專案的 Unity 區段所需的所有專案。Everything needed for the Unity section of this project has now been completed.

  1. 流覽至 組建設定 (檔案 > 組建設定) 。Navigate to Build Settings (File > Build Settings).

  2. 從 [ 組建設定 ] 視窗中,按一下 [ 建立]。From the Build Settings window, click Build.

    組建專案

  3. 檔案總管 視窗會快顯視窗,提示您輸入要建立的位置。A File Explorer window will popup, prompting you for a location to Build. 按一下左上角的 [ 新增資料夾 ],以建立新的資料夾 () ,並為其 建立 名稱。Create a new folder (by clicking New Folder in the top-left corner), and name it BUILDS.

    組建的新資料夾

    1. 開啟 [新 組建 ] 資料夾,然後再使用新) 資料夾 (建立另一個資料夾,並將它命名為 NH _ Desktop _ 應用程式Open the new BUILDS folder, and create another folder (using New Folder once more), and name it NH_Desktop_App.

      資料夾名稱 NH_Desktop_App

    2. 已選取 NH _ 桌面 _ 應用程式With the NH_Desktop_App selected. 按一下 [ 選取資料夾]。click Select Folder. 專案需要一分鐘的時間才能建立。The project will take a minute or so to build.

  4. 下列組建之後, 檔案總管 將會顯示新專案的位置。Following build, File Explorer will appear showing you the location of your new project. 但不需要開啟它,因為您需要先建立其他 Unity 專案,請前往接下來的幾章。No need to open it, though, as you need to create the other Unity project first, in the next few Chapters.

第12章-設定混合現實 Unity 專案Chapter 12 - Set up Mixed Reality Unity Project

以下是使用混合式開發進行開發的一般設定,因此,它是適用于其他專案的絕佳範本。The following is a typical set up for developing with the mixed reality, and as such, is a good template for other projects.

  1. 開啟 Unity ,然後按一下 [ 新增]。Open Unity and click New.

    新增 unity 專案

  2. 您現在將需要提供 Unity 專案名稱,請插入 UnityMRNotifHubYou will now need to provide a Unity Project name, insert UnityMRNotifHub. 請確定專案類型設定為 3dMake sure the project type is set to 3D. 位置 設定為適合您 (記住,較接近根目錄的) 。Set the Location to somewhere appropriate for you (remember, closer to root directories is better). 然後,按一下 [ 建立專案]。Then, click Create project.

    名稱 UnityMRNotifHub

  3. 在 Unity 開啟的情況下,值得檢查預設 腳本編輯器 是否設定為 Visual StudioWith Unity open, it is worth checking the default Script Editor is set to Visual Studio. 移至 [編輯 > 喜好 設定],然後在新視窗中,流覽至 [外部工具]。Go to Edit > Preferences and then from the new window, navigate to External Tools. 外部腳本編輯器 變更為 Visual Studio 2017Change External Script Editor to Visual Studio 2017. 關閉 [ 喜好 設定] 視窗。Close the Preferences window.

    將外部編輯器設定為 VS

  4. 接下來,移 至 [ 檔案 > 組建設定],然後按一下 [切換平臺] 按鈕,將平臺切換至 通用 Windows 平臺Next, go to File > Build Settings and switch the platform to Universal Windows Platform, by clicking on the Switch Platform button.

    將平臺切換至 UWP

  5. 移至 檔案 > 組建設定,並確定:Go to File > Build Settings and make sure that:

    1. 目標裝置 已設定為 任何裝置Target Device is set to Any Device

      針對 Microsoft HoloLens,請將 目標裝置 設定為 HoloLensFor the Microsoft HoloLens, set Target Device to HoloLens.

    2. 組建類型 設定為 D3DBuild Type is set to D3D

    3. SDK 已設定為 最新安裝SDK is set to Latest installed

    4. Visual Studio 版本 設定為 最新安裝Visual Studio Version is set to Latest installed

    5. 組建並執行 設定為 本機電腦Build and Run is set to Local Machine

    6. 在這裡,您可以儲存場景,並將其新增至組建。While here, it is worth saving the scene, and adding it to the build.

      1. 若要這麼做,請選取 [ 新增開啟的場景]。Do this by selecting Add Open Scenes. [儲存] 視窗隨即出現。A save window will appear.

        新增開啟的場景

      2. 為此和任何未來的場景建立新的資料夾,然後選取 [ 新增資料夾 ] 按鈕,建立新的資料夾,將它命名為 場景Create a new folder for this, and any future, scene, then select the New folder button, to create a new folder, name it Scenes.

        新的場景資料夾

      3. 開啟新建立的 場景 資料夾,然後在 [ 檔案名: 文字] 欄位中,輸入 NH _ MR _ 場景,然後按 [ 儲存]。Open your newly created Scenes folder, and then in the File name: text field, type NH_MR_Scene, then press Save.

        新增場景-NH_MR_Scene

    7. [ 組建設定] 中的其餘設定,現在應該保持為預設值。The remaining settings, in Build Settings, should be left as default for now.

  6. 在相同的視窗中,按一下 [ 播放程式設定 ] 按鈕,這會開啟偵測 所在空間中的相關面板。In the same window click on the Player Settings button, this will open the related panel in the space where the Inspector is located.

    開啟播放機設定

  7. 在此面板中,需要驗證幾個設定:In this panel, a few settings need to be verified:

    1. 在 [ 其他設定 ] 索引標籤中:In the Other Settings tab:

      1. 腳本執行階段版本 應該是 實驗 ( .net 4.6 對等) Scripting Runtime Version should be Experimental (.NET 4.6 Equivalent)

      2. 腳本後端 應該是 .netScripting Backend should be .NET

      3. API 相容性層級 應為 .net 4.6API Compatibility Level should be .NET 4.6

        api 相容性

    2. 在面板的 [ XR 設定] 中,在 [設定] (找到的 [發佈設定]) 、[支援的滴答 虛擬事實],確定已新增 Windows Mixed Reality SDKFurther down the panel, in XR Settings (found below Publish Settings), tick Virtual Reality Supported, make sure the Windows Mixed Reality SDK is added

      更新 xr 設定

    3. 在 [ 發行設定 ] 索引標籤的 [功能] 下,[ 功能] 下:Within the Publishing Settings tab, under Capabilities, heck:

      • InternetClientInternetClient

        刻度網際網路用戶端

  8. 回到 組建設定Unity c # 專案 不再呈現灰色:勾選這個旁邊的核取方塊。Back in Build Settings, Unity C# Projects is no longer greyed out: tick the checkbox next to this.

  9. 完成這些變更後,請關閉 [組建設定] 視窗。With these changes done, close the Build Settings window.

  10. 儲存場景和專案 > 儲存場景/ 檔案 > 儲存專案Save your Scene and Project File > Save Scene / File > Save Project.

    重要

    如果您想要略過此專案的 Unity 設定 元件 (混合現實應用程式) ,並直接繼續進行程式碼,請隨意 下載 unitypackage,並將它匯入至您的專案中做為 自訂套件,然後繼續進行第 14 章If you wish to skip the Unity Set up component for this project (mixed reality App), and continue straight into code, feel free to download this .unitypackage, import it into your project as a Custom Package, and then continue from Chapter 14. 您仍然需要新增腳本元件。You will still need to add the script components.

第13章-匯入混合現實 Unity 專案中的 DllChapter 13 - Importing the DLLs in the Mixed Reality Unity Project

您將使用適用于 Azure) 的 .Net SDK (Azure 儲存體 for Unity library。You will be using Azure Storage for Unity library (which uses the .Net SDK for Azure). 請遵循此 連結,以瞭解如何搭配使用 Azure 儲存體與 UnityPlease follow this link on how to use Azure Storage with Unity. Unity 中目前有已知的問題,需要在匯入之後重新設定外掛程式。There is currently a known issue in Unity which requires plugins to be reconfigured after import. 在解決 bug 之後,將不再需要這一節中的這些步驟 (4-7) 。These steps (4 - 7 in this section) will no longer be required after the bug has been resolved.

若要將 SDK 匯入您自己的專案中,請確定您已下載最新的 unitypackageTo import the SDK into your own project, make sure you have downloaded the latest .unitypackage. 然後執行下列動作:Then, do the following:

  1. 使用 [資產 匯 > 入封裝 > 自訂套件] 功能表選項,將您從上述下載的 unitypackage 新增至 Unity。Add the .unitypackage you downloaded from the above, to Unity by using the Assets > Import Package > Custom Package menu option.

  2. 在快顯的 [匯 入 Unity 套件] 方塊中,您可以選取 外掛程式 > 儲存體 下的所有專案。In the Import Unity Package box that pops up, you can select everything under Plugin > Storage.

    匯入套件

  3. 按一下 [匯 ] 按鈕,將專案新增至您的專案。Click the Import button to add the items to your project.

  4. 移至專案視圖中 [外掛程式] 下的 [儲存體] 資料夾,並 選取下列外掛程式:Go to the Storage folder under Plugins in the Project view and select the following plugins only:

    • Microsoft.Data.EdmMicrosoft.Data.Edm
    • Microsoft.Data.ODataMicrosoft.Data.OData
    • Microsoft.WindowsAzure.StorageMicrosoft.WindowsAzure.Storage
    • Newtonsoft.JsonNewtonsoft.Json
    • System.SpatialSystem.Spatial

    選取外掛程式

  5. 選取 這些特定的外掛程式 之後, 取消 核取 任何平臺 ,然後 取消 核取 WSAPlayer然後按一下 [ 套用]。With these specific plugins selected, uncheck Any Platform and uncheck WSAPlayer then click Apply.

    套用平臺變更

    注意

    您要將這些特定外掛程式標示為僅用於 Unity 編輯器。You are marking these particular plugins to only be used in the Unity Editor. 這是因為在 [WSA] 資料夾中有不同版本的相同外掛程式,在從 Unity 匯出專案之後,將會使用這些相同的外掛程式。This is because there are different versions of the same plugins in the WSA folder that will be used after the project is exported from Unity.

  6. 在 [ 儲存體 外掛程式] 資料夾中,只選取:In the Storage plugin folder, select only:

    • Microsoft.Data.Services.ClientMicrosoft.Data.Services.Client

      選取資料服務用戶端

  7. 勾選 [平臺設定] 下的 [不處理] 方塊,然後按一下 [ 套用]。Check the Don't Process box under Platform Settings and click Apply.

    不處理

    注意

    您正在將此外掛程式標示為「不要處理」,因為 Unity 元件 patcher 在處理此外掛程式時遇到困難。You are marking this plugin "Don't process" because the Unity assembly patcher has difficulty processing this plugin. 雖然外掛程式不會進行處理,但仍可運作。The plugin will still work even though it isn't processed.

第14章-在混合現實 Unity 專案中建立 TableToScene 類別Chapter 14 - Creating the TableToScene class in the mixed reality Unity project

TableToScene 類別與第 9 章所述的類別相同。The TableToScene class is identical to the one explained in Chapter 9. 遵循第 9 章所述的相同程式,在混合現實 Unity 專案中建立相同的類別。Create the same class in the mixed reality Unity Project following the same procedure explained in Chapter 9.

當您完成本章節之後,這兩個 Unity 專案 將會在主要攝影機上設定此類別。Once you have completed this Chapter, both of your Unity Projects will have this class set up on the Main Camera.

第15章-在混合現實 Unity 專案中建立 NotificationReceiver 類別Chapter 15 - Creating the NotificationReceiver class in the Mixed Reality Unity Project

您需要建立的第二個腳本是 NotificationReceiver,其負責:The second script you need to create is NotificationReceiver, which is responsible for:

  • 在初始化時向通知中樞註冊應用程式。Registering the app with the Notification Hub at initialization.
  • 接聽來自通知中樞的通知。Listening to notifications coming from the Notification Hub.
  • 將物件資料從收到的通知還原序列化。Deserializing the object data from received notifications.
  • 根據已還原序列化的資料,在場景中移動 Gameobject。Move the GameObjects in the scene, based on the deserialized data.

若要建立 NotificationReceiver 腳本:To create the NotificationReceiver script:

  1. 在 [ 腳本 ] 資料夾內按一下滑鼠右鍵,按一下 [ 建立]、[ C # 腳本]。Right-click inside the Scripts folder, click Create, C# Script. 將腳本命名為 NotificationReceiverName the script NotificationReceiver.

    建立新的 c # 腳本  名稱 NotificationReceivercreate new c# script name it NotificationReceiver

  2. 按兩下腳本將它開啟。Double click on the script to open it.

  3. 新增下列命名空間:Add the following namespaces:

    //using Microsoft.WindowsAzure.Messaging;
    using Newtonsoft.Json;
    using System;
    using System.Collections;
    using UnityEngine;
    
    #if UNITY_WSA_10_0 && !UNITY_EDITOR
    using Windows.Networking.PushNotifications;
    #endif
    
  4. 插入下列變數:Insert the following variables:

        /// <summary>
        /// allows this class to behave like a singleton
        /// </summary>
        public static NotificationReceiver instance;
    
        /// <summary>
        /// Value set by the notification, new object position
        /// </summary>
        Vector3 newObjPosition;
    
        /// <summary>
        /// Value set by the notification, object name
        /// </summary>
        string gameObjectName;
    
        /// <summary>
        /// Value set by the notification, new object position
        /// </summary>
        bool notifReceived;
    
        /// <summary>
        /// Insert here your Notification Hub Service name 
        /// </summary>
        private string hubName = " -- Insert the name of your service -- ";
    
        /// <summary>
        /// Insert here your Notification Hub Service "Listen endpoint"
        /// </summary>
        private string hubListenEndpoint = "-Insert your Notification Hub Service Listen endpoint-";
    
  5. hubName 值取代為您的通知中樞服務名稱,並使用 Azure 入口網站中 [存取原則] 索引標籤中的 [存取原則] 索引標籤、Azure 通知中樞服務中找到的端點值來取代 hubListenEndpoint 值 (請參閱) 下圖Substitute the hubName value with your Notification Hub Service name, and hubListenEndpoint value with the endpoint value found in the Access Policies tab, Azure Notification Hub Service, in the Azure Portal (see image below).

    插入通知中樞原則端點

  6. 現在新增 [ 開始 () ] 和 [ 喚醒] () 方法,以初始化類別。Now add the Start() and Awake() methods to initialize the class.

        /// <summary>
        /// Triggers before initialization
        /// </summary>
        void Awake()
        {
            // static instance of this class
            instance = this;
        }
    
        /// <summary>
        /// Use this for initialization
        /// </summary>
        void Start()
        {
            // Register the App at launch
            InitNotificationsAsync();
    
            // Begin listening for notifications
            StartCoroutine(WaitForNotification());
        }
    
  7. 新增 WaitForNotification 方法,以允許應用程式接收來自通知中樞程式庫的通知,而不需要與主執行緒衝突:Add the WaitForNotification method to allow the app to receive notifications from the Notification Hub Library without clashing with the Main Thread:

        /// <summary>
        /// This notification listener is necessary to avoid clashes 
        /// between the notification hub and the main thread   
        /// </summary>
        private IEnumerator WaitForNotification()
        {
            while (true)
            {
                // Checks for notifications each second
                yield return new WaitForSeconds(1f);
    
                if (notifReceived)
                {
                    // If a notification is arrived, moved the appropriate object to the new position
                    GameObject.Find(gameObjectName).transform.position = newObjPosition;
    
                    // Reset the flag
                    notifReceived = false;
                }
            }
        }
    
  8. InitNotificationAsync () 的下列方法會在初始化時向通知中樞服務註冊應用程式。The following method, InitNotificationAsync(), will register the application with the notification Hub Service at initialization. 因為 Unity 將無法建立專案,所以已將程式碼標記為批註。The code is commented out as Unity will not be able to Build the project. 當您在 Visual Studio 中匯入 Azure 訊息 Nuget 套件時,您將會移除批註。You will remove the comments when you import the Azure Messaging Nuget package in Visual Studio.

        /// <summary>
        /// Register this application to the Notification Hub Service
        /// </summary>
        private async void InitNotificationsAsync()
        {
            // PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    
            // NotificationHub hub = new NotificationHub(hubName, hubListenEndpoint);
    
            // Registration result = await hub.RegisterNativeAsync(channel.Uri);
    
            // If registration was successful, subscribe to Push Notifications
            // if (result.RegistrationId != null)
            // {
            //     Debug.Log($"Registration Successful: {result.RegistrationId}");
            //     channel.PushNotificationReceived += Channel_PushNotificationReceived;
            // }
        }
    
  9. 每次接收到通知時,將會觸發下列處理常式( Channel _ PushNotificationReceived ())。The following handler, Channel_PushNotificationReceived(), will be triggered every time a notification is received. 它會將通知還原序列化,這將會是已移至桌面應用程式的 Azure 資料表實體,然後將 MR 場景中對應的 GameObject 移至相同的位置。It will deserialize the notification, which will be the Azure Table Entity that has been moved on the Desktop Application, and then move the corresponding GameObject in the MR scene to the same position.

    重要

    程式碼會被標記為批註,因為程式碼會參考 Azure 訊息程式庫,而您將在 Visual Studio 中使用 Nuget 封裝管理員建立 Unity 專案之後,將會新增此程式碼。The code is commented out because the code references the Azure Messaging library, which you will add after building the Unity project using the Nuget Package Manager, within Visual Studio. 因此,除非您將其批註化,否則 Unity 專案將無法建立。請注意,如果您要建立專案,然後想要返回 Unity,您將需要 重新批註 該程式碼。As such, the Unity project will not be able to build, unless it is commented out. Be aware, that should you build your project, and then wish to return to Unity, you will need to re-comment that code.

        ///// <summary>
        ///// Handler called when a Push Notification is received
        ///// </summary>
        //private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)    
        //{
        //    Debug.Log("New Push Notification Received");
        //
        //    if (args.NotificationType == PushNotificationType.Raw)
        //    {
        //        //  Raw content of the Notification
        //        string jsonContent = args.RawNotification.Content;
        //
        //        // Deserialise the Raw content into an AzureTableEntity object
        //        AzureTableEntity ate = JsonConvert.DeserializeObject<AzureTableEntity>(jsonContent);
        //
        //        // The name of the Game Object to be moved
        //        gameObjectName = ate.RowKey;          
        //
        //        // The position where the Game Object has to be moved
        //        newObjPosition = new Vector3((float)ate.X, (float)ate.Y, (float)ate.Z);
        //
        //        // Flag thats a notification has been received
        //        notifReceived = true;
        //    }
        //}
    
  10. 請記得先儲存您的變更,再返回 Unity 編輯器。Remember to save your changes before going back to the Unity Editor.

  11. 按一下 [階層 ] 面板中 的 [主要攝影機],使其屬性出現在 [偵測 ] 中。Click the Main Camera from the Hierarchy panel, so that its properties appear in the Inspector.

  12. 開啟 [ 腳本 ] 資料夾,選取 NotificationReceiver 腳本,並將它拖曳到 主要攝影機 上。With the Scripts folder open, select the NotificationReceiver script and drag it onto the Main Camera. 結果應如下所示:The result should be as below:

    將通知接收器腳本拖曳至相機

    注意

    如果您正在針對 Microsoft HoloLens 進行開發,則必須更新 主要攝影機攝影機 元件,如此才能:If you are developing this for the Microsoft HoloLens, you will need to update the Main Camera's Camera component, so that:

    • 清除旗標:單色Clear Flags: Solid Color
    • 背景:黑色Background: Black

第16章-將 Mixed Reality 專案建立到 UWPChapter 16 - Build the Mixed Reality Project to UWP

這一章與先前專案的組建程式相同。This Chapter is identical to build process for the previous project. 此專案的 Unity 區段所需的所有專案現在都已完成,因此您可以從 Unity 建立它。Everything needed for the Unity section of this project has now been completed, so it is time to build it from Unity.

  1. 流覽至 組建設定 (檔案 > 組建設定) 。Navigate to Build Settings ( File > Build Settings ).

  2. 在 [ 組建設定 ] 功能表中,確定 [ Unity c # 專案] 是 [核取 (],這可讓您在組建) 之後,編輯此專案中的腳本。From the Build Settings menu, ensure Unity C# Projects* is ticked (which will allow you to edit the scripts in this project, after build).

  3. 完成之後,請按一下 [ 建立]。After this is done, click Build.

    組建專案

  4. 檔案總管 視窗會快顯視窗,提示您輸入要建立的位置。A File Explorer window will popup, prompting you for a location to Build. 按一下左上角的 [ 新增資料夾 ],以建立新的資料夾 () ,並為其 建立 名稱。Create a new folder (by clicking New Folder in the top-left corner), and name it BUILDS.

    建立組建資料夾

    1. 開啟 [新 組建 ] 資料夾,然後再使用新) 資料夾 (建立另一個資料夾,並將它命名為 NH _ MR _ 應用程式Open the new BUILDS folder, and create another folder (using New Folder once more), and name it NH_MR_App.

      建立 NH_MR_Apps 資料夾

    2. 已選取 NH _ MR _ 應用程式With the NH_MR_App selected. 按一下 [ 選取資料夾]。click Select Folder. 專案需要一分鐘的時間才能建立。The project will take a minute or so to build.

  5. 在組建之後, 檔案總管 視窗將會在新專案的位置開啟。Following the build, a File Explorer window will open at the location of your new project.

第17章-將 NuGet 套件新增至 UnityMRNotifHub 方案Chapter 17 - Add NuGet packages to the UnityMRNotifHub Solution

警告

請記住,一旦您將下列 NuGet 套件新增 (並取消批註下一 中的程式碼) ,在 Unity 專案中重新開啟程式碼時,就會出現錯誤。Please remember that, once you add the following NuGet Packages (and uncomment the code in the next Chapter), the Code, when reopened within the Unity Project, will present errors. 如果您想要返回並繼續在 Unity 編輯器中編輯,您將需要 errosome 程式碼的批註,然後在您回到 Visual Studio 之後,稍後再取消批註。If you wish to go back and continue editing in the Unity Editor, you will need comment that errosome code, and then uncomment again later, once you are back in Visual Studio.

完成混合的事實組建之後,請流覽至您所建立的 mixed reality 專案,然後按兩下該資料夾內的方案 ( .sln) 檔案,以 Visual Studio 2017 開啟您的方案。Once the mixed reality build has been completed, navigate to the mixed reality project, which you built, and double click on the solution (.sln) file within that folder, to open your solution with Visual Studio 2017. 您現在需要新增 WindowsAzure、managed NuGet 套件;這是用來從通知中樞接收通知的程式庫。You will now need to add the WindowsAzure.Messaging.managed NuGet package; this is a library that is used to receive Notifications from the Notification Hub.

若要匯入 NuGet 套件:To import the NuGet package:

  1. 方案總管 中,以滑鼠右鍵按一下您的方案In the Solution Explorer, right click on your Solution

  2. 按一下 [ 管理 NuGet 套件]。Click on Manage NuGet Packages.

    開啟 nuget 管理員

  3. 選取 [流覽] 索引標籤,然後搜尋 _ * WindowsAzure. managed * *。Select the *Browse _ tab and search for _*WindowsAzure.Messaging.managed**.

    尋找 windows azure 訊息套件

  4. 選取 [結果] (如下所示) ,然後在右邊的視窗中,選取 [ 專案] 旁的核取方塊。Select the result (as shown below), and in the window to the right, select the checkbox next to Project. 這會在 [ 專案] 旁的核取方塊中放置一個刻度,並在 [ 元件-CSharp ] 和 [ UnityMRNotifHub ] 專案旁邊加上核取方塊。This will place a tick in the checkbox next to Project, along with the checkbox next to the Assembly-CSharp and UnityMRNotifHub project.

    勾選所有專案

  5. 最初提供的版本 可能 與此專案不相容。The version initially provided may not be compatible with this project. 因此,請按一下 [ 版本] 旁的下拉式功能表,然後按一下 [ 版本 0.1.7.9],再按一下 [ 安裝]。Therefore, click on the dropdown menu next to Version, and click Version 0.1.7.9, then click Install.

  6. 您現在已完成安裝 NuGet 套件。You have now finished installing the NuGet package. 尋找您在 NotificationReceiver 類別中輸入的批註程式碼,並移除批註。Find the commented code you entered in the NotificationReceiver class and remove the comments..

第18章-編輯 UnityMRNotifHub 應用程式,NotificationReceiver 類別Chapter 18 - Edit UnityMRNotifHub application, NotificationReceiver class

新增 NuGet 封裝 之後,您必須將 NotificationReceiver 類別內的一些程式碼 取消 批註。Following having added the NuGet Packages, you will need to uncomment some of the code within the NotificationReceiver class.

這包括:This includes:

  1. 位於頂端的命名空間:The namespace at the top:

    using Microsoft.WindowsAzure.Messaging;
    
  2. >initnotificationsasync () 方法內的所有程式碼:All the code within the InitNotificationsAsync() method:

        /// <summary>
        /// Register this application to the Notification Hub Service
        /// </summary>
        private async void InitNotificationsAsync()
        {
            PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    
            NotificationHub hub = new NotificationHub(hubName, hubListenEndpoint);
    
            Registration result = await hub.RegisterNativeAsync(channel.Uri);
    
            // If registration was successful, subscribe to Push Notifications
            if (result.RegistrationId != null)
            {
                Debug.Log($"Registration Successful: {result.RegistrationId}");
                channel.PushNotificationReceived += Channel_PushNotificationReceived;
            }
        }
    

警告

上述程式碼在其中有一個批註:請確定您不會不慎 取消批註 該批註 (因為如果您有! ) ,程式碼將不會進行編譯。The code above has a comment in it: ensure that you have not accidentally uncommented that comment (as the code will not compile if you have!).

  1. 最後, Channel_PushNotificationReceived 的事件:And, lastly, the Channel_PushNotificationReceived event:

        /// <summary>
        /// Handler called when a Push Notification is received
        /// </summary>
        private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
        {
            Debug.Log("New Push Notification Received");
    
            if (args.NotificationType == PushNotificationType.Raw)
            {
                //  Raw content of the Notification
                string jsonContent = args.RawNotification.Content;
    
                // Deserialize the Raw content into an AzureTableEntity object
                AzureTableEntity ate = JsonConvert.DeserializeObject<AzureTableEntity>(jsonContent);
    
                // The name of the Game Object to be moved
                gameObjectName = ate.RowKey;
    
                // The position where the Game Object has to be moved
                newObjPosition = new Vector3((float)ate.X, (float)ate.Y, (float)ate.Z);
    
                // Flag thats a notification has been received
                notifReceived = true;
            }
        }
    

在這些取消批註中,請確定您已儲存,然後繼續進行下一章。With these uncommented, ensure that you save, and then proceed to the next Chapter.

第19章-將 mixed reality 專案關聯至 Store 應用程式Chapter 19 - Associate the mixed reality project to the Store app

您現在需要將 mixed reality 專案與您在實驗室開始時建立的 Store 應用程式產生關聯。You now need to associate the mixed reality project to the Store App you created in at the start of the lab.

  1. 開啟解決方案。Open the solution.

  2. 以滑鼠右鍵按一下 [方案總管面板中的 UWP 應用程式專案,移至 [ 儲存],然後 將應用程式與存放區建立關聯...。Right click on the UWP app Project in the Solution Explorer panel, the go to Store, and Associate App with the Store....

    開啟存放區關聯

  3. 將會出現新的視窗 ,稱為將您的應用程式與 Windows Store 產生關聯A new window will appear called Associate Your App with the Windows Store. 按一下 [下一步] 。Click Next.

    移至下一個畫面

  4. 它會載入與您已登入的帳戶相關聯的所有應用程式。It will load up all the Applications associated with the Account which you have logged in. 如果您尚未登入您的帳戶,您可以 登入 此頁面。If you are not logged in to your account, you can Log In on this page.

  5. 尋找您在本教學課程開始時所建立的 商店應用程式名稱 ,然後選取它。Find the Store App name that you created at the start of this tutorial and select it. 然後按一下 [下一步] 。Then click Next.

    尋找並選取您的商店名稱

  6. 按一下 [關聯]。Click Associate.

    建立應用程式的關聯

  7. 您的應用程式現在已與 Store 應用程式建立 關聯Your App is now Associated with the Store App. 這是啟用通知的必要動作。This is necessary for enabling Notifications.

第20章-部署 UnityMRNotifHub 和 UnityDesktopNotifHub 應用程式Chapter 20 - Deploy UnityMRNotifHub and UnityDesktopNotifHub applications

這一章可能會讓兩人更容易使用,因為結果會包括執行的應用程式、電腦桌面上執行的應用程式,以及您的沉浸式耳機內的應用程式。This Chapter may be easier with two people, as the result will include both apps running, one running on your computer Desktop, and the other within your immersive headset.

沉浸式耳機應用程式正在等候接收 (位置變更) 本機 Gameobject 的場景變更,而桌面應用程式將會變更其本機場景 (位置變更) ,這將會與 MR 應用程式共用。The immersive headset app is waiting to receive changes to the scene (position changes of the local GameObjects), and the Desktop app will be making changes to their local scene (position changes), which will be shared to the MR app. 先部署 MR 應用程式,然後再部署傳統型應用程式,讓接收者可以開始接聽,是合理的。It makes sense to deploy the MR app first, followed by the Desktop app, so that the receiver can begin listening.

若要在本機電腦上部署 UnityMRNotifHub 應用程式:To deploy the UnityMRNotifHub app on your Local Machine:

  1. Visual Studio 2017 中開啟 UnityMRNotifHub 應用程式的解決方案檔案。Open the solution file of your UnityMRNotifHub app in Visual Studio 2017.

  2. 在 [ 方案平臺] 中,選取 [ x86]、[本機電腦]。In the Solution Platform, select x86, Local Machine.

  3. 在 [ 方案 設定] 中選取 [ Debug]。In the Solution Configuration select Debug.

    設定專案設定

  4. 移至 [ 組建] 功能表 ,然後按一下 [ 部署方案 ],將應用程式側載至您的電腦。Go to Build menu and click on Deploy Solution to sideload the application to your machine.

  5. 您的應用程式現在應該會出現在已安裝的應用程式清單中,準備好要啟動。Your App should now appear in the list of installed apps, ready to be launched.

若要在本機電腦上部署 UnityDesktopNotifHub 應用程式:To deploy the UnityDesktopNotifHub app on Local Machine:

  1. Visual Studio 2017 中開啟 UnityDesktopNotifHub 應用程式的解決方案檔案。Open the solution file of your UnityDesktopNotifHub app in Visual Studio 2017.

  2. 在 [ 方案平臺] 中,選取 [ x86]、[本機電腦]。In the Solution Platform, select x86, Local Machine.

  3. 在 [ 方案 設定] 中選取 [ Debug]。In the Solution Configuration select Debug.

    設定專案設定

  4. 移至 [ 組建] 功能表 ,然後按一下 [ 部署方案 ],將應用程式側載至您的電腦。Go to Build menu and click on Deploy Solution to sideload the application to your machine.

  5. 您的應用程式現在應該會出現在已安裝的應用程式清單中,準備好要啟動。Your App should now appear in the list of installed apps, ready to be launched.

  6. 啟動混合現實應用程式,然後啟動桌面應用程式。Launch the mixed reality application, followed by the Desktop application.

執行這兩個應用程式時,請使用滑鼠左鍵) ,在桌面場景 (中移動物件。With both applications running, move an object in the desktop scene (using the Left Mouse Button). 這些位置變更將會在本機進行序列化,並傳送至函數應用程式服務。These positional changes will be made locally, serialized, and sent to the Function App service. 函數應用程式服務接著會更新該資料表以及通知中樞。The Function App service will then update the Table along with the Notification Hub. 收到更新後,通知中樞會將更新的資料直接傳送到所有已註冊的應用程式 (在本例中為「沉浸式耳機」應用程式) ,然後將傳入的資料還原序列化,並將新的位置資料套用至本機物件,並在場景中移動它們。Having received an update, the Notification Hub will send the updated data directly to all the registered applications (in this case the immersive headset app), which will then deserialize the incoming data, and apply the new positional data to the local objects, moving them in scene.

您已完成 Azure 通知中樞應用程式Your finished your Azure Notification Hubs application

恭喜,您已建立混合的現實應用程式,以利用 Azure 通知中樞服務,並允許應用程式之間的通訊。Congratulations, you built a mixed reality app that leverages the Azure Notification Hubs Service and allow communication between apps.

最終產品-結束

額外練習Bonus exercises

練習1Exercise 1

您可以瞭解如何變更 Gameobject 的色彩,並將該通知傳送至其他應用程式來查看場景?Can you work out how to change the color of the GameObjects and send that notification to other apps viewing the scene?

練習2Exercise 2

您是否可以將 Gameobject 移動到您的 MR 應用程式,並在您的桌面應用程式中查看更新的場景?Can you add movement of the GameObjects to your MR app and see the updated scene in your desktop app?