在 Visual Studio 中编辑的 Unity 脚本不提供自动完成功能 [英] Unity Scripts edited in Visual studio don't provide autocomplete

查看:43
本文介绍了在 Visual Studio 中编辑的 Unity 脚本不提供自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想编辑 C# Unity 脚本时,它们会在 Visual Studio 中打开.它应该为所有与 Unity 相关的代码提供自动完成功能,但它不起作用.

在这里您可以看到缺少的功能:

如所见,

杂项文件"消息可能有多种原因:

  1. 当您从另一个文件夹打开 Unity C# 文件而不是从 Unity 编辑器打开它时,可能会发生这种情况.

  2. 这也可能是因为 Unity 在 Visual Studio 仍处于打开状态时崩溃,从而损坏了一些文件.

  3. 这可能是因为 Unity 已关闭然后重新打开但不再连接到 Visual Studio.当 Visual Studio 打开时,您会看到杂项文件"并且没有自动完成.

  4. 如果未安装 Visual Studio Tools for unity,就会发生这种情况.

  5. 当您从 Unity 创建脚本时,请在 Unity 完成处理之前或圆形图标动画完成动画之前快速打开它.


大多数情况下,重新启动 Unity 和 Visual Studio 应该可以解决这个问题.

我不知道是哪一个导致了问题,但我会介绍最有可能解决此问题的解决方案.

修复第 1 部分:


修复第 2 部分:

如果新创建的 C# 文件以杂项的形式出现,请按照以下说明进行操作:

  1. 从 Visual Studio,转到 工具选项...Unity 工具杂项.在显示连接图标下,将其设置为true,然后重新启动 Visual Studio.

  2. 当您重新启动时,Visual Studio 中现在应该可以使用连接图标.单击它,然后选择要连接的 Unity 实例.红色的x"图标现在应该变成棕色的复选标记图标.现在,当你在 Unity 中创建一个新的 C# 文件时,它应该打开而不说 Miscellaneous.


修复第 3 部分:

还没修好?

重新导入项目,然后打开 C# 项目.

  1. 关闭 Visual Studio.

  2. 从 Unity,通过转至 AssetsReimport All 重新导入项目.

  3. 现在,通过转到资产打开 C# 项目,在 Visual Studio 中打开项目.这将重新加载项目并修复可能的解决方案文件问题.


修复第 4 部分:

还没修好?

单独修复每个 C# 文件.

  1. 点击显示所有文件图标.

  2. 选择不自动完成的脚本,然后右键单击并选择包含在项目中.


修正第 5 部分:

还没修好?

归功于 chrisvarnz 对于这个似乎适用于多人的特定解决方案.

  1. 关闭 Visual Studio

  2. 转到您的项目目录并删除所有生成的 Visual Studio 文件.

    这些是要删除的文件扩展名:

    • .csproj
    • .user
    • .sln

    示例:

    假设您的项目名称为 Target_Shoot,这些是要删除的文件的样子:

    • Target_Shoot.csproj
    • Target_Shoot.Editor.csproj
    • Target_Shoot.Editor.csproj.user
    • Target_Shoot.Player.csproj
    • Target_Shoot.Player.csproj.user
    • Target_Shoot.sln

    不要删除任何其他内容.

  3. 再次从 Unity 双击脚本,这应该会生成新的 Visual Studio 文件,然后打开 Visual Studio.这可能会解决您的问题.


修复第 6 部分:

如果不起作用,请检查您是否遇到​​此错误:

<块引用>

GetReferenceNearestTargetFrameworkTask"未找到任务

  1. 此处Nuget PackageManager>.

  2. 重新启动 Visual Studio.

有关详细信息,请参阅答案.

When I want to edit C# Unity scripts, they open in Visual Studio. It is supposed to provide auto complete for all Unity related code, but it doesn't work.

Here you can see the missing functionality:

As seen, the transform object does not open the autocomplete menu.

Unity version: 5.5.2f1
Visual studio 2015
Visual Studio Tools for Unity is installed

解决方案

There is no auto-completion because the script says "Miscellaneous Files" instead of the of the name of the Project. Take a look at the image below that came from the video in your question:

The "Miscellaneous Files" message can happen for many reasons:

  1. It can happen when you open your Unity C# file from another folder instead of opening it from Unity Editor.

  2. This can also happen because Unity crashed while Visual Studio is still open therefore corrupting some files.

  3. It can happen because Unity was closed then re-opened but is no longer connected to Visual Studio. When Visual Studio is opened you get "Miscellaneous Files" and no auto-completion.

  4. This can happen when Visual Studio Tools for unity is not installed.

  5. When you create a script from Unity then quickly open it before Unity finish processing it or before the round icon animation stuff finish animating.


Most of the times, restarting Unity and Visual Studio should fix this.

I can't tell which one is causing the problem but I will cover the most likely solution to fix this.

Fix Part 1:

  1. Download and Install Visual Studio Tools for unity from this link. Do this while Unity and Visual Studio are both closed.

  2. From Unity Editor, go to EditPreferences... → External Tools. On the External Script Editor drop down menu, change that to Visual Studio 2015.


Fix Part 2:

If newly created C# files are coming up as Miscellaneous then follow the instruction below:

  1. From Visual Studio, go to ToolsOptions...Tools for UnityMiscellaneous. Under Show connectivity icon, set it to true then restart Visual Studio.

  2. When you re-start, connection icon should now be available in Visual Studio. Click it then choose the Unity instance to connect to. The red 'x' icon should now turn into a brown checkmark icon. Now, when you create a new C# file in Unity, it should open without saying Miscellaneous.


Fix Part 3:

Still not fixed?

Re-import project then open C# Project.

  1. Close Visual Studio.

  2. From Unity, re-import project by going to AssetsReimport All.

  3. Now, open the project in Visual Studio by going to AssetsOpen C# Project. This will reload the project and fix possible solution file problems.


Fix Part 4:

Still not fixed?

Fix each C# file individually.

  1. Click on Show All Files icon.

  2. Select the script that doesn't do auto-complete then right-click and select Include In Project.


Fix Part 5:

Not fixed yet?

Credit goes to chrisvarnz for this particular solution which seems to have worked for multiple people.

  1. Close Visual Studio

  2. Go your project directory and delete all the generated Visual Studio files.

    These are the files extensions to delete:

    • .csproj
    • .user
    • .sln

    Example:

    Let's say that the name of your Project is called Target_Shoot, these are what the files to delete should look like:

    • Target_Shoot.csproj
    • Target_Shoot.Editor.csproj
    • Target_Shoot.Editor.csproj.user
    • Target_Shoot.Player.csproj
    • Target_Shoot.Player.csproj.user
    • Target_Shoot.sln

    Do not delete anything else.

  3. Double click on the script again from Unity which should generate new Visual Studio file then open Visual Studio. This may solve your problem.


Fix Part 6:

If not working, check if you are having this error:

The "GetReferenceNearestTargetFrameworkTask" task was not found

  1. Install Nuget PackageManager from here.

  2. Restart Visual Studio.

See this answer for more information.

这篇关于在 Visual Studio 中编辑的 Unity 脚本不提供自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆