使用Visual Studio 2019加快Unity中的编程工作流的10种方法

本文介绍了使用Visual Studio 2019提升Unity编程工作流效率的10个实用技巧,包括:调试器与Unity的连接,性能洞察,任务列表管理,代码片段,变量重命名,代码注释快捷键,GitHub集成,实时远程协作(Live Share),Unity消息模板代码创建,以及键盘快捷键。这些技巧可以帮助开发者更高效地编写、调试和协作开发Unity项目。
摘要由CSDN通过智能技术生成

Visual Studio 2019 offers world-class debugging, and lots of new tools and customization options so that you can set up your coding environment exactly the way you want it. It also comes with a range of powerful features for working with C# in Unity that helps you write and refactor code quicker than before. In this blog post, we will take a look at 10 tips on some of these features which may speed up your workflow too.

Visual Studio 2019提供了世界一流的调试功能,以及许多新工具和自定义选项,因此您可以完全按照自己的方式设置编码环境。 它还具有用于在Unity中使用C#的一系列强大功能,可帮助您比以前更快地编写和重构代码。 在此博客文章中,我们将介绍其中一些功能的10个技巧,这些技巧也可能会加速您的工作流程。

Our Unity evangelist Arturo Nereu and Abdullah Hamed, program manager for Visual Studio Tools for Unity at Microsoft, recently hosted a Unite Now session sharing tips and tricks on how to get the most out of Visual Studio 2019 when developing in Unity.

我们的Unity传播者 Arturo Nereu 和 Microsoft Visual Studio Tools for Unity的项目经理 Abdullah Hamed 最近 主持了一个Unite Now会话, 共享有关在Unity中进行开发时如何充分利用Visual Studio 2019的技巧和窍门。

演示地址

This post shows a quick overview of some of these tips. We also added links directly to those sections from the talk as well as other related content, if you want to dig deeper.

这篇文章展示了其中一些技巧的快速概述。 如果您想深入了解,我们还直接添加了指向演讲中这些部分以及其他相关内容的链接。

提示1:将调试器附加到Unity and Play (Tip 1: Attach the Debugger to Unity and Play)

Using Console.Log is an easy and quick way to help debug your project utilizing Unity’s console view. However, Visual Studio offers a more efficient way which becomes increasingly valuable as your project gets more complex. In Visual Studio, simply click the Attach to Unity button in the Visual Studio menu. This creates a connection between the two applications so that you can insert breakpoints and step through your code, while being in Play mode in Unity. You can also click Attach to Unity and play to start the execution without leaving your IDE. The beauty here is that it allows you to inspect the state of your code flow and values of the properties etc. at runtime. While this may seem trivial, being able to pause the execution at any time during gameplay and step through to inspect the specific state of the game and values of your properties in that exact frame, is a very powerful tool when debugging. 

使用 Console.Log 是使用Unity的控制台视图帮助调试项目的简便快捷方法。 但是,Visual Studio提供了一种更有效的方法,随着您的项目变得越来越复杂,它变得越来越有价值。 在Visual Studio中, 只需单击 Visual Studio菜单中的 附加到Unity” 按钮 。 这将在两个应用程序之间建立连接,以便您可以在Unity的“播放”模式下插入断点并单步执行代码。 您也可以单击 附加到Unity并播放 以开始执行而无需离开IDE。 这里的好处是,它允许您在运行时检查代码流的状态以及属性的值等。 尽管这似乎微不足道,但是在调试时,能够在游戏过程中随时暂停执行并逐步检查游戏的特定状态以及该属性在特定框架中的属性值是非常强大的工具。


Another handy option when working with breakpoints is that you can insert your own conditions with related actions such as a conditional expression which has to evaluate as true before applying in your debug flow. 


使用断点时的另一个方便的选择是,您可以 将自己的条件插入 相关操作中,例如条件表达式,在应用到调试流程之前必须将条件表达式评估为true。

提示2:获取性能见解和最佳实践建议 (Tip 2: Get performance insights and suggestions for best practices )

Visual Studio 2019 introduces Unity Analyzers. An analyzer works by detecting a code pattern and can offer to replace it with a more recommended pattern. Unity Analyzers are a collection of Unity-specific code diagnostics and code fixes that are open source and available on GitHub. Analyzers can provide you with a better understanding of Unity-specific diagnostics or simply help your project by removing general C# diagnostics that don’t apply to Unity projects. An example could be a simple conditional statement where you need to check if the GameObject has a specific tag to apply a certain behavior to it.

Visual Studio 2019引入了Unity分析器。 分析器通过检测代码模式来工作,并可以提出用更推荐的模式替换它。 Unity Analyzers是Unity特定的代码诊断和代码修复的集合,这些诊断和代码修复是开源的,可以在 GitHub上获得 。 分析器可以使您更好地了解Unity特定的诊断,或者通过删除不适用于Unity项目的常规C#诊断来帮助您的项目。 一个示例可能是一个简单的条件语句,您需要在其中检查GameObject是否具有特定标签以对其应用特定行为。

1

2
3
4
if(collision.gameObject.tag == "enemy")
{
// Logic being applied to enemy GO
}

1

2
3
4
if ( collision . gameObject . tag == "enemy" )
{
// Logic being applied to enemy GO
}

The analyzer would be able to analyze your code, will detect the pattern and offer to use the more optimized method instead. In this case, the analyzer would suggest the CompareTag method which is more efficient.

分析器将能够分析您的代码,将检测模式并提供使用更优化的方法来代替。 在这种情况下,分析人员会建议使用 CompareTag 方法, 该 方法效率更高。

1

2
3
4
if(collision.gameObject.CompareTag("enemy"))
{
// Logic being applied to enemy GO
}

1

2
3
4
if ( collision . gameObject . CompareTag ( "enemy" ) )
{
// Logic being applied to enemy GO
}

While the above example represents a minor optimization tweak with no significant impact in a single script attached to a single GameObject, this may be different for a large scale project with 1000s of GameObjects with the script attached. It’s the sum of all parts when looking into performance optimization and Analyzers can make it easy to help you identify and improve your performance simply by reducing the unneeded overhead by optimizing the code syntax. 

尽管上面的示例表示次要的优化调整,但对单个游戏对象附加的单个脚本没有显着影响,但对于具有数千个游戏对象并附加了脚本的大型项目,这可能会有所不同。 它是研究性能优化时所有部分的总和,分析器可以轻松地通过优化代码语法来减少不必要的开销,从而帮助您识别和提高性能。

You can also find a list of the analyzers here and if you are interested in learning more visit this blog post or jump directly to this part of the Unite Now talk

您还可以在 此处 找到 分析仪 列表, 如果您想了解更多信息,请访问此 博客文章, 或直接跳至 Unite Now对话的 这一部分 。

提示3:将“任务列表”用作后续检查清单 (Tip 3: Use the Task List as a followup checklist )

A common challenge when creating your scripts is the need to come back at a later point and revisit the code. That might be a result of implementing code snippets which eventually will need refactoring for better performance later but serves the current needs as you are f.x. testing out game mechanics. Visual Studio has a handy feature to keep track of this called Task List which allows you to track code comments that use tokens such as TODO and HACK, or even custom tokens. You can also manage shortcuts that take you directly to a predefined location in code. To create a task for later simply add the token in your code: 

创建脚本时的一个常见挑战是需要稍后再回来重新访问代码。 这可能是由于实现了代码片段,最终需要重构以提高性能,但是在您测试游戏机制时可以满足当前需求。 Visual Studio具有方便的功能来跟踪此称为“ 任务列表 ”的 任务 ,它使您可以跟踪使用令牌(例如TODO和HACK甚至自定义令牌)的代码注释。 您还可以管理将您直接带到代码中预定义位置的快捷方式。 要为以后创建任务,只需在代码中添加令牌:

1

// TODO: Change the collision detection once new colliders are ready

1

// TODO: Change the collision detection once new colliders are ready

The Task List window, which you can access from under View in the menu, gives you an overview of all the tasks you tagged and links you to those specific parts of the code in just one click.  

您可以从 菜单的“ 视图” 下访问“任务列表”窗口,该窗口 概述了您标记的所有任务,只需单击一下即可将您链接到代码的那些特定部分。

Adding a comment followed by the // TODO: in your code makes it easy to “tag” action items you want to follow up on at a later point using the Task List window as shown in the above on the right-hand side.

在代码中添加一个注释,然后在// TODO:之后添加代码,可以很容易地使用“任务列表”窗口对以后要跟进的动作项目进行“标记”,如上右侧所示。

As the list of action items in your project grows, you can even configure your own custom tokens in the task list and assign priorities and organizing for your refactoring process effectively. To customize your Task List tokens: go to Tools > Options.

随着项目中操作项列表的增加,您甚至可以在任务列表中配置自己的自定义标记,并为重构过程有效地分配优先级和组织。 要自定义您的任务列表令牌:转到 工具 > 选项


See the full example in the Unite Now session here.


请参阅 此处的 立即团结会议中 的完整示例 。

提示4:使用摘要加快您的工作流程 (Tip 4: Use snippets to speed up your workflows)

Code snippets are small blocks of reusable code that can be inserted in a code file using a right-click menu (context menu) command or a combination of hotkeys. They typically contain commonly used code blocks such as try-finally or if-else blocks, but you can also use them to insert entire classes or methods. In short, they offer you a handy way to save a lot of time by creating the boilerplate code for you.
To surround your code with a snippet such as a namespace or region, press CTRL + K + S. That allows you to apply the snippet as demonstrated in the video below:

代码段是可重用代码的小块,可以使用右键单击菜单(上下文菜单)命令或热键组合将其插入代码文件中。 它们通常包含常用的代码块,例如try-finally或if-else块,但是您也可以使用它们来插入整个类或方法。 简而言之,通过为您创建样板代码,它们为您提供了一种节省大量时间的便捷方法。
要用代码段(例如名称空间或区域)包围代码,请按CTRL + K +S。这样您就可以按照以下视频中的说明应用代码段:


You can find a step-by-step walkthrough of creating your own code snippets in Microsoft’s documentation (Visual Studio, Visual Studio for Mac).


您可以在Microsoft文档( Visual StudioVisual Studio for Mac )中 找到创建自己的代码段的分步指南 。

提示5:只需单击几下即可重命名所有变量 (Tip 5: Rename all variables in just a few clicks)

A common workflow when you are refactoring your code is renaming your variables to more descriptive and accurate names. Changing it one place obviously means you also have to fix all references to that variable. However, Visual Studio offers an easy shortcut to do this in one operation. Simply highlight the name of the variable you want to rename and right-click (or use the keyboard shortcut CTRL + R) and then rename the variable. Select preview changes to review the implications of the change before applying.

重构代码时,常见的工作流程是将变量重命名为更具描述性和准确性的名称。 更改一个位置显然意味着您还必须修复对该变量的所有引用。 但是,Visual Studio提供了一种简单的快捷方式,可以一次完成此操作。 只需突出显示要重命名的变量的名称,然后右键单击(或使用键盘快捷键CTRL + R),然后重命名该变量。 选择 预览更改 以在应用之前查看 更改 的含义。


You can use the same tip for changing the classes of your script but remember you have to rename the C# file accordingly to avoid the compilation errors. Learn more about the class renaming flow this part of the Unite Now session.


您可以使用相同的技巧来更改脚本的类,但是请记住,必须相应地重命名C#文件,以避免编译错误。 在Unite Now会话的 这一部分 中 了解有关类重命名流程的更多信息 。

提示6:使用键盘快捷键注释掉您的代码 (Tip 6: Comment out your code with a keyboard shortcut)

Commenting or uncommenting blocks of code is another common workflow when refactoring or debugging your code. It can be a time-consuming task when you do it one line at the time. Visual Studio, however, allows you to comment out entire blocks of code using a simple shortcut command: Ctrl+K+C and Ctrl+K+U for uncommenting it again. If you are on Mac, simply use CMD+K+C to comment out and CMD+K+U to remove the comments again.

在重构或调试代码时,注释或取消注释代码块是另一种常见的工作流程。 如果您一次只执行一行,则可能是一项耗时的任务。 但是,Visual Studio允许您使用简单的快捷命令注释掉整个代码块:Ctrl + K + C和Ctrl + K + U,以再次取消注释。 如果您使用的是Mac,只需使用CMD + K + C注释掉,然后使用CMD + K + U再次删除注释即可。


Being able to comment out entire blocks quickly can be an efficient way to suppress specific game logic during your testing workflows.


能够快速注释掉所有块是在测试工作流程中抑制特定游戏逻辑的有效方法。

技巧7:只需单击几下即可与GitHub集成 (Tip 7: Set up integration with GitHub in a few clicks)

While Unity Collaborate makes it easy to save, share, and sync your project with others directly from Unity with a user-friendly visual interface, some teams and developers prefer using source control solutions such as GitHub. Working with GitHub for source control is now much easier with the Github for Unity plugin. The extension is completely open source and it allows you to view your project history, experiment in branches, craft a commit from your changes, and push your code to GitHub without leaving Unity. The GitHub authentication is embedded in Unity, including 2FA and with a click of a button, you can quickly initialize your game’s repository without having to use command-line instructions. It allows you to create a Unity specific .gitignore file so you don’t have to set it up manually. With Visual Studio 2019 also comes a new interface which makes it even easier to work with GitHub directly in the IDE.

虽然 Unity Collaborate 可以使用用户友好的可视界面直接从Unity轻松保存,共享和与他人同步项目,但是某些团队和开发人员更喜欢使用GitHub等源代码控制解决方案。 现在,通过 Github for Unity 插件, 与GitHub进行源代码控制变得更加容易 。 该扩展是完全开源的,它允许您查看项目历史记录,在分支机构中进行实验,根据所做的更改进行提交以及将代码推送到GitHub,而无需离开Unity。 GitHub身份验证嵌入在Unity中,包括2FA,只需单击一个按钮,您就可以快速初始化游戏的存储库,而无需使用命令行说明。 它允许您创建一个特定于Unity的.gitignore文件,因此您不必手动设置它。 Visual Studio 2019还提供了一个新界面,使直接在IDE中使用GitHub变得更加容易。


To activate the new interface in Visual Studio: Go to Tools > Options > Environment > Preview features > New Git user experience.


要在Visual Studio中激活新界面:转到 工具>选项>环境>预览功能> New Git用户体验。

You can also follow along with the video instructions from the Unite Now session, which shows a more in-depth walkthrough of getting started.

您还可以按照 Unite Now会话中 的视频说明进行操作 ,其中显示了更深入的入门指南。

提示8:使用Live Share进行实时远程协作 (Tip 8: Collaborate remotely in real-time with Live Share)

Live Share enables you to share your instance of Visual Studio directly with your teammate using just a link, allowing them to edit your code and collaborate on your project in real-time. You don’t have to clone a repo or set up the environment first in order to do the sharing. You both just need to have Visual Studio installed and then it’s as easy as clicking a button to create the Live Share session. 

通过Live Share,您可以仅通过链接就可以直接与队友共享Visual Studio实例,从而使他们可以编辑代码并在项目上进行实时协作。 您无需克隆存储库或先设置环境即可进行共享。 你们都只需要安装Visual Studio,然后只需单击一个按钮即可创建Live Share会话。

Visual Studio Live Share allows you to share your Visual Studio session and collaborate with others directly on the code in real time.

Visual Studio Live Share允许您共享Visual Studio会话,并直接在代码上与其他人直接协作。

To get started simply select Live Share to generate a link to the parts of your code that you want to share with anyone that has Visual Studio or Visual Studio Code installed. A sharing session is created between you and your collaborators, allowing them to see your code without having to install anything except for the editor. It works almost instantly. 

要开始使用,只需选择“ 实时共享” 即可生成指向您要与安装了Visual Studio或Visual Studio Code的任何人共享的代码部分的链接。 您和您的协作者之间将创建一个共享会话,使他们可以看到您的代码而无需安装除编辑器以外的任何内容。 它几乎立即起作用。

You can learn more about Live Share from our Unite Session here, visit the Visual Studio product page or jump directly to the Quickstart guide here.

您可以 在此处Unite会话中 了解有关Live Share的更多信息 ,访问 Visual Studio产品页面 或直接跳至 此处 的 快速入门指南

提示9:轻松为Unity Messages创建样板代码 (Tip 9: Create boilerplate code easily for Unity Messages)

Remembering the signature of all the MonoBehaviour methods is tricky and while the Unity documentation will have you covered, Visual Studio provides a neat feature that allows you to look it up directly in the IDE. Simply click CTRL + Shift + M, search for the function you would like to implement, and filter through the search result to find the method. Select the checkbox and click Ok to insert the boilerplate code for the method directly in your code ready for you to use.

记住所有MonoBehaviour方法的签名都是很棘手的,虽然涵盖了Unity文档,但Visual Studio提供了一项简洁的功能,可让您直接在IDE中进行查找。 只需单击CTRL + Shift + M,搜索您要实现的功能,然后筛选搜索结果以找到方法。 选中该复选框,然后单击“确定”以将方法的样板代码直接插入您的代码中以备使用。

提示10:键盘快捷键 (Tip 10: Keyboard shortcuts)

Several of the above tips are available with handy shortcuts and at the end of the day, knowing those shortcuts may be the biggest timesaver of them all. So let’s wrap up the list with a summary of the keyboard shortcuts to these tips and a few more as a bonus.

上面的一些技巧可通过方便的快捷方式获得,并且在一天结束时知道这些快捷方式可能是它们中最大的节省时间。 因此,让我们用这些提示的键盘快捷键的总结来总结一下清单,并附上一些额外的奖励。

WhatWindowsMac
Search your entire project for anything.Use CTRL+T CMD + .
Implement Unity MessagesCTRL + Shift + M CMD + Shift + M 
Comment out code blocksCTRL + K  / CTRL + CCMD + /
Uncomment blocks of codeCTRL + K  / CTRL + U CMD + /
Copy from clipboard historyCTRL + Shift + V
View task listCTRL + TNo default keybinding, but you can bind it.
Insert a surrounding snippet such as namespaceCTRL + K + S:No default keybinding, but you can bind it.
Renaming a variable while updating all referencesCTRL + RCMD +R
Compile the code CTRL+SHIFT+B CMD + Shift + B
什么 视窗 苹果电脑
在整个项目中搜索任何内容。 使用CTRL + T CMD +。
实施Unity消息 CTRL + Shift + M CMD + Shift + M
注释掉代码块 CTRL + K / CTRL + C CMD + /
取消注释代码块 CTRL + K / CTRL + U CMD + /
从剪贴板历史记录复制 CTRL + Shift + V
查看任务清单 CTRL + T 没有默认的键盘绑定,但是您可以绑定它。
插入周围的代码段,例如名称空间 CTRL + K + S: 没有默认的键盘绑定,但是您可以绑定它。
更新所有引用时重命名变量 CTRL + R CMD + R
编译代码 Ctrl + Shift + B CMD + Shift + B

分享您的提示? (Share your tips?)

Visual Studio 2019 is packaged with features and there are so many customization options that can enhance your productivity working with Unity depending on your specific workflows. There’s too many to cover them all here. We hope that the few tips that we shared here will inspire you to dive in and that you’re finding the format useful. Let us know if you have any tips we didn’t cover, and feel free to share them with the community in the comments. We’d also love to hear if you would like more tips and tricks on how to improve your workflows in Unity and if there are any topics, in particular, that you would like to have covered in a future blog post.

Visual Studio 2019随附了功能,并且有许多自定义选项可以根据您的特定工作流程提高使用Unity的效率。 这里太多了,无法涵盖所有​​内容。 我们希望我们在这里分享的一些技巧会启发您学习,并希望您找到有用的格式。 如果您有我们未涵盖的任何提示,请告诉我们,并随时在评论中与社区分享。 我们也很想听听您是否想要更多有关如何改善Unity工作流的提示和技巧,以及是否有任何特别的话题,特别是您希望在以后的博客文章中介绍。

获得有关如何改进Visual Studio的反馈? (Got feedback on how Visual Studio could be improved?)

We are constantly working on improving the workflows and our teams are working closely with Microsoft in terms of giving you the best IDE experience. Hence we would love to hear from you if you have any ideas or feedback. Feel free to ping John Miller at @jmillerdev, Senior Program Manager, Visual Studio Tools for Unity at Microsoft, or share your feedback with us in our Scripting forum.

我们一直在不断改进工作流程,我们的团队与Microsoft紧密合作,以为您提供最佳的IDE体验。 因此,如果您有任何想法或反馈,我们希望能收到您的来信。 随时可以通过 @jmillerdev的 ping John Miller, Microsoft的Visual Studio Tools for Unity的高级程序经理,或者在我们的 脚本论坛上 与我们分享您的反馈 。

翻译自: https://blogs.unity3d.com/2020/07/14/10-ways-to-speed-up-your-programming-workflows-in-unity-with-visual-studio-2019/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值