4.將物件置放在場景中4. Positioning objects in the scene

概觀Overview

在本教學課程中,您將匯入教學課程資產,並將所提供的物件放置在場景中。In this tutorial, you will import the tutorial assets and position the provided objects in the scene.

目標Objectives

  • 了解如何將物件放置在場景中Learn how to position objects in the scene
  • 了解如何使用 MRTK 的網格物件集合功能Learn how to use MRTK's Grid Object Collection feature

匯入教學課程資產Importing the tutorial assets

下載並匯入下列 Unity 自訂套件:Download and import the following Unity custom package:

匯入教學課程資產之後,您的專案視窗看起來應該會像這樣:After you have imported the tutorial assets your Project window should look similar to this:

匯入教學課程資產後的 Unity 階層、場景和專案視窗

提示

如需有關如何匯入 Unity 自訂套件的提示,您可以參閱匯入 MRTK 的指示。For a reminder on how to import a Unity custom package, you can refer to the Importing the MRTK instructions.

建立父物件Creating the parent object

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

Unity [建立空物件] 內容相關快顯功能表

提示

若要並排顯示場景和遊戲視窗 (如上圖所示),將遊戲視窗拖曳至場景視窗的右邊即可。To display your Scene and Game window side by side as shown in the image above, drag the Game window to the right side of the Scene window. 若要深入了解如何自訂您的工作區,您可以參閱 Unity 的自訂您的工作區文件。To learn more about customizing your workspace, you can refer to Unity's Customizing Your Workspace documentation.

以滑鼠右鍵按一下新建立的物件並選取 [重新命名],然後將名稱變更為 RoverExplorerRight-click on the newly created object, select Rename, and change the name to RoverExplorer:

Unity [重新命名] 內容相關快顯功能表

在仍選取 RoverExplorer 物件的情況下,在 [偵測器] 視窗中設定 變形 元件,如下所示:With the RoverExplorer object still selected, in the Inspector window, configure the Transform component as follows:

  • 位置:X = 0、Y = -0.6、Z = 2Position: X = 0, Y = -0.6, Z = 2
  • 旋轉:X = 0、Y = 0、Z = 0Rotation: X = 0, Y = 0, Z = 0
  • 縮放:X = 1、Y = 1、Z = 1Scale: X = 1, Y = 1, Z = 1

已選取並置放 RoverExplorer 物件的 Unity

注意

相機代表使用者頭部,位於原點:X = 0、Y = 0、Z = 0。The camera represents the users head and is positioned at origin, X = 0, Y = 0, Z = 0. 一般情況下,Unity 中的 1 個單位大約是真實世界的 1 公尺。In general, 1 unit in Unity is roughly 1 meter in the physical world. 但有例外狀況,例如,當物件是所縮放物件的子系時。However, there are exceptions to this, for example, when objects are children of scaled objects. 在上述案例中,RoverExplorer 會放置在使用者頭部前方 2 公尺及下方 0.6 公尺處。In the scenario above, the RoverExplorer is positioned 2 meters in front of and 0.6 meters below the user's head.

新增教學課程 PrefabAdding the tutorial prefabs

在 [專案] 視窗中,瀏覽至 [資產] > [MRTK.Tutorials.GettingStarted] > [Prefabs] 資料夾。In the Project window, navigate to the Assets > MRTK.Tutorials.GettingStarted > Prefabs folder:

已選取 Prefabs 資料夾的 Unity [專案] 視窗

提示

Prefab 是預先設定且儲存為 Unity 資產的 GameObject,可以在整個專案中重複使用。A prefab is a pre-configured GameObject stored as a Unity Asset and can be reused throughout your project.

在 [專案] 視窗中,按一下 資料表 Prefab 並拖曳至 RoverExplorer 物件上,使其成為 RoverExplorer 物件的子系,然後在 [偵測器] 視窗中設定 變形 元件,如下所示:From the Project window, click-and-drag the Table prefab on to the RoverExplorer object to make it a child of the RoverExplorer object, then in the Inspector window, configure the Transform component as follows:

  • 位置:X = 0、Y = -0.005、Z = 0Position: X = 0, Y = -0.005, Z = 0
  • 旋轉:X = 0、Y = 0、Z = 0Rotation: X = 0, Y = 0, Z = 0
  • 縮放:X = 1.2、Y = 0.01、Z = 1.2Scale: X = 1.2, Y = 0.01, Z = 1.2

已選取並置放新加入資料表 Prefab 的 Unity

提示

若要顯示您的場景 (如上圖所示),請使用場景視窗右上角的場景 Gizmo,將檢視角度調整為以 Z 軸為前方,並按兩下 MixedRealityPlayspace 物件將焦點放在相機上,然後視需要進行放大。To display your scene as shown in the image above, use the Scene Gizmo, located in the top right corner of the Scene window, to adjust the viewing angle to be along the forward Z axis, double-click the MixedRealityPlayspace object to focus on the camera, and zoom in as needed.

在 [專案] 視窗中,按一下 [RoverAssembly] Prefab 並拖曳至 RoverExplorer 物件,使其成為 RoverExplorer 物件的子系,然後在 [偵測器] 視窗中設定 變形 元件,如下所示:From the Project window, click-and-drag the RoverAssembly prefab on to the RoverExplorer object to make it a child of the RoverExplorer object, then in the Inspector window, configure the Transform component as follows:

  • 位置:X = -0.1、Y = 0、Z = 0Position: X = -0.1, Y = 0, Z = 0
  • 旋轉:X = 0、Y = -135、Z = 0Rotation: X = 0, Y = -135, Z = 0
  • 縮放:X = 1、Y = 1、Z = 1Scale: X = 1, Y = 1, Z = 1

已選取並置放新加入 RoverAssembly Prefab 的 Unity

組織集合中的 3D 物件Organizing objects in a collection

在 [階層] 視窗中,以滑鼠右鍵按一下 RoverExplorer 物件並選取 [建立空物件],將空白物件新增為 RoverExplorer 的子系,然後將物件命名為 RoverParts,並設定 變形 元件,如下所示:In the Hierarchy window, right-click on the RoverExplorer object and select Create Empty to add an empty object as a child of the RoverExplorer, name the object RoverParts, and configure the Transform component as follows:

  • 位置:X = 0、Y = 0.06、Z = 0Position: X = 0, Y = 0.06, Z = 0
  • 旋轉:X = 0、Y = 90、Z = 0Rotation: X = 0, Y = 90, Z = 0
  • 縮放:X = 1、Y = 1、Z = 1Scale: X = 1, Y = 1, Z = 1

已選取並置放新建立 RoverParts 物件的 Unity

在 [階層] 視窗中,選取所有 RoverExplorer > RoverAssembly > RoverModel > Parts 子物件,以滑鼠右鍵按一下這些子物件,然後選取 [複製],為每個零件建立複本:In the Hierarchy window, select all the RoverExplorer > RoverAssembly > RoverModel > Parts child objects, right-click on them and select Duplicate to create a copy of each of the parts:

已選取所有組件且具有 [複製] 內容快顯功能表的 Unity

提示

若要選取多個連續的物件,請按住 SHIFT 鍵,同時使用滑鼠來選取第一個和最後一個物件。To select multiple adjacent objects, press-and-hold the SHIFT key while using the mouse to select the first and last object.

繼續選取新複製的 Parts 子物件,按一下這些物件並拖曳至 RoverParts 物件,使其成為 RoverParts 物件的子物件:With the newly duplicated Parts child objects still selected, click-and-drag them on to the RoverParts object to make them child objects of the RoverParts object:

以新複製的組件作為 RoverParts 物件子系的 Unity

為了讓您更輕鬆地使用場景,請在 [階層] 視窗中,按一下物件左邊的 眼睛 圖示,以將 RoverAssembly 物件的 場景可見度 切換為關閉。To make it easier to work with your scene, in the Hierarchy window, click the eye icon to the left of the object to toggle the scene visibility for the RoverAssembly object off. 這會在場景視窗中隱藏物件,而不會變更其遊戲內的可見度:This hides the object in the Scene window without changing its in-game visibility:

已關閉 RoverAssembly 場景可見度的 Unity

提示

若要深入了解場景可見度的控制項,以及如何使用其來最佳化場景檢視和工作流程,您可以參閱 Unity 的場景可見度文件。To learn more about the Scene Visibility controls and how you can use them to optimize your scene view and workflow, you can refer to Unity's Scene Visibility documentation.

在 [階層] 視窗中,將附加的 (1) 取代為 _Part,以清除 RoverParts 子物件的名稱:In the Hierarchy window, clean up the RoverParts child objects' names by replacing the appended (1) with _Part:

已清除重複組件名稱的 Unity

在 [階層] 視窗中,選取 RoverParts 物件,然後在 [偵測器] 視窗中,按一下 [新增元件] 按鈕,搜尋並選取 [GridObjectCollection],將 GridObjectCollection 元件新增至 RoverParts 物件:In the Hierarchy window, select the RoverParts object, then in the Inspector window, click the Add Component button, and search for and select GridObjectCollection to add the GridObjectCollection component to the RoverParts object:

正在進行 [新增元件 Grid Object Collection] 的 Unity RoverParts 物件

設定 GridObjectCollection 元件值,如下所示:Configure the GridObjectCollection component values as follows:

  • 排序類型:字母順序Sort Type: Alphabetic
  • 版面配置:水平Layout: Horizontal
  • 儲存格寬度:0.25Cell Width: 0.25
  • 與父系的距離:0.38Distance from parent: 0.38

已設定 GridObjectCollection 元件的 Unity

然後按一下 [更新集合] 按鈕,以更新 RoverParts 子物件的位置:Then click the Update Collection button to update the position of the RoverParts child objects:

已套用 GridObjectCollection 元件的 Unity

恭喜!Congratulations

在本教學課程中,您已了解如何將物件放置在場景中與使用者相對的位置,並使用 MRTK 的網格物件集合功能來組織集合中的物件。In this tutorial, you learned how to position objects in the scene relative to the user and use MRTK's Grid Object Collection feature to organize objects in a collection.