- Home /
Problems compiling DLL for use with Unity's current version (2019.2.0f1)
Trying to create a new DLL for use with Unity. I never had problems beyond minor-ones with this before, but now, after upgrading to the latest version of Unity, I’m unable to get some new DLL projects to compile. I follow, exactly the steps provided in the online documentation( https://docs.unity3d.com/Manual/UsingDLL.html ):
But I still got the following error when I try to compile it (not the main issue here):
The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?)
I ensured that the file C:\Program Files\Unity\Editor\Data\Managed\UnityEngine\UnityEngine.dll is present in the project references. (this is the path specified on docs page)
So I Replaced that reference with: C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll (different from docs, but how I had my previous DLL projects setup) and it worked. (Re-reading the docs, I see this was my mistake.. the docs DO have the correct path.)
Ok, great. Then I attempted to use “Selectable”, a class defined in the UnityEngine.UI namespace
First issue: the ONLY UnityEngine.UI.DLL I can find on my PC, is in my Unity project’s (oft deleted) “Library” folder, but when I add it as a reference, to the DLL project, I get a ton of warnings, and an error similar to the original error.
Warnings after adding UnityEngine.UI.dll reference:
The primary reference "UnityEngine.UI" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework
.
Error after adding UnityEngine.UI.dll reference:
The type 'MonoBehaviour' is defined in an assembly that is not referenced. You must add a reference to assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
Not clear on how the USER of my custom DLL supposed to get this file that now exists only in my project folder, not my “system” folders. Surely I’m not supposed to package the UnityEngine.UI.DLL file with my custom DLL, or am I? I also notice that if I remove the code from my project that uses UnityEngine.UI, and recompile the unity project, the UnityEngine.UI.DLL vanishes!
What’s going on here, why is the UnityEngine.UI.dll file not-permanent?
Why isn’t it compiling as expected?
What files SHOULD I be referencing in my DLL project and how is this determined?
What DLL files, if any, will I need to distribute with my DLL?
(Not sure if it's actually related to this issue: I found some references to "Package Manager" in the forums, I went in there, and DO see "Unity UI".. but the only option it provides me, is to "remove" it.)
Edit: code.. I didn't include it because it's so minimal.. but here it is just-incase:
using UnityEngine;
using UnityEngine.UI;
public class MonoDerived : MonoBehaviour
{
public Selectable selectableRef;
}
Answer by Bunny83 · Sep 18, 2019 at 03:46 PM
Well Unity has split a lot of the engine's core stuff into seperate DLLs in order to load those modules seperately. In the past there only was a single file (UnityEngine.dll). The new UI system always has been in a seperate dll file. All relevant files you should find in your corresponding Unity installation folder. If you use the Unity Hub it's somewhere along those:
C:\Program Files\Unity\Hub\Editor\2019.1.10f1\Editor\Data\UnityExtensions\Unity\GUISystem\UnityEngine.UI.dll
All the different modules for the version 2019.1.10f1 are located here:
C:\Program Files\Unity\Hub\Editor\2019.1.10f1\Editor\Data\Managed\UnityEngine\
As you might notice there are many dll files which have a name that ends in "Module". All those files essentially form the old UnityEngine.dll, just as seperate files. The Unity editor will reference and include the relevant dlls when you build your game depending on what classes your code actually uses.
When you build your own DLL you have to take care of referencing the modules you need. You can use a .NET reflector like ILSpy to figure out in which module which classes are located.
Note that many documentation pages have been updated and state at the top where this class is actually implemented. For example the Input class:
Input
class in UnityEngine / Implemented in:UnityEngine.InputLegacyModule
If in doubt you could simple create an empty Unity project and build your dll source files as part of that project in Unity. Inspect the resulting "assembly-csharp.dll" with ILSpy and see which dlls are referenced.
Good answer, as always, but I'm still not sure which dll's to include for "Selectable".. which seems to have been removed from the docs, at least, in version 2019.2. Oddly, it still shows up on the "manual" side of the docs- just not the API side. So- I cannot find which module dll to load for it. As a shot in the dark, I tried the UnityEngine.UI$$anonymous$$odule.dll (after removing UnityEngine.UI.dll reference)- but it still says "Selectable" and namespace UnityEngine.UI is not defined.
I also noticed that classes that previously USED to be derived from Selectable (like Button), are now derived from different "UIelement" type classes. EDIT: So, I also tried adding the UnityEngine.UIElements.dll reference, from the ...\managed\UnityEngine folder, but got the same undefined error.
ILSpy: I loaded every single file in the ....\managed\UnityEngine folder, and did a search- the Only "Selectable" I found was in some enumeration, and the ISelectable interface... I couldn't find the class itself. edit: oops- missed this part " Inspect the resulting "assembly-csharp.dll"" trying that now.
UIElements is a different / new / seperate UI system which is not based on gameobjects. So that "new" Button class is not part of the common UI system. I haven't used Unity recently but as far as I've seen UIElements is a mix of the old immediate mode GUI wrapped and optimised into distinct classes. It also allows to describe your UI through UX$$anonymous$$L. UIElements goes more into the direction of HT$$anonymous$$L content. See this blog post for more details
Answer by Glurth · Sep 18, 2019 at 05:44 PM
UPDATE: I ran into this problem again, and the solution I posted previously just wasn’t working.
Here is what I ended up doing:
created/had a normal UNITY project (not DLL project), which automatically generates VisualStudio Solution and Project files: these project files contain references to your code in the Asset Folder, and ALSO lists all the DLL references required.) These are usually called something like assembly-csharp.csproj, and assemblyEditor-csharp.csproj (in the root of the Unity project folder). They are normal text files, with an XML format. Open up these unity-generated .csproj files with NOTEPAD!!!
Copy ALL the lines/xml-elements where the file references other dll's (will probably be dozens of 'em, possibly in two or more sections)
Open up YOUR matching (editor or player) DLL's .csproj file in NOTEPAD. Then, paste into here, all those DLL references. Be careful, (backup first) it's all too easy to mess up the XML layout, when editing the file in notepad.
NOTE: Some of the references you copy/paste will reference the unity project’s Library folder e.g.
<Reference Include="UnityEditor.UI">
<HintPath>C:/Data/Unity Projects/MyProjectFolder/Library/ScriptAssemblies/UnityEditor.UI.dll</HintPath>
</Reference>
and some will reference the unity path in program files e.g.
<Reference Include="UnityEngine.InputModule">
<HintPath>C:/Program Files/Unity/Hub/Editor/2019.2.9f1/Editor/Data/Managed/UnityEngine/UnityEngine.InputModule.dll</HintPath>
</Reference>
*How to get the location of the DLL’s in the Libray path, BEFORE unity writes them in there, I still don’t know. Important: So, deleting the Library folder, WILL create DLL compilation problems, UNLESS you open it in Unity first, which will regenerate that Library folder.
*This is similar to what Bunny recommended in his answer, but is a little bit simpler/faster that using ILSPY to find the required references, and manually adding them in visual studio.
*I've learned since I posted the original answer: referencing additional DLL's in the project, ones that YOUR dll does not actually need, will NOT force them to be "required" by YOUR dll- only your actual code does that.
ORIGINAL POST: The resolution was, as usual, right there in the error text itself. (Guess I was too wrapped up in not understanding the DLL locations & thinking I had the wrong ones, to see it- see Bunny's excellent answer to help understand this.)
.Net versioning. I was able to get it working when I switched ALL project's setting to use .NET 4.0 compatibility.
In the Unity Editor, in Project Settings -> player, there is a drop-down to select "API Compatability Level".. I changed it from 2.0 (new project) to use ".NET4.0"
I also changed my DLL's csproj -> properties->application tab-> target framework dropdown, to ".NET Framework 4"
For references:
I used the UnityEngine.UI.dll (from the Library\ScriptAssembies Folder)
I used the UnityEngine.dll (from the C:\Program Files\Unity\Editor\Data\Managed Folder) This compiled
HOWEVER:
When I created a new class, DERIVED from selectable ,“SelectableDerived “, it generated an error saying I needed the UnityEngineCoreModule.dll
I added it, from the …data\Managed\UnityEngine folder, but that generated a conflict with the …data\Managed\UnityEngine.dll
So I removed that version’s refence, and referenced …data\Managed\UnityEngine\UnityEngine.dll instead (same folder as UnityEngine.CoreModule.dll)
So, now I’m using:UnityEngine.UI.dll (from the Library\ScriptAssembies Folder)
UnityEngine.dll (from the C:\Program Files\Unity\Editor\Data\Managed\UnityEngine Folder)
UnityEngine.CoreModule.dll (from the C:\Program Files\Unity\Editor\Data\Managed\UnityEngine Folder)
Now it compiles and generates a DLL file.
Great!! Let’s try it out… Create new project, pull in new DLL.
Warnings about obsolete API’s being used (can’t reproduce now)
Create an empty game object. Add “MonoDerived” component. No problem.
Add “SelectableDerived” component.. no such option… the class does not appear in the add component menu, nor under the DLL’s foldout (project folder)!
So I’m finally back to the “original” issue that sent me on this quest for how to compile a DLL in 2019, but guess I’ll create a new Question for this issue: Why aren’t my Selectable-derived Components, from DLLs, showing up?
Answer by lowepitts545 · Jun 12, 2020 at 10:02 AM
Managed plug-ins Managed plug-ins are managed .NET assemblies that you create using tools such as Visual Studio. They only contain .NET code. This means that they cannot access functions that do not support .NET libraries. The standard .NET tools that Unity uses to compile scripts can access the managed code. Therefore, there is no big difference in usage between managed plug-ins Code and Unity script code, except that plug-ins are compiled outside of Unity and the source may not be available.
Typically, Unity keeps scripts in a project as source files and compiles them when the source changes. However, you can use an external compiler to compile a script into a dynamically linked library (DLL). You can then add the DLL file to the project and attach the classes it contains to GameObjects just like normal scripts. A compiled DLL is called a managed plug-in in unity.
It is generally much easier to work with scripts than with DLLs in Unity. However, you may want to use compilers in your code that Unity does not support, or include third-party mono code in a .dll file, or provide Unity code without a source. Creating and adding a DLL file to your Unity project is the easiest way to get around these limitations.
Create a managed plug-in To create a managed plug-in, you need to create a DLL for which you need a suitable compiler. It is guaranteed that not all compilers that generate .NET code will work with Unity. Therefore, you should test the compiler with available code before you do extensive work with it. If the DLL does not contain any code that depends on the Unity API, you can use the appropriate compiler options to compile it into a DLL file.
If you want to use the Unity API, you have to provide the compiler with Unity's own DLLs. On macOS, the DLLs are included in the application package. To view it, locate the Unity.app file on your computer (Programs / Unity / Hub / Editor / [version number] /Unity.app), right-click on Unity.app and select Show Package Contents.
On macOS, the path to the Unity DLLs is:
/Applications/Unity/Unity.app/Contents/Managed/UnityEngine On Windows, the path to the Unity DLLs is:
C: \ Program Files \ Unity \ Editor \ Data \ Managed \ UnityEngine The UnityEngine folder contains the DLL files for a number of modules that you can reference to find the namespace you want. In addition, some namespaces require a reference to a compiled library from a Unity project (e.g. UnityEngine.UI), which is located in the directory of the project folder:
~ \ Library \ ScriptAssemblies The exact options for compiling the DLL depend on the compiler used. For example, the command line for the Mono C # compiler mcs on macOS might look like this:
mcs -r: /Applications/Unity/Unity.app/Contents/Managed/UnityEngine/UnityEngine.UIModule.dll -Target: Library ClassesForDLL.cs In this example, the -r option specifies a path to a library to be included in the build. In this case the UnityEngine.UIModule library. The -target option specifies which build type is required. The word "library" means a DLL build. Finally, the name of the source file to be compiled is ClassesForDLL.cs. The compiler assumes that this file is in the current working folder. However, you can use a full path to specify the file if necessary. The resulting DLL file is displayed in the same folder as the source file.
Use the managed plug-in Once you have compiled the DLL, you can drag the DLL file into the Unity project just like any other asset. The managed plug-in has a fold-out triangle that you can use to display the individual classes in the library. You can also add templatetoaster crack You can drag classes derived from MonoBehaviour onto game objects like normal scripts. You can use non-MonoBehaviour classes directly from other scripts as usual.
An expanded DLL with the visible classes An expanded DLL with the visible classes Step-by-step instructions for Visual Studio This section explains how to create and integrate a simple DLL sample in Visual Studio, and how to prepare a debugging session for the DLL.
Set up project Open Visual Studio and create a new project. Select File> New> Project and then Visual C #> Class Library.
Complete the following information for the new library:
Name is the namespace
,Managed plug-ins Managed plug-ins are managed .NET assemblies that you create using tools such as Visual Studio. They only contain .NET code. This means that they cannot access functions that do not support .NET libraries. The standard .NET tools that Unity uses to compile scripts can access the managed code. Therefore, there is no big difference in usage between managed plug-ins Code and Unity script code, except that plug-ins are compiled outside of Unity and the source may not be available.
Typically, Unity keeps scripts in a project as source files and compiles them when the source changes. However, you can use an external compiler to compile a script into a dynamically linked library (DLL). You can then add the DLL file to the project and attach the classes it contains to GameObjects just like normal scripts. A compiled DLL is called a managed plug-in in unity.
It is generally much easier to work with scripts than with DLLs in Unity. However, you may want to use compilers in your code that Unity does not support, or include third-party mono code in a .dll file, or provide Unity code without a source. Creating and adding a DLL file to your Unity project is the easiest way to get around these limitations.
Create a managed plug-in To create a managed plug-in, you need to create a DLL for which you need a suitable compiler. It is guaranteed that not all compilers that generate .NET code will work with Unity. Therefore, you should test the compiler with available code before you do extensive work with it. If the DLL does not contain any code that depends on the Unity API, you can use the appropriate compiler options to compile it into a DLL file.
If you want to use the Unity API, you have to provide the compiler with Unity's own DLLs. On macOS, the DLLs are included in the application package. To view it, locate the Unity.app file on your computer (Programs / Unity / Hub / Editor / [version number] /Unity.app), right-click on Unity.app and select Show Package Contents.
On macOS, the path to the Unity DLLs is:
/Applications/Unity/Unity.app/Contents/Managed/UnityEngine On Windows, the path to the Unity DLLs is:
C: \ Program Files \ Unity \ Editor \ Data \ Managed \ UnityEngine The UnityEngine folder contains the DLL files for a number of modules that you can reference to find the namespace you want. In addition, some namespaces require a reference to a compiled library from a Unity project (e.g. UnityEngine.UI), which is located in the directory of the project folder:
~ \ Library \ ScriptAssemblies The exact options for compiling the DLL depend on the compiler used. For example, the command line for the Mono C # compiler mcs on macOS might look like this:
mcs -r: /Applications/Unity/Unity.app/Contents/Managed/UnityEngine/UnityEngine.UIModule.dll -Target: Library ClassesForDLL.cs In this example, the -r option specifies a path to a library to be included in the build. In this case the UnityEngine.UIModule library. The -target option specifies which build type is required. The word "library" means a DLL build. Finally, the name of the source file to be compiled is ClassesForDLL.cs. The compiler assumes that this file is in the current working folder. However, you can use a full path to specify the file if necessary. The resulting DLL file is displayed in the same folder as the source file.
Use the managed plug-in Once you have compiled the DLL, you can drag the DLL file into the Unity project just like any other asset. The managed plug-in has a fold-out triangle that you can use to display the individual classes in the library. You can also be able to add templatetoaster crack You can drag classes derived from MonoBehaviour onto game objects like normal scripts. You can use non-MonoBehaviour classes directly from other scripts as usual.
An expanded DLL with the visible classes An expanded DLL with the visible classes Step-by-step instructions for Visual Studio This section explains how to create and integrate a simple DLL sample in Visual Studio, and how to prepare a debugging session for the DLL.
Set up project Open Visual Studio and create a new project. Select File> New> Project and then Visual C #> Class Library.
Complete the following information for the new library:
Name is the namespace
Answer by khanmira663 · Nov 18, 2020 at 05:09 AM
This thread is a great example of the members on the forums going that extra mile to help newbies. I remember gettiing similar help like this from Struck, Jessee Tony and others. forcrack crackred keyslog
Your answer
![](https://koobas.hobune.stream/wayback/20220612212323im_/https://answers.unity.com/themes/thub/images/avi.jpg)