I am using JetBrains
Rider to try and create a DLL
for use in a Unity3d
. I have a ClassLibrary
project with some of the classes inside that inherit from Monobehaviour
and I can't seem to find or link the Mono assembly properly in the references.
When I build the project I get this error :
TestScript.cs(8, 31): [CS0246] The type or namespace name 'Monobehaviour' could not be found (are you missing a using directive or an assembly reference?)
Can anyone help me out? Thanks.
Are you sure that you want to create a class library? Usually, code for Unity is added directly as C# script file assets, through the Unity interface, and all of these files live in the Assets
folder. Double clicking one of these files will start your external editor, such as Rider or Visual Studio, with a .csproj
generated for you that contains all the references you need.
If you want to split your Unity project into smaller, more manageable pieces, you can use .asmdef
Assembly Definition files. These also live in the Assets
folder, and essentially tell Unity that the folder they live in should be compiled into a separate assembly, and treated as a separate C# project.
You can get even better separation and reuse by creating a package. These live in the Packages
folder at the root of your project. Your package can contain multiple .asmdef
files that will be treated as multiple projects/assemblies. You can also add a package directly from a Git URL, such as GitHub.
The downside to creating a class library assembly for Unity is that you need to manually copy it into the Assets
folder before Unity will recognise it and use the code inside it. Assembly definition files and packages are usually a better way to solve this problem.
If you still want to create a Unity class library project, Rider will help with a project template - from the New Project dialog, you can select "Unity class library", and you can select the name of the project, the location, and more importantly, select the location of the UnityEngine.dll that will be added as a reference. Rider will find all of the Unity installs on your machine (as long as they are in the standard places) and give you a choice in a drop down.
If you've already got a class library that you want to add Unity support to, then you can add a reference to UnityEngine.dll
. You'll need to find the file, and that will be in a different location depending on what OS you have, and whether you've install Unity manually, or using Unity Hub. For example, on my machine, when I've installed Unity via Unity Hub, the file is at /Applications/Unity/Hub/Editor/2019.3.0f3/Unity.app/Contents/Managed/UnityEngine.dll
. On Windows, this will be at a similar path under Program Files.