9.使用語音命令9. Using speech commands

在本教學課程中,您將了解如何建立語音命令,以及如何全域性地控制這些命令。In this tutorial, you will learn how to create speech commands and how to control them globally. 您也將了解如何控制局部語音命令,該命令會要求使用者看著控制語音命令的物件。You will also learn how to control local speech commands that require the user to look at the object that controls the speech command.

目標Objectives

  • 了解如何建立語音命令Learn how to create speech commands
  • 了解如何全域性和局部性地控制語音命令Learn how to control speech commands globally and locally

確定已啟用麥克風功能Ensuring the Microphone capability is enabled

在 Unity 功能表中,選取 [混合實境工具組] > [公用程式] > [設定 Unity 專案] 以開啟 [MRTK 專案設定程式] 視窗,然後在 [UWP 功能] 區段中,確認 [啟用麥克風功能] 呈現灰色:In the Unity menu, select Mixed Reality Toolkit > Utilities > Configure Unity Project to open the MRTK Project Configurator window, then in the UWP Capabilities section, verify that Enable Microphone Capability is greyed out:

啟用麥克風功能

注意

當您在本教學課程系列開頭設定 Unity 時,「麥克風」功能應該在套用 MRTK 專案設定程式設定指示期間啟用。The Microphone capability should have been enabled during the Apply the MRTK Project Configurator settings instructions when you configured the Unity project at the beginning of this tutorial series. 不過如果未啟用,請務必立即啟用。However, if it is not enabled, make sure you enable it now.

建立語音命令Creating speech commands

在 [階層] 視窗中,選取 MixedRealityToolkit 物件,然後在 [偵測器] 視窗中,選取 [MixedRealityToolkit] > [輸入] 索引標籤,並採取下列步驟:In the Hierarchy window, select the MixedRealityToolkit object, then in the Inspector window, select the MixedRealityToolkit > Input tab and take the following steps:

  • 展開 [語音] 區段Expand the Speech section
  • 複製 DefaultMixedRealitySpeechCommandsProfile 並為其指定適當的名稱,例如 GettingStarted_MixedRealitySpeechCommandsProfileClone the DefaultMixedRealitySpeechCommandsProfile and give it a suitable name, for example, GettingStarted_MixedRealitySpeechCommandsProfile
  • 確認 [啟動行為] 已設定為 [自動啟動]Verify that Start Behaviour is set to Auto Start

建立語音命令

提示

如需有關如何複製 MRTK 設定檔的提示,您可以參閱設定 MRTK 設定檔的指示。For a reminder on how to clone MRTK profiles, you can refer to the Configuring the MRTK profiles instructions.

在 [語音] > [語音命令] 區段中,按四次 [+ 新增語音命令] 按鈕,將四個新的語音命令新增至現有語音命令的清單中,然後在 [關鍵字] 欄位中輸入下列片語:In the Speech > Speech Commands section, click the + Add a New Speech Command button four times to add four new speech commands to the list of the existing speech commands, then in the Keyword fields enter the following phrases:

  • 啟用指標Enable Indicator
  • 啟用點選放置Enable Tap to Place
  • 啟用週框方塊Enable Bounding Box
  • 停用週框方塊Disable Bounding Box

新增語音命令

提示

如果您的電腦沒有麥克風,您可以將 KeyCode 指派給語音命令,讓您在按下對應的按鍵時觸發語音命令。If your computer does not have a microphone you can assign a KeyCode to the speech commands, which will let you trigger them when the corresponding key is pressed.

控制語音命令Controlling speech commands

在 [專案] 視窗中,瀏覽至 [資產] > [MRTK] > [SDK] > [功能] > [UX] > [Prefabs] > [ToolTip] 資料夾找出工具提示 Prefab:In the Project window, navigate to the Assets > MRTK > SDK > Features > UX > Prefabs > ToolTip folder to locate the tooltip prefabs:

開啟工具提示資料夾

在 [階層] 視窗中,以滑鼠右鍵按一下空的位置,然後選取 [建立空物件],將空的物件新增至您的場景。In the Hierarchy window, right-click on an empty spot and select Create Empty to add an empty object to your scene.

將物件命名為 SpeechInputHandler_Global,然後在 [偵測器] 視窗中,使用 [新增元件] 按鈕來新增 SpeechInputHandler 元件並進行下列設定:Name the object SpeechInputHandler_Global, then in the Inspector window, use the Add Component button to add the SpeechInputHandler component and configure it as follows:

  • 取消核取 [需要對焦] 核取方塊,如此使用者就不需要看著具有 SpeechInputHandler 元件的物件來觸發語音命令Uncheck the Is Focus Required checkbox, so the user is not required to look at the object with the SpeechInputHandler component to trigger the speech command
  • 從 [專案] 視窗中,將 SpeechConfirmation Tooltip Prefab 指派給 [語音確認工具提示 Prefab] 欄位,以在辨識到語音命令時顯示此 PrefabFrom the Project window, assign the SpeechConfirmation Tooltip prefab to the Speech Confirmation Tooltip Prefab field, to have this prefab appear when a speech command is recognized

設定語音輸入處理常式元件

在 SpeechInputHandler 元件上,按三次小型 + 圖示,以新增三個關鍵字元素:On the SpeechInputHandler component, click the small + icon three times to add three keyword elements:

將關鍵字元素新增至語音輸入處理常式

展開 元素 0 並進行下列設定:Expand Element 0 and configure it as follows:

  • 在 [關鍵字] 欄位中輸入 啟用指標,以參考您在上一節中建立的啟用指標語音命令。In the Keyword field, enter Enable Indicator, to reference the Enable Indicator speech command you created in the previous section
  • 按一下小型 + 圖示,以新增事件Click the small + icon to add an event
  • 從 [階層] 視窗中,將 [指標] 物件指派給 [無 (物件)] 欄位From the Hierarchy window, assign the Indicator object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [GameObject] > [SetActive (bool)],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select GameObject > SetActive (bool) to set this function as the action to be executed when the event is triggered
  • 核取引數核取方塊,讓其呈現 已核取 狀態Check the argument checkbox, so it is checked

設定關鍵字元素 0

展開 元素 1 並進行下列設定:Expand Element 1 and configure it as follows:

  • 在 [關鍵字] 欄位中輸入 啟用週邊方塊,以參考您在上一節中建立的啟用週邊方塊命令。In the Keyword field, enter Enable Bounding Box, to reference the Enable Bounding Box command you created in the previous section
  • 按一下小型 + 圖示,以新增事件Click the small + icon to add an event
  • 從 [階層] 視窗中,將 [RoverExplorer] 物件指派給 [無 (物件)] 欄位From the Hierarchy window, assign the RoverExplorer object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [BoundingBox] > [bool enabled] 以在觸發事件時更新此屬性值From the No Function dropdown, select BoundingBox > bool enabled to update this property value when the event is triggered
  • 核取引數核取方塊,讓其呈現 已核取 狀態Check the argument checkbox, so it is checked
  • 按一下小型 + 圖示,以新增另一個事件Click the small + icon to add another event
  • 從 [階層] 視窗中,將 [RoverExplorer] 物件指派給 [無 (物件)] 欄位From the Hierarchy window, assign the RoverExplorer object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [ObjectManipulator] > [bool enabled] 以在觸發事件時更新此屬性值From the No Function dropdown, select ObjectManipulator > bool enabled to update this property value when the event is triggered
  • 核取引數核取方塊,讓其呈現 已核取 狀態Check the argument checkbox, so it is checked

設定關鍵字元素 1

展開 元素 2 並進行下列設定:Expand Element 2 and configure it as follows:

  • 在 [關鍵字] 欄位中輸入 停用週邊方塊,以參考您在上一節中建立的停用週邊方塊命令In the Keyword field, enter Disable Bounding Box, to reference the Disable Bounding Box command you created in the previous section
  • 按一下小型 + 圖示,以新增事件Click the small + icon to add an event
  • 從 [階層] 視窗中,將 [RoverExplorer] 物件指派給 [無 (物件)] 欄位From the Hierarchy window, assign the RoverExplorer object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [BoundingBox] > [bool enabled] 以在觸發事件時更新此屬性值From the No Function dropdown, select BoundingBox > bool enabled to update this property value when the event is triggered
  • 確認 未核取 引數核取方塊Verify that the argument checkbox is unchecked
  • 按一下小型 + 圖示,以新增另一個事件Click the small + icon to add another event
  • 從 [階層] 視窗中,將 [RoverExplorer] 物件指派給 [無 (物件)] 欄位From the Hierarchy window, assign the RoverExplorer object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [ObjectManipulator] > [bool enabled] 以在觸發事件時更新此屬性值From the No Function dropdown, select ObjectManipulator > bool enabled to update this property value when the event is triggered
  • 確認 未核取 引數核取方塊Verify that the argument checkbox is unchecked

設定關鍵字元素 2

在 [階層] 視窗中選取 [RoverExplorer] > RoverAssembly 物件,然後在 [偵測器] 視窗中使用 [新增元件] 按鈕來新增 SpeechInputHandler 元件,並進行以下設定:In the Hierarchy window, select the RoverExplorer > RoverAssembly object, then in the Inspector window, use the Add Component button to add the SpeechInputHandler component and configure it as follows:

  • 確認 已核取 [需要對焦] 核取方塊,如此使用者就必須看著具有 SpeechInputHandler 元件的物件 (也就是 RoverAssembly) 來觸發語音命令Verify that the Is Focus Required checkbox is check, so the user is required to look at the object with the SpeechInputHandler component, i.e., the RoverAssembly, to trigger the speech command
  • 從 [專案] 視窗中,將 SpeechConfirmation Tooltip Prefab 指派給 [語音確認工具提示 Prefab] 欄位,以在辨識到語音命令時顯示此 PrefabFrom the Project window, assign the SpeechConfirmation Tooltip prefab to the Speech Confirmation Tooltip Prefab field, to have this prefab appear when a speech command is recognized

將語音輸入處理常式新增至 Rover 組件

在 SpeechInputHandler 元件上,按一下小型 + 圖示以新增關鍵字元素,然後展開新建立的元素,並進行下列設定:On the SpeechInputHandler component, click the small + icon to add a keyword element, expand the newly created element, then configure it as follows:

  • 在 [關鍵字] 欄位中輸入 啟用點選放置,以參考您在上一節中建立的點選放置命令In the Keyword field, enter Enable Tap to Place, to reference the Enable Tap to Place command you created in the previous section
  • 按一下小型 + 圖示,以新增事件Click the small + icon to add an event
  • 從 [階層] 視窗中,將物件本身 (也就是相同的 RoverAssembly 物件) 指派給 [無 (物件)] 欄位From the Hierarchy window, assign the object itself, i.e., the same RoverAssembly object, to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [TapToPlace] > [bool enabled] 以在觸發事件時更新此屬性值From the No Function dropdown, select TapToPlace > bool enabled to update this property value when the event is triggered
  • 核取引數核取方塊,讓其呈現 已核取 狀態Check the argument checkbox, so it is checked

在 Rover 組件上設定語音輸入處理常式

恭喜!Congratulations

在本教學課程中,您已了解如何建立語音命令,以及如何全域性地控制這些命令。In this tutorial, you learned how to create speech commands and how to control them globally. 您也已了解如何控制局部語音命令,該命令會要求使用者看著控制語音命令的物件。You also learned how to control local speech commands that require the user to look at the object that controls the speech command.

這同時也會結束入門教學課程系列。This also concludes the Getting started tutorials series. 透過這些教學課程,您已成功地使用 MRTK 從頭開始建立完整的混合實境體驗。Through these tutorials, you have successfully built a complete mixed reality experience from scratch using the MRTK.

在接下來的兩個教學課程系列:Azure Spatial Anchors 教學課程多使用者功能教學課程中,您會先了解如何將 Azure Spatial Anchors 整合到您的專案中,以在現實世界中錨定您建立的 Rover Explorer 體驗。In the next two tutorial series, Azure Spatial Anchors tutorials and Multi-user capabilities tutorials, you will first learn how to integrate Azure Spatial Anchors into your project to anchor the Rover Explorer experience you created in the real world. 然後,您將了解如何在專案中新增多使用者功能,以即時分享使用者和物件的動作。Then, you will learn how to add multi-user capabilities to your project to share user and object movements in real-time.

下一個開發檢查點Next Development Checkpoint

依循我們配置的 Unity 開發檢查點旅程,您的下一個工作將是熟悉混合實境應用程式的核心建置組塊。If you're following the Unity development checkpoint journey we've laid out, your next task is to familiarize yourself with core building blocks of Mixed Reality apps.

您可以隨時回到 Unity 開發檢查點You can always go back to the Unity development checkpoints at any time.