2.初始化您的專案和第一個應用程式2. Initializing your project and first application

在第一個教學課程中,您將著手進行新的 Unreal 項目,並啟用 HoloLens 外掛程式、建立和亮顯層級,以及新增棋子。In the first tutorial, you'll start out with a new Unreal project and enable the HoloLens plugin, create and light a level, and add chess pieces. 您將對所有 3D 物件和材質使用預先製作的資產,因此無須擔心自行建立模型的問題。You'll be using our pre-made assets for all 3D objects and materials, so don't worry about modeling anything yourself. 在本教學課程結束時,您將會有一個可供混合實境使用的空白畫布。By the end of this tutorial, you'll have a blank canvas that's ready for mixed reality.

重要

請確定您擁有開始使用頁面中的所有必要項目。Make sure you have all the prerequisites from the Getting Started page.

目標Objectives

  • 設定 Unreal 專案以用於 HoloLens 開發Configuring an Unreal project for HoloLens development
  • 匯入資產和設定場景Importing assets and setting up a scene
  • 使用藍圖建立動作項目和指令碼層級事件Creating Actors and script-level events with blueprints

建立新的 Unreal 專案Creating a new Unreal project

您需要的第一個項目是供您使用的專案。The first thing you need is a project to work with. 如果您是首次接觸的 Unreal 開發人員,則需要從 Epic Launcher 下載支援檔案If you're a first-time Unreal developer, you'll need to download supporting files from the Epic Launcher.

  1. 啟動 Unreal EngineLaunch Unreal Engine

  2. 在 [新增專案類別] 中選取 [遊戲],然後按 [下一步]。Select Games in New Project Categories and click Next.

選取遊戲專案範本

  1. 選取 空白 範本,然後按 [下一步]。Select the Blank Template and click Next.

選取空白範本

  1. 將 [C++]、[可縮放 3D 或 2D]、[行動裝置/平板電腦] 及 [無起始內容] 設為您的 [專案設定],然後選擇儲存位置並按一下 [建立專案]。Set C++, Scalable 3D or 2D, Mobile/Tablet, and No Starter Content as your Project Settings, then choose a save location and click Create Project.

注意

您必須選取 C++ 專案 (而非藍圖專案) 以建置 UX 工具外掛程式 (您稍後將在第 4 節設定此外掛程式)。You must select a C++ project rather than a Blueprint project in order to build the UX Tools plugin, which you'll be setting up later on in section 4.

初始專案設定

專案應該會自動在 Unreal 編輯器中開啟,這表示您已準備好進行下一節。The project should open up automatically in the Unreal editor, which means you're ready for the next section.

啟用必要的外掛程式Enabling required plugins

在您開始將物件新增至場景之前,必須啟用兩個外掛程式。You'll need to enable two plugins before you can start adding objects to the scene.

  1. 開啟 [編輯] > [外掛程式],然後從內建選項清單中選取 [擴增實境]。Open Edit > Plugins and select Augmented Reality from the built-in options list.
    • 向下捲動至 HoloLens,並勾選 [啟用]。Scroll down to HoloLens and check Enabled.

啟用 HoloLens 外掛程式

  1. 從內建選項清單中選取 [虛擬實境]。Select Virtual Reality from the built-in options list.
    • 向下捲動至 Microsoft Windows Mixed Reality、勾選 [啟用],然後重新啟動您的編輯器。Scroll down to Microsoft Windows Mixed Reality, check Enabled, and restart your editor.

啟用 Windows Mixed Reality 外掛程式

注意

這兩個外掛程式都是進行 HoloLens 2 開發的必要項目。Both plugins are required for HoloLens 2 development.

啟用外掛程式後,您的空白層級即可供公司使用。With the plugins enabled, your empty level is ready for company.

建立層級Creating a level

您的下一個工作是建立玩家設定,其中包含一個起點和一個用於參考和調整的立方體。Your next task is to create a player setup with a starting point and a cube for reference and scale.

  1. 選取 [檔案] > [新增層級],然後選擇 [空白層級]。Select File > New Level and choose Empty Level. 檢視區中的預設場景現在應該是空白的。The default scene in the viewport should now be empty.

  2. 從 [模式] 索引標籤中選取 [基本],然後將 PlayerStart 拖曳至場景。Select Basic from the Modes tab and drag PlayerStart into the scene.

    • 在 [詳細資料] 索引標籤中,將 [位置] 設定為 X = 0Y = 0Z = 0,這會在應用程式啟動時,將使用者設釘在場景中心。Set Location to X = 0, Y = 0, and Z = 0 in the Details tab to set the user at the center of the scene when the app starts up.

具有 PlayerStart 的檢視區

  1. 將 [立方體] 從 [基本] 索引標籤拖曳到場景。Drag a Cube from the Basic tab into the scene.
    • 將 [位置] 設定為 X = 50Y = 0Z = 0Set Location to X = 50, Y = 0, and Z = 0. 在開始時將立方體放在離玩家 50 公分處。to position the cube 50 cm away from the player at start time.
    • 將 [縮放] 變更為 X = 0.2Y = 0.2Z = 0.2,以縮小立方體。Change Scale to X = 0.2, Y = 0.2, and Z = 0.2 to shrink the cube down.

您將無法看到立方體,除非您將光線新增至場景,這是測試場景前的最後一項工作。You can't see the cube unless you add a light to your scene, which is your last task before testing the scene.

  1. 切換至 [模式] 面板上的 [光線] 索引標籤,然後將 [定向光線] 拖曳至場景。Switch to the Lights tab in the Modes panel and drag a Directional Light into the scene. 將光線放在 PlayerStart 上方,讓您可以看到。Position the light above PlayerStart so you can see it.

已新增光線的檢視區

  1. 移至 [檔案] > [儲存目前的]、將您的層級命名為 Main,然後選取 [儲存]。Go to File > Save Current, name your level Main, and select Save.

設定場景後,請按下工具列中的 [播放],以查看行動中的立方體!With the scene set, press Play in the toolbar to see your cube in action! 當您完成工作的管理時,請按 Esc 以停止應用程式。When you're finished admiring your work, press Esc to stop the application.

檢視區中的立方體

場景現已完成設定,您可以開始加入棋盤和棋子,以完善應用程式環境。Now that the scene is set up, you can start adding in the chess board and piece to round out the application environment.

匯入資產Importing assets

場景目前看起來有點空,但是您會將現成的資產匯入專案,以修正此情況。The scene is looking a bit empty at the moment, but you'll fix that by importing the ready-made assets into the project.

  1. 使用 7-zip 下載並解壓縮 GitHub 資產資料夾。Download and unzip the GitHub assets folder using 7-zip.

  2. 從 [內容瀏覽器] 中選取 [新增] > [新增資料夾],並將其命名為 ChessAssetsSelect Add New > New Folder from the Content Browser and name it ChessAssets.

    • 按兩下您將在其中匯入3D 資產的新資料夾。Double-click the new folder where you'll import the 3D assets.

顯示或隱藏來源面板

  1. 從 [內容瀏覽器] 中選取 [匯入]、選取解壓縮資產資料夾中的所有專案,然後按一下 [開啟]。Select Import from the Content Browser, select all the items in the unzipped assets folder and click Open.

    • 資產包含適用於棋盤和棋子並採用 FBX 格式的 3D 物件網格,以及將用於材質並採用 TGA 格式的紋理圖。Assets include the 3D object meshes for the chess board and pieces in FBX format and texture maps in TGA format that you'll use to for materials.
  2. 當 [FBX 匯入選項] 視窗快顯時,請展開 [材質] 區段,然後將 [材質匯入方法] 變更為 [不要建立材質]。When the FBX Import Options window pops up, expand the Material section and change Material Import Method to Do Not Create Material.

    • 選取 [全部匯入]。Select Import All.

匯入 FBX 選項

您只需針對資產執行這個動作即可。That's all you need to do for the assets. 您的下一組工作是使用藍圖來建立應用程式的建置區塊。Your next set of tasks is to create the building blocks of the application with blueprints.

新增藍圖Adding blueprints

  1. 選取 [內容瀏覽器] 中的 [新增] > [新增資料夾],並將其命名為 BlueprintsSelect Add New > New Folder in the Content Browser and name it Blueprints.

注意

如果您不熟悉藍圖,這些藍圖是特殊資產,可提供節點型介面,用於建立新類型的動作項目和指令碼層級事件。If you're new to blueprints, they're special assets that provide a node-based interface for creating new types of Actors and script level events.

  1. 按兩下 [藍圖] 資料夾,然後按一下滑鼠右鍵並選取 [藍圖類別]。Double-click into the Blueprints folder, then right-click and select Blueprint Class.
    • 選取 [動作項目],並將藍圖命名為 BoardSelect Actor and name the blueprint Board.

選取您藍圖的父類別

新的 Board 藍圖現在會顯示在 Blueprints 資料夾中,如下列螢幕擷取畫面中所看到。The new Board blueprint now shows up in the Blueprints folder as seen in the following screenshot.

新的棋盤藍圖

您已準備好開始將材質新增至所建立的物件。You're all set to start adding materials to the created objects.

使用材質Working with materials

您所建立的物件預設為灰色,這並不好查看。The objects you've created are default grey, which isn't much fun to look at. 將材質和網格新增至您的物件是本教學課程中的最後一組工作。Adding materials and meshes to your objects is the last set of tasks in this tutorial.

  1. 按兩下 Board 以開啟藍圖編輯器。Double-click Board to open the blueprint editor.

  2. 從 [元件] 面板中選取 [新增元件] > [場景],並將其命名為 RootSelect Add Component > Scene from the Components panel and name it Root. 請注意,Root 會在下列螢幕擷取畫面中顯示為 DefaultSceneRoot 的子系:Notice that Root shows up as a child of DefaultSceneRoot in the screenshot below:

取代藍圖中的根項目

  1. 按一下 Root 並將其拖曳至 DefaultSceneRoot 以進行取代,然後消除檢視區中的球體。Click-and-drag Root onto DefaultSceneRoot to replace it and get rid of the sphere in the viewport.

取代根項目

  1. 從 [元件] 面板中選取 [新增元件] > [靜態網格],並將其命名為 SM_BoardSelect Add Component > Static Mesh from the Components panel and name it SM_Board. 其會在 Root 底下顯示為子物件。It will appear as a child object under Root.

新增靜態網格

  1. 選取 SM_Board、向下捲動至 [ 詳細資料] 面板的 [靜態網格] 區段,然後從下拉式清單中選取 ChessBoardSelect SM_Board, scroll down to the Static Mesh section of the Details panel, and select ChessBoard from the dropdown.

檢視區中的棋盤網格

  1. 仍在 [詳細資料] 面板中,展開 [材質] 區段,然後從下拉式清單中選取 [建立新資產] > [材質]。Still in the Details panel, expand the Materials section and select Create New Asset > Material from the dropdown.
    • 將材質命名為 M_ChessBoard,並將其儲存在 ChessAssets 資料夾中。Name the material M_ChessBoard and save it to the ChessAssets folder.

建立新的材質

  1. 按兩下 M_ChessBoard 材質影像以開啟材質編輯器。Double-click the M_ChessBoard material imaged to open the Material Editor.

開啟材質編輯器

  1. 在材質編輯器中,按一下滑鼠右鍵,然後搜尋 紋理範例In the Material Editor, right-click and search for Texture Sample.
    • 展開 [詳細資料] 面板中的 [材質呈現紋理基底] 區段,然後將 [紋理] 設定為 ChessBoard_AlbedoExpand the Material Expression Texture Base section in the Details panel and set Texture to ChessBoard_Albedo.
    • RGB 輸出釘選拖曳到 M_ChessBoard 的 [基本色彩] 釘選。Drag the RGB output pin to the Base Color pin of M_ChessBoard.

設定基本色彩

  1. 再重複 4 次上述步驟,使用下列設定再建立四個 紋理範例 節點:Repeat the previous step 4 more times to create four more Texture Sample nodes with the following settings:
    • 將 [材質] 設定為 ChessBoard_AO,並將 RGB 連結至 [環境光遮蔽] 釘選。Set Texture to ChessBoard_AO and link the RGB to the Ambient Occlusion pin.
    • 將 [材質] 設定為 ChessBoard_Metal,並將 RGB 連結至 [金屬] 釘選。Set Texture to ChessBoard_Metal and link the RGB to the Metallic pin.
    • 將 [材質] 設定為 ChessBoard_Normal,並將 RGB 連結至 [一般] 釘選。Set Texture to ChessBoard_Normal and link the RGB to the Normal pin.
    • 將 [材質] 設定為 ChessBoard_Rough,並將 RGB 連結至 [粗糙度] 釘選。Set Texture to ChessBoard_Rough and link the RGB to the Roughness pin.
    • 按一下 [儲存]Click Save.

連結其餘紋理

請先確定您的材質設定看起來像是上述螢幕擷取畫面,再繼續進行。Make sure your material setup looks like the above screenshot before continuing.

填入場景Populating the scene

如果回到 Board 藍圖,您會看到已套用您剛建立的材質。If you go back to the Board blueprint, you'll see that the material you just created has been applied. 剩下的就是設定場景!All that's left is setting up the scene! 首先,變更下列屬性,以確保棋盤在放入場景時大小合理,且角度正確:First, change the following properties to make sure the board is a reasonable size and angled correctly when it's placed in the scene:

  1. 將 [縮放] 設定為 (0.05, 0.05, 0.05) ,並將 [Z 旋轉] 設定為 90Set Scale to (0.05, 0.05, 0.05) and Z Rotation to 90.
    • 按一下頂端工具列中的 [編譯],然後按一下 [儲存] 並返回主視窗。Click Compile in the top toolbar, then Save and return to the Main window.

已套用材質的棋盤

  1. 以滑鼠右鍵按一下 [立方體] > [編輯] > [刪除],然後將 [棋盤] 從 [內容瀏覽器] 拖曳至檢視區。Right-click Cube > Edit > Delete and drag Board from the Content Browser into the viewport.

    • 將 [位置] 設定為 X = 80Y = 0Z = 20Set Location to X = 80, Y = 0, and Z = -20.
  2. 選取 [播放] 按鈕,以在層級中檢視您的新棋盤。Select the Play button to view your new board in the level. Esc 即可返回編輯器。Press Esc to return to the editor.

現在您將遵循相同的步驟來建立棋子,如同您建立棋盤一般:Now you'll follow the same steps to create a chess piece as you did with the board:

  1. 移至 [藍圖] 資料夾、按一下滑鼠右鍵並選取 [藍圖類別],然後選擇 [動作項目]。Go to the Blueprints folder, right-click, and select Blueprint Class and choose Actor. 將動作項目命名為 WhiteKingName the actor WhiteKing.

  2. 按兩下 WhiteKing 以在藍圖編輯器中將其開啟、選取 [新增元件] > [場景],並將其命名為 RootDouble-click WhiteKing to open it in the Blueprint Editor, select Add Component > Scene and name it Root.

    • Root 拖放至 DefaultSceneRoot,以將其取代。Drag-and-drop Root onto DefaultSceneRoot to replace it.
  3. 按一下 [新增元件] > [靜態網格],並將其命名為 SM_KingClick Add Component > Static Mesh and name it SM_King.

    • 在 [詳細資料] 面板中,將 [靜態網格] 設定為 Chess_King,將 [材質] 設定為名為 M_ChessWhite 的新材質。Set Static Mesh to Chess_King and Material to a new Material called M_ChessWhite in the Details panel.
  4. 在材質編輯器中開啟 M_ChessWhite,並將下列 紋理範例 節點連結至下列項目:Open M_ChessWhite in the Material editor and hook up the following Texture Sample nodes to the following:

    • 將 [紋理] 設定為 ChessWhite_Albedo,並將 RGB 連結至 [基本色彩] 釘選。Set Texture to ChessWhite_Albedo and link the RGB to the Base Color pin.
    • 將 [材質] 設定為 ChessWhite_AO,並將 RGB 連結至 [環境光遮蔽] 釘選。Set Texture to ChessWhite_AO and link the RGB to the Ambient Occlusion pin.
    • 將 [材質] 設定為 ChessWhite_Metal,並將 RGB 連結至 [金屬] 釘選。Set Texture to ChessWhite_Metal and link the RGB to the Metallic pin.
    • 將 [材質] 設定為 ChessWhite_Normal,並將 RGB 連結至 [一般] 釘選。Set Texture to ChessWhite_Normal and link the RGB to the Normal pin.
    • 將 [材質] 設定為 ChessWhite_Rough,並將 RGB 連結至 [粗糙度] 釘選。Set Texture to ChessWhite_Rough and link the RGB to the Roughness pin.
    • 按一下 [儲存]Click Save.

在繼續之前,您的 M_ChessKing 材質應該看起來像是下圖。Your M_ChessKing material should look like the following image before continuing.

為國王棋建立材質

您幾乎只需將新的棋子加入場景中就大功告成了:You're almost there, just need to add the new chess piece into the scene:

  1. 開啟 WhiteKing 藍圖,然後將 [縮放] 變更為 (0.05, 0.05, 0.05) ,並將 [Z 旋轉] 設定為 90Open the WhiteKing blueprint and change the Scale to (0.05, 0.05, 0.05) and Z Rotation to 90.

    • 編譯並儲存您的藍圖,然後回到主視窗。Compile and save your blueprint, then head back to the main window.
  2. WhiteKing 拖曳到檢視區、切換至 [世界大綱] 面板,然後將 WhiteKing 拖曳至 [棋盤],使其成為子物件。Drag WhiteKing into the viewport, switch to the World Outliner panel drag WhiteKing onto Board to make it a child object.

世界大綱

  1. 在 [變形] 底下的 [詳細資料] 面板中,將 WhiteKing 的 [位置] 設定為 X = -26Y = 4Z = 0In the Details panel under Transform, set WhiteKing's Location to X = -26, Y = 4, and Z = 0.

收工了!That's a wrap! 選取 [播放] 查看您填入的作用中層級,然後在您準備好要結束時,按 EscSelect Play to see your populated level in action, and press Esc when you're ready to exit. 您僅僅是建立簡單的專案就牽涉到許多層面,而現在您可以繼續進行本系列的下一個部分:設定混合實境。You covered a lot of ground just creating a simple project, but now you're ready to move on to the next part of the series: setting up for mixed reality.

下一節:3.設定您的專案以進行混合實境Next Section: 3. Set up your project for mixed reality