简体   繁体   中英

Unity type or namespace name 'InputSystem' does not exist in the namespace 'UnityEngine'

I have installed the new InputSystem package in my Unity project. I have set the input to "new inputsystem" in the Unity Player Settings. I have quit and restarted both Unity and VS Code.

But when I try to use the system in VS Code, I immediately get this error:

using UnityEngine;
using System.Collections;
using UnityEngine.InputSystem;              // <-error
using UnityEngine.InputSystem.Controls;     // <-error

The type or namespace name 'InputSystem' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?) [Assembly-CSharp]

What can I do to make VS Code recognise the Unity InputSystem package?

The issue is that the required .csproj files (VSCode needs them to understand the available libraries) for the new package were not auto-generated when you installed the new package (it didn't happen for me either, maybe it is intentionally manual).

Go to Edit -> Preferences... and then click on External Tools . You need to tick the box to Generate .csproj files for: Registry packages (and any other ones you might want) and then click on Regenerate project files .

Unity 首选项对话框的屏幕截图

That should generate whatever is needed for VSCode to recognize the new package - you shouldn't even need to restart it.

You need to install the package. Go to Windows / Package Manager. Search Input system and press install confirm to restart and replace the old input system.

选择您的输入操作资产,并从检查器窗口中打开生成 C# 类,然后单击应用以生成 C# 脚本文件。

There's multiple reasons for this issue

I had an existing Assembly Definition that needed an Assembly Definition Reference to Unity.InputSystem

项目视图中的装配定义

I added an Assembly Definition Reference to Unity.InputSystem in the inspector for "Scripts"

检查器中的程序集定义,引用了 Unity.InputSystem

Rebuild had 0 errors.

I had the same issue (Mac 10.15.17 with Unity 2019.3.151f1 personal) but the above didn't work 100% for me. Here was my solution:

First, I ensured that I installed the InputSystem package via the package manager. Window > Package Manager. Search for it and you should be able to install 1.0.2 which is the newest as of this post.

From there, I ensured that VS code was setup correctly. I when to Unity > Preferences > External Editor and ensured that VS code was the correct editor. I then selected Regenerate project files.

Lastly, on the script I was using, I included it in the top of the file like so:

using UnityEngine;
using UnityEngine.InputSystem;

关闭 Unity 和 VB,从项目文件夹 *.sln *.csproj 中删除这些文件,运行 unity 然后单击 Assets -> open C# project

I came across the same issue, and solved it. so, leaving a note here.

my case

In my case, after installing the new InputSystem package by Package Manger on Unity Editor, and then restarting Unity Editor following the appeared dialog, I could see Input System under Packages folder on Project view. However, any reference to namespece UnityEngine.InputSystem got errors.

What I found that Unity.InputSystem.dll didn't exist under [Project Dir]/Library/ScriptAssemblies/Unity.InputSystem.dll , which is what the Unity project refers to. This path can be found in Assembly-CSharp.csproj , placed right under Unity project directory.

Actually, it was not only for Input System package but also the same for all the other packages I uses. [Project Dir]/Library/ScriptAssemblies only had 2 files, Assembly-CSharp.dll and Assembly-CSharp.pdb , although this directory should hold dll files from all the installed packages.

solution

My solution was simple.

  1. delete Assembly-CSharp.csproj file ( to be regenerated at some stage )
  2. close Unity Editor
  3. close Unity Hub
  4. open Unity Hub
  5. open Unity Editor

At the last step, before Unity Editor fully waked up, I could see all the package dll files are loaded to [Project Dir]/Library/ScriptAssemblies directory.

I'm not so sure whether csproj file delete and Unity Hub restart was necessary, but this is what I did, which recovered my broken project.

my environment

  • Unity 2019.4.14f1
  • macOS 10.15.7
  • Visual Studio 8.8.4

In my case (Windows, Unity 2019.4 15f1, Visual Studio Code 1.53.0, Input System 1.0.2) Setting the Architecture of the project to x86_64 helped.

File-> Build Settings -> Plattform (PC, MAC & Linux Standalone)

I'm not sure though if this could even effect VS Code or if I did anything else in between to resolve this.

如果你想使用类似 inputField 或 Etc 之类的东西,你可以通过添加来做到

using UnityEngine.UI;

In my case I was creating my Script in a third-party folder (Mirror). As soon as I moved it to my Scripts folder it worked.

I don't know why but I did remove the tick and regenerate project files and it worked (no need to restart VS Code)
when I put the tick back on the error appears again :
enter image description here
ps: VS Code version: 1.61.2 , Input System Package version: 1.2.4

If you get errors when Unity is trying to resolve the dependencies for the new Input System.Follow these steps and it should work: Regenerating your project only works, if you have the Input System actually installed.

  1. Install Input System (Windows > Package Manager)

在此处输入图片说明

  1. Set your project to new Input System (Project Settings > Player > Input System)

在此处输入图片说明

  1. Regenerate Project Files

Like other answers suggested, try regenerating your project files. This will only work, if the previous steps were succesful.

在此处输入图片说明

I did all the above and still had the problem. What finally, helped me was:

Changing VS Code package to 1.2.3. You can do that by going to:

Packages/Manifest.json

finding:

"com.unity.ide.vscode"

and changing version from "1.2.4" to "1.2.3"

The original solution was posted by user Cartina: https://forum.unity.com/threads/cannot-find-unityengine-inputsystem.807645/

I had the same issue and nothing seemed to work, tried everything in this thread. What fixed it was installing the new Unity HUB 3.0.0.

I have no idea why, but after that all the errors were gone. Perhaps the HUB was somehow corrupted and reinstalling it fixed things.

Win 10
Unity 2021.2.7f1
VS Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM