使用 Visual Studio Tools for UnityUse Visual Studio Tools for Unity

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

在 Visual Studio 中開啟 Unity 指令碼Open Unity scripts in Visual Studio

一旦 Visual Studio 設定為 unity 的外部編輯器之後,按兩下 unity 編輯器中的腳本將會自動啟動或切換至 Visual Studio,然後開啟所選的腳本。Once Visual Studio is set as the external editor for Unity, double-clicking a script from the Unity editor will automatically launch or switch to Visual Studio and open the chosen script.

或者,您也可以在 Unity 中選取 [ > 開啟 c # 專案 ] 功能表中的 [資產],開啟沒有開啟腳本的 Visual Studio。Alternatively, you can open Visual Studio with no script open in the source editor by selecting the Assets > Open C# Project menu in Unity.

在 Visual Studio 中開啟 c # 專案

在 Visual Studio for Mac 中開啟 c # 專案

Unity 文件存取Unity documentation access

您可以從 Visual Studio 快速存取 Unity 指令碼文件。You can access the Unity scripting documentation quickly from Visual Studio. 如果 Visual Studio Tools for Unity 在本機找不到應用程式開發介面文件,則會嘗試在線上尋找。If Visual Studio Tools for Unity doesn't find the API documentation locally, it will try to find it online.

  • 在 Visual Studio 中,反白顯示或將游標放在您想要瞭解的 Unity API 上方,然後按 ctrl + Alt + Mctrl + HIn Visual Studio, highlight or place the cursor over the Unity API you want to learn about, then press Ctrl+Alt+M , Ctrl+H
  • 您也可以使用 [說明 > UNITY API 參考 ] 功能表,而不是 keybinding。You can also use the Help > Unity API Reference menu instead of the keybinding. Visual Studio 中的 [Unity API 參考] 功能表The Unity API Reference menu in Visual Studio
  • 在 Visual Studio for Mac 中,反白顯示或將游標放在您想要瞭解的 Unity API 上方,然後按下 Cmd + 'In Visual Studio for Mac, highlight or place the cursor over the Unity API you want to learn about, then press Cmd+'
  • 您也可以使用 [說明 > UNITY API 參考 ] 功能表,而不是 keybinding。You can also use the Help > Unity API Reference menu instead of the keybinding. Visual Studio for Mac 中的 [Unity API 參考] 功能表The Unity API Reference menu in Visual Studio for Mac

Unity API 訊息的 IntelliSenseIntellisense for Unity API Messages

Intellisense 程式碼編譯可在 MonoBehaviour 指令碼中輕鬆地實作 Unity API 訊息,並協助了解 Unity API。Intellisense code-completion makes it easy to implement Unity API 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. 一旦鍵入 「 ontri " 字母之後,會出現 IntelliSense 建議清單。Once the letters " ontri " have been typed, a list of IntelliSense suggestions appears.

在 Visual Studio 中使用 Intellisense

  1. 有三種方式可以變更清單上的選項: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.

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

    • TabBy pressing Tab.

    • EnterBy pressing Enter.

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

在 Visual Studio 中從 IntelliSense 插入 Unity 訊息

Unity MonoBehavior 指令碼精靈Unity MonoBehavior scripting wizard

您可以使用 MonoBehavior 精靈來檢視所有 Unity API 方法清單,並快速實作空白的定義。You can use the MonoBehavior wizard to view a list of all the Unity API methods and quickly implement an empty definition. 若您仍在了解 Unity API 中可用的項目有哪些,此功能 (特別是在已啟用 [產生方法註解] 選項時) 非常實用。This feature, particularly with the Generate method comments option enabled, is helpful if you are still learning what's available in the Unity API.

使用 MonoBehavior 精靈建立空白 MonoBehavior 方法定義:To create empty MonoBehavior method definitions with the MonoBehavior wizard:

  1. 在 Visual Studio 中,將游標放在您想要插入方法的位置,然後按 Ctrl + Shift + M 以啟動 MonoBehavior wizard。In Visual Studio, position the cursor where you want the methods to be inserted, then press Ctrl+Shift+M to launch the MonoBehavior wizard. 在 Visual Studio for Mac 中,按下 Cmd + Shift + MIn Visual Studio for Mac, press Cmd+Shift+M.

  2. 在 [建立指令碼方法] 視窗中,標記您要加入之每個方法名稱旁的核取方塊。In the Create script methods window, mark the checkbox next to the name of each method you want to add.

  3. 使用 [Framework 版本] 下拉式清單來選取您想要的版本。Use the Framework version dropdown to select your desired version.

  4. 根據預設,方法會插入到游標所在位置。By default, the methods are inserted at the position of the cursor. 或者,您可以選擇將它們插入到已在您的類別中實作的任何方法之後,方法是將 [插入點] 下拉式清單的值變更為您想要的位置。Alternatively, you can choose to insert them after any method that's already implemented in your class by changing the value of the Insertion point dropdown to the location you want.

  5. 如果您希望精靈為您選取的方法產生註解,請標記 [產生方法註解] 核取方塊。If you want the wizard to generate comments for the methods you selected, mark the Generate method comments checkbox. 這些註解可協助您了解何時呼叫方法,以及方法的一般責任為何。These comments are meant to help you understand when the method is called and what its general responsibilities are.

  6. 選擇 [確定] 按鈕以結束精靈,並將方法插入到您的程式碼。Choose the OK button to exit the wizard and insert the methods into your code.

Visual Studio 中的 [monobehavior wizard] 對話方塊。

Visual Studio for Mac 中的 [monobehavior wizard] 對話方塊。

Unity Project ExplorerUnity Project Explorer

Unity 專案總管會使用與 Unity 編輯器一樣的方式顯示您的所有 Unity 專案檔案與目錄。The Unity Project Explorer shows all of your Unity project files and directories in the same way that the Unity Editor does. 這與使用一般 Visual Studio 方案總管瀏覽 Unity 指令碼的方式不同,一般 Visual Studio 方案總管會將它們組織為專案與 Visual Studio 產生的方案。This is different than navigating your Unity scripts with the normal Visual Studio Solution Explorer, which organizes them into projects and a solution generated by Visual Studio.

  • 在 Visual Studio 主功能表上,選擇 [檢視] > [Unity 專案總管]。On the main Visual Studio menu, choose View > Unity Project Explorer. 鍵盤快速鍵: Alt + Shift + E  觀看 Unity Project Explorer 視窗。Keyboard shortcut: Alt+Shift+E View the Unity Project Explorer window.
  • 在 Visual Studio for Mac 中,當 Unity 專案開啟時,Solution Pad 會自動以這樣的行為。In Visual Studio for Mac, the Solution Pad automatically behaves like this when a Unity project is opened.

Unity 偵錯Unity debugging

Visual Studio Tools for Unity 可讓您使用 Visual Studio 的強大偵錯工具,同時對 Unity 專案的編輯器和遊戲指令碼進行偵錯。Visual Studio Tools for Unity lets you debug both editor and game scripts for your Unity project using Visual Studio's powerful debugger.

在 Unity 編輯器中偵錯Debug in the Unity editor

開始偵錯Start debugging

  1. 按一下標籤為 [附加至 Unity] 的 [播放] 按鈕,或使用鍵盤快速鍵 F5 ,以將 Visual Studio 附加至 Unity。Connect Visual Studio to Unity by clicking the Play button labeled Attach to Unity , or use the keyboard shortcut F5. 在 Visual Studio 中按一下 [播放]Click Play in Visual Studio
  1. 按一下 [播放] 按鈕,或者鍵入 Command + ReturnF5 ,以將 Visual Studio 連線至 Unity。Connect Visual Studio to Unity by clicking the Play button, or type Command + Return , or F5. ![按一下 播放于 Visual Studio for MacClick Play in Visual Studio for Mac
  1. 切換至 Unity,然後按一下 [播放] 按鈕,以在編輯器中執行遊戲。Switch to Unity and click the Play button to run the game in the editor.

在 Windows 上按一下 Unity 中的 [播放]

在 macOS 上按一下 Unity 中的 [播放]

  1. 如果遊戲在連線至 Visual Studio 時於 Unity 編輯器中執行,則遇到的任何中斷點都會暫停執行遊戲,並啟動遊戲在 Visual Studio 中叫用中斷點的一行程式碼。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.

停止偵錯Stop debugging

在 Visual Studio 中按一下 [停止] 按鈕,或使用鍵盤快速鍵 Shift + F5Click the Stop button in Visual Studio, or use the keyboard shortcut Shift + F5. 在 Visual Studio 中按一下 [停止]Click Stop in Visual Studio

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

若要深入了解如何在 Visual Studio 中進行偵錯,請參閱搶先了解 Visual Studio 偵錯工具To learn more about debugging in Visual Studio, see First look at the Visual Studio Debugger.

附加至 Unity 並試玩Attach to Unity and Play

如果要方便執行,您可以將 [附加至 Unity] 按鈕變更為 [附加至 Unity 並試玩] 模式。For added convenience, you can change the Attach to Unity button to Attach to Unity and Play mode.

  1. 按一下 [附加至 Unity] 按鈕旁的小型 下拉式箭頭Click the small down arrow next to the Attach to Unity button.
  2. 從下拉式功能表選取 [附加至 Unity 並試玩]。Select Attach to Unity and Play from the dropdown menu. 在 Visual Studio 中附加和播放Attach and play in Visual Studio

[播放] 按鈕的標籤會變成 [附加至 Unity 並試玩]。The play button becomes labeled Attach to Unity and Play. 除了附加 Visual Studio 偵錯工具之外,按一下此按鈕或使用鍵盤快速鍵 F5 現在會自動切換到 Unity 編輯器並在編輯器中執行遊戲。Clicking this button or using the keyboard shortcut F5 now automatically switches to the Unity editor and runs the game in the editor, in addition to attaching the Visual Studio debugger.

選擇 [附加至 Unity 並試玩] 組態,可直接從 Visual Studio for Mac 在單一步驟中啟動偵錯和播放 Unity 編輯器。Starting debugging and playing the Unity editor can be completed in a single step directly from Visual Studio for Mac by choosing the Attach to Unity and Play configuration.

選取 [附加至 Unity] 並在 Visual Studio for Mac 中播放

注意

如果您使用 [ 附加至 Unity] 和 [播放 ] 設定開始調試,[ 停止 ] 按鈕也會停止 Unity 編輯器。If you started debugging using the Attach to Unity and Play configuration, the Stop button will also stop the Unity Editor.

針對 Unity 玩家組建進行偵錯Debug Unity player builds

您可以使用 Visual Studio 來對 Unity 播放程式的開發組建進行 debug。You can debug development builds of Unity players with Visual Studio.

在 Unity 播放器中啟用指令碼偵錯Enable script debugging in a Unity player

  1. 在 Unity 中,選取 [檔案] > [組建設定]以開啟 [組建設定]。In Unity, open the Build Settings by selecting File > Build Settings.

  2. 在 [組建設定] 視窗中,標記 [開發建置] 和 [指令碼偵錯] 核取方塊。In the Build Settings window, mark the Development Build and Script Debugging checkboxes.

    設定用於偵錯的 Unity 組建設定。Configure the Unity build settings for debugging.

選取要附加偵錯工具的 Unity 執行個體Select a Unity instance to attach the debugger to

  • 在 Visual Studio 主功能表中,選擇 [偵錯] > [附加 Unity 偵錯工具]。In Visual Studio, on the main menu, choose Debug > Attach Unity Debugger.

    附加 Unity 的偵錯工具。Attach the debugger of Unity.

    [選取 Unity 執行個體] 對話方塊會顯示您可連接之每個 Unity 執行個體的一些資訊。The Select Unity Instance dialog displays some information about each Unity instance that you can connect to.

    選擇要連接之 Unity 的執行個體。Choose an instance of Unity to connect to.

    專案Project

    在這個 Unity 執行個體中執行的 Unity 專案名稱。The name of the Unity project that's running in this instance of Unity.

    機器 這個 Unity 執行個體執行所在的電腦或裝置名稱。Machine The name of the computer or device that this instance of Unity is running on.

    如果這個 Unity 執行個體當做 Unity Editor 的一部分來執行,則為 Type Editor ;如果這個 Unity 執行個體是獨立播放器,則為 PlayerType Editor if this instance of Unity is running as part of the Unity Editor; Player if this instance of Unity is a stand-alone player.

    連接埠 這個 Unity 執行個體用於通訊之 UDP 通訊端的通訊埠編號。Port The port number of the UDP socket that this instance of Unity is communicating over.

重要

由於 Visual Studio Tools for Unity 和 Unity 實例是透過 UDP 網路通訊端進行通訊,因此您的防火牆可能需要規則來允許它。Because Visual Studio Tools for Unity and the Unity instance are communicating over a UDP network socket, your firewall may need rule to allow it. 如有需要,您可能會看到提示,您必須授權連接,VSTU 和 Unity 才能進行通訊。If needed, you may see a prompt, you'll have to authorize the connection so that VSTU and Unity can communicate.

  • 在 Visual Studio for Mac 的上方功能表上,選擇 [ 執行] > 附加至進程 ]。In Visual Studio for Mac, on the top menu, choose Run > Attach to Process.
  • 在 [ 附加至進程 ] 對話方塊中,選取底部偵錯工具下拉式功能表中的 [ Unity 偵錯工具 ] 選項。In the Attach to Process dialog, select Unity Debugger option in the Debugger drop-down menu at the bottom.
  • 從清單中選取 Unity 實例,然後按一下 [ 附加 ] 按鈕。Select a Unity instance from the list and click the Attach button.

針對 Unity 專案中的 DLL 進行偵錯Debug a DLL in your Unity project

許多 Unity 開發人員會將程式碼元件撰寫成外部 DLL,以便與其他專案共用他們所開發的功能。Many Unity developers are writing code components as external DLLs so that the functionality they develop can be easily shared with other projects. Visual Studio Tools for Unity 可讓您順暢地連同 Unity 專案中的其他程式碼一起為這些 DLL 中的程式碼偵錯。Visual Studio Tools for Unity makes it easy to debug code in these DLLs seamlessly with other code in your Unity project.

注意

目前,Visual Studio Tools for Unity 僅支援 Managed DLL,At this time, Visual Studio Tools for Unity only supports managed DLLs. 並不支援對機器碼 DLL 進行偵錯 (例如以 C++ 撰寫的 DLL)。It does not support debugging of native code DLLs, such as those written in C++.

請注意,此處所述的情節假設您有原始程式碼;也就是說,您正在開發或重複使用自己的第一方程式碼,或您有協力廠商程式庫的程式碼,並打算將其部署至 Unity 專案中做為 DLL。Note that the scenario described here assumes that you have the source code—that is, you are developing or re-using your own first-party code, or you have the source code to a third-party library, and plan to deploy it in your Unity project as a DLL. 該情節不會說明如何對您沒有原始程式碼的 DLL 進行偵錯。This scenario does not describe debugging a DLL for which you do not have the source code.

為 Unity 專案所使用的 Managed DLL 專案偵錯To debug a managed DLL project used in your Unity project

  1. 將現有的 DLL 專案加入 Visual Studio Tools for Unity 所產生的 Visual Studio 方案。Add your existing DLL project to the Visual Studio solution generated by Visual Studio Tools for Unity. 在較不常見的情況下,您可能會啟動新的 Managed DLL 專案來包含 Unity 專案中的程式碼元件;如果是這種情況,您可以改為將新的 Managed DLL 專案加入 Visual Studio 方案。Less commonly, you might be starting a new managed DLL project to contain code components in your Unity project; if that's the case, you can add a new managed DLL project to the Visual Studio solution instead.

    將現有的 DLL 專案加入方案。Add your existing DLL project to the solution.

    不論是哪一種情況,Visual Studio Tools for Unity 都會保留專案參考 (即使必須再次重新產生專案和方案檔亦然),因此您只需要執行這些步驟一次。In either case, Visual Studio Tools for Unity maintains the project reference, even if it has to regenerate the project and solution files again, so you only need to perform these steps once.

  2. 參考 DLL 專案中的正確 Unity Framework 設定檔。Reference the correct Unity framework profile in the DLL project. 在 Visual Studio 的 DLL 專案屬性中,將 [目標 Framework] 屬性設定為您所使用的 Unity Framework 版本。In Visual Studio, in the DLL project's properties, set the Target framework property to the Unity framework version you're using. 這是與專案的目標應用程式開發介面相容的 Unity 基底類別庫,例如 Unity 完整、微型或 Web 基底類別庫。This is the Unity Base Class Library that matches the API compatibility that your project targets, such as the Unity full, micro, or web base class libraries. 如此可防止 DLL 呼叫存在於其他 Framework 或相容性層級中,但可能不存在於您所使用之 Unity Framework 版本的 Framework 方法。This prevents your DLL from calling framework methods that exist in other frameworks or compatibility levels, but which might not exist in the Unity framework version you're using.

注意

僅在使用 Unity 的舊版執行階段時才需要以下內容。The following is only required if you are using Unity's legacy runtime. 如果您使用的是新的 Unity 執行階段,則不再需要使用這些專用的 3.5 設定檔。If you are using the new Unity runtime, you don't need to use those dedicated 3.5 profiles anymore. 使用與 Unity 版本相容的 .NET 4.x 設定檔。Use a .NET 4.x profile compatible with your Unity version.

DLL 的目標架構設定為 Unity 架構。Set the DLL's target framework to Unity framework.

  1. 將 DLL 複製到 Unity 專案的 Assets 資料夾。Copy the DLL to your Unity project's Asset folder. 在 Unity 中,資產是與 Unity 應用程式一起封裝及部署的檔案,以便可以在執行階段載入。In Unity, assets are files that are packaged and deployed together with your Unity app so that they can be loaded at run-time. 由於 Dll 是在執行時間連結,因此必須將 Dll 部署為資產。Since DLLs are linked at run time, DLLs must be deployed as assets. 為了將 DLL 部署為資產,Unity Editor 會要求將 DLL 放在 Unity 專案的 [Assets] 資料夾中。To be deployed as an asset, the Unity Editor requires the DLLs to be put inside the Assets folder in your Unity project. 有兩種方式可讓您完成這個步驟:There are two ways you can do this:

    • 修改 DLL 專案的組建設定,以包含將輸出 DLL 和 PDB 檔案從其輸出資料夾複製到 Unity 專案之 [Assets] 資料夾的建置後工作。Modify the build settings of your DLL project to include a post-built task that copies the output DLL and PDB files from its output folder to the Assets folder of your Unity project.

    • 修改 DLL 專案的組建設定,將其輸出資料夾設定為 Unity 專案的 [Assets] 資料夾。Modify the build settings of your DLL project to set its output folder to be the Assets folder of your Unity project. DLL 和 PDB 檔案都會被放在 [Assets] 資料夾中。Both DLL and PDB files will be placed in the Assets folder.

    由於 PDB 檔案包含 DLL 的偵錯符號,並將 DLL 程式碼對應至其原始程式碼形式,因此偵錯時會需要這些檔案。The PDB files are needed for debugging because they contain the DLL's debugging symbols, and map the DLL code to its source code form. 如果您是以舊版執行階段為目標,Visual Studio Tools for Unity 將會使用 DLL 和 PDB 中的資訊來建立 DLL.MDB 檔案,這是舊版 Unity 指令碼引擎所使用的偵錯符號格式。If you are targeting the legacy runtime, Visual Studio Tools for Unity will use information from the DLL and PDB to create a DLL.MDB file, which is the debug symbol format used by the legacy Unity scripting engine. 如果您是以新的執行階段為目標,並使用可攜式 PDB,則 Visual Studio Tools for Unity 將不會嘗試執行任何的符號轉換,因為新的 Unity 執行階段是以原生方式使用可攜式 PDB。If you are targeting the new runtime, and using Portable-PDB, Visual Studio Tools for Unity will not try to do any symbol conversion as the new Unity runtime is able to natively consume Portable-PDBs.

    有關產生 PDB 的更多資訊,請參閱此處More information about PDB generation can be found here. 如果您是以新的執行階段為目標,請確定 [偵錯資訊] 設定為 [可攜式],以便正確地產生可攜式 PDB。If you are targeting the new runtime, please make sure that "Debugging Information" is set to "Portable", in order to properly generate Portable-PDB. 如果您是以舊版執行階段為目標,則需要使用 [完整]。If you are targeting the legacy runtime, you need to use "Full".

  2. 為程式碼偵錯。Debug your code. 您現在可以連同 Unity 專案的原始程式碼一起為 DLL 原始程式碼偵錯,並使用您慣用的所有偵錯功能,例如中斷點和逐步執行程式碼。You can now debug your DLL source code together with your Unity project's source code, and use all the debugging features you are used to, such as breakpoints and stepping through code.

鍵盤快速鍵Keyboard shortcuts

您可以使用鍵盤快速鍵快速存取 Unity Tools for Visual Studio 功能。You can quickly access the Unity Tools for Visual Studio functionality by using their keyboard shortcuts. 以下是可用的快速鍵摘要。Here's a summary of the shortcuts that are available.

命令Command 快速鍵Shortcut 快速鍵命令名稱Shortcut command name
開啟 MonoBehavior 精靈Open the MonoBehavior Wizard Ctrl +Shift +MCtrl+Shift+M EditorContextMenus.CodeWindow.ImplementMonoBehavioursEditorContextMenus.CodeWindow.ImplementMonoBehaviours
開啟 Unity Project ExplorerOpen the Unity Project Explorer Alt +Shift +EAlt+Shift+E View.UnityProjectExplorerView.UnityProjectExplorer
存取 Unity 文件Access Unity documentation Ctrl +Alt +M、Ctrl +HCtrl+Alt+M, Ctrl+H Help.UnityAPIReferenceHelp.UnityAPIReference
附加至 Unity 偵錯工具 (播放器或編輯器)Attach to Unity debugger (player or editor) 沒有預設值no default Debug.AttachUnityDebuggerDebug.AttachUnityDebugger

如果您不喜歡預設值,可以變更快速鍵組合。You can change the shortcut key combinations if you don't like the default. 如需如何變更它的詳細資訊,請參閱識別及自訂 Visual Studio 中的鍵盤快速鍵For information on how to change it, see Identify and customize keyboard shortcuts in Visual Studio.

命令Command 快速鍵Shortcut 快速鍵命令名稱Shortcut command name
開啟 MonoBehavior 精靈Open the MonoBehavior Wizard Cmd +Shift +MCmd+Shift+M EditorContextMenus.CodeWindow.ImplementMonoBehavioursEditorContextMenus.CodeWindow.ImplementMonoBehaviours
存取 Unity 文件Access Unity documentation Cmd + 'Cmd+' Help.UnityAPIReferenceHelp.UnityAPIReference

如果您不喜歡預設值,可以變更快速鍵組合。You can change the shortcut key combinations if you don't like the default. 如需如何變更它的詳細資訊,請參閱 自訂 IDEFor information on how to change it, see Customizing the IDE.