使用 Visual Studio for Mac Tools for UnityUsing Visual Studio for Mac Tools for Unity

在本節中,您將了解如何使用 Visual Studio for Mac Tools for Unity 的整合和生產力功能,以及如何針對 Unity 開發使用 Visual Studio for Mac 偵錯工具。In this section, you'll learn how to use Visual Studio for Mac Tools for Unity's integration and productivity features, and how to use the Visual Studio for Mac debugger for Unity development.

在 Visual Studio for Mac 中開啟 Unity 指令碼Opening Unity scripts in Visual Studio for Mac

將 Visual Studio for Mac 設定為 Unity 的外部指令碼編輯器之後,在開啟所選擇指令碼的情況下,從 Unity 編輯器中開啟任何指令碼將會自動啟動或切換至 Visual Studio for Mac。Once Visual Studio for Mac is set as the external script editor for Unity, opening any script from the Unity editor will automatically launch or switch to Visual Studio for Mac, with the chosen script open.

或者,從 Unity 的 [資產] 功能表中選取 [Open C# Project] (開啟 C# 專案),也可以在原始檔編輯器中未開啟任何指令碼的情況下開啟 Visual Studio for Mac。Alternatively, Visual Studio for Mac can be opened with no script open in the source editor by selecting Open C# Project from the Assets menu in Unity.

開啟 C# 專案

Unity 文件存取Unity documentation access

Visual Studio for Mac Tools for Unity 包含存取 Unity API 文件的捷徑。Visual Studio for Mac Tools for Unity includes a shortcut for accessing the Unity API documentation. 若要從 Visual Studio for Mac 存取 Unity API 文件,請將游標放在您要了解的 Unity API 上方,然後按 ⌘ 命令 + ‘To access Unity API documentation from Visual Studio for Mac, place the cursor over the Unity API you want to learn about and press ⌘ command + ‘.

Unity 訊息的 IntelliSenseIntelliSense for Unity messages

Unity 引擎會將訊息廣播到 MonoBehaviour 指令碼,讓開發人員可以撰寫回應訊息的程式碼,例如 OnMouseDown、OnTriggerEnter 等等。因為這些不是基底 MonoBehaviour 類別中的虛擬方法,所以有些 IDE (例如 MonoDevelop) 缺乏 Unity 訊息的程式碼完成功能。The Unity engine broadcasts messages to MonoBehaviour scripts, allowing developers to write code that reacts to messages such as OnMouseDown, OnTriggerEnter, etc. Because these are not virtual methods in the base MonoBehaviour class, some IDEs such as MonoDevelop lack code completion functionality for Unity messages.

不過,Visual Studio for Mac Tools for Unity 會將其 IntelliSense 功能擴充為 Unity 訊息。However, Visual Studio for Mac Tools for Unity extends its IntelliSense functionality to Unity messages. 這可在 MonoBehaviour 指令碼中輕鬆地實作 Unity 訊息,並協助了解 Unity API。This makes it easy to implement Unity messages in MonoBehaviour scripts, and assists with learning the Unity API. 使用適用於 Unity 訊息的 IntelliSense:To use IntelliSense for Unity messages:

  1. 將游標放在衍生自 MonoBehaviour 之類別主體內的新行。Place the cursor on a new line inside the body of a class that derives from MonoBehaviour.

  2. 開始鍵入 Unity 訊息的名稱,例如 OnTriggerEnterBegin typing the name of a Unity message, such as OnTriggerEnter.

  3. 已鍵入 "ont" 字母之後,會出現 IntelliSense 建議清單。Once the letters "ont" have been typed, a list of IntelliSense suggestions appears.

    Using IntelliSense

  4. 有三種方式可以變更清單上的選項:The selection on the list can be changed in three ways:

    • 使用向上和向下方向鍵。With the Up and Down arrow keys.

    • 使用滑鼠按一下所需的項目。By clicking with the mouse on the desired item.

    • 繼續鍵入所需項目的名稱。By continuing to type the name of the desired item.

  5. IntelliSense 可以插入選取的 Unity 訊息,包含任何必要參數:IntelliSense can insert the selected Unity message, including any necessary parameters:

    • TabBy pressing Tab.

    • ReturnBy pressing Return.

    • 按兩下選取的項目。By double-clicking the selected item.

    從 IntelliSense 插入 Unity 訊息

新增 Unity 檔案和資料夾Adding new Unity files and folders

雖然您一律可以在 Unity 編輯器中將新檔案新增至 Unity 專案,但是 Visual Studio for Mac 允許在 Visual Studio 內輕鬆地建立新的 Unity 指令碼、著色器和資料夾。While you can always add new files to a Unity project in the Unity editor, Visual Studio for Mac allows for easily creating new Unity scripts, shaders, and folders from within Visual Studio.

新增 C# MonoBehaviour 指令碼Add a new C# MonoBehaviour script

若要新增 C# MonoBehaviour 指令碼,請以滑鼠右鍵按一下 [資產] 資料夾或它在 Solution Pad 中的其中一個子目錄,然後選取 [新增] > [New MonoBehaviour] (新增 MonoBehaviour)。To add a new C# MonoBehaviour script, right-click on the Assets folder or one of its subdirectories in the Solution pad and select Add > New MonoBehaviour.

新增 MonoBehaviour

新增 Unity 著色器Add a new Unity shader

若要新增 Unity 著色器,請以滑鼠右鍵按一下 [資產] 資料夾或 Solution Pad 中的子目錄,然後選取 [新增] > [新增著色器]。To add a new Unity shader, right-click on the Assets folder or a subdirectory in the Solution pad and select Add > New Shader.

新增資料夾Add a new folder

若要新增資料夾,請以滑鼠右鍵按一下 [資產] 資料夾或 Solution Pad 中的子目錄,然後選取 [新增] > [新增資料夾]。To add a new folder, right-click on the Assets folder or a subdirectory in the Solution pad and select Add > New Folder.

這些新增作業會反映在 Unity 編輯器的 [專案] 視窗中。These additions are reflected in the Project window of the Unity editor.

重新命名檔案或資料夾To rename a file or folder

以滑鼠右鍵按一下 Solution Pad 中要重新命名的項目,然後選取 [重新命名]。right-click on the item to rename in the Solution pad and select Rename....

Note

如果您的新 Unity 專案沒有指令碼,而且 [資產] 資料夾未顯示在 Visual Studio for Mac 的 Solution Pad 中,請從 Unity 編輯器新增初始 C# 指令碼。If you have a new Unity project with no scripts and the Assets folder does not show up in the Solution pad in Visual Studio for Mac, add an initial C# script from within the Unity editor.

Unity 偵錯Unity debugging

Unity 專案可以使用 Visual Studio for Mac 進行偵錯。Unity projects can be debugged with Visual Studio for Mac.

開始偵錯Start debugging

啟動偵錯:To start debugging:

  1. 按一下 [播放] 按鈕,或者鍵入 Command + ReturnF5,以將 Visual Studio 連線至 Unity。Connect Visual Studio to Unity by clicking the Play button, or type Command + Return, or F5.

    在 Visual Studio 中按一下 [播放]

  2. 切換至 Unity,然後按一下 [播放] 按鈕,以在編輯器中執行遊戲。Switch to Unity and click the Play button to run the game in the editor.

    在 Unity 中按一下 [播放]

  3. 如果遊戲在連線至 Visual Studio 時於 Unity 編輯器中執行,則遇到的任何中斷點都會暫停執行遊戲,並啟動遊戲在 Visual Studio for Mac 中叫用中斷點的一行程式碼。When the game is running in the Unity editor while connected to Visual Studio, any breakpoints encountered will pause execution of the game and bring up the line of code where the game hit the breakpoint in Visual Studio for Mac.

停止偵錯Stop debugging

停止偵錯:To stop debugging:

  1. 在 Visual Studio for Mac 中按一下 [停止] 按鈕,或按 Shift + Command + ReturnClick the Stop button in Visual Studio for Mac, or press Shift + Command + Return.

    在 Visual Studio 中按一下 [停止]

若要深入了解 Visual Studio for Mac 中的偵錯,請參閱 Using the debugger (使用偵錯工具)。To learn more about debugging in Visual Studio for Mac, see Using the debugger.