Managed plug-insA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary are managed .NET assemblies that you create with tools like Visual Studio. They contain only .NET code which means that they can’t access any features that .NET libraries do not support. However, the standard .NET tools that Unity uses to compile scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary can access the managed code. Therefore, there isn’t a lot of difference in usage between managed plug-inA managed .NET assembly that is created with tools like Visual Studio for use in Unity. More info
See in Glossary code and Unity script code, except that plug-ins are compiled outside of Unity and so the source might not be available.
Usually, Unity keeps scripts in a Project as source files and compiles them whenever the source changes. However, you can use an external compiler to compile a script to a dynamically linked library (DLL). You can then add the .dll file to the Project and attach the classes it contains to GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary just like normal scripts. A compiled DLL is known as a managed plug-in in Unity.
It’s generally much easier to work with scripts than DLLs in Unity. However, you might want to use compilers in your code that Unity doesn’t support, or add third party Mono code in a .dll file, or you might want to supply Unity code without the source. Creating and adding a .dll file to your Unity Project is the easiest way to get around these limitations.
To create a managed plug-in, you need to create a DLL, which you need a suitable compiler for. Not all compilers that produce .NET code are guaranteed to work with Unity, so you should test the compiler with some available code before doing significant work with it. If the DLL contains no code that depends on the Unity API then you can use the appropriate compiler options to compile it to a .dll file.
If you do want to use the Unity API then you need to make Unity’s own DLLs available to the compiler. On macOS, the DLLs are contained in the application bundle. To see them, find the Unity.app
file on your computer (Applications/Unity/Hub/Editor/[Version Number]/Unity.app
) and then right click on Unity.app
and select Show Package Contents.
On macOS the path to the Unity DLLs are:
/Applications/Unity/Hub/Editor/<version-number>/Unity.app/Contents/Managed/UnityEngine
On Windows the path to the Unity DLLs are:
C:\Program Files\Unity\Hub\Editor\<version-number>\Editor\Data\Managed\UnityEngine
The UnityEngine
folder contains the .dll files for a number of modules that you can reference to get to the specific namespace you require. Additionally, some namepsaces require a reference to a compiled library from a Unity Project (for instance, UnityEngine.UI
), which is located in the project folder’s directory:
~\Library\ScriptAssemblies
The exact options for compiling the DLL will vary depending on the compiler you use. As an example, the command line for the Mono C# compiler, mcs
, might look like this on macOS:
mcs -r://Applications/Unity/Hub/Editor/<version-number>/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 include in the build; in this case the UnityEngine.UIModule
library. The -target
option specifies which type of build is required; the word “library” is signifies a DLL build. Finally, the name of the source file to compile is ClassesForDLL.cs
. The compiler assumes that this file is in the current working folder, but you can use a full path to specify the file if necessary. The resulting .dll file appears in the same folder as the source file.
Once you’ve compiled the DLL, you can drag the .dll file into the Unity Project like any other Asset. The managed plug-in has a foldout triangle which you can use to reveal the separate classes inside the library. You can drag classes that derive from MonoBehaviour
onto Game Objects like ordinary scripts. You can use non-MonoBehaviour
classes directly from other scripts in the usual way.
This section explains how to build and integrate a simple DLL example with Visual Studio, and also how to prepare a debugging session for the DLL.
Open Visual Studio and create a new project. Select File > New > Project and then choose Visual C# > Class Library.
Complete the following information for the new library:
DLLTest
as the name).Next, add references to the Unity DLLs. In Visual Studio, open the contextual menu for References in the Solution Explorer and select Add Reference. Then, select Browse > Select File.
At this stage, select the required .dll file, located in the UnityEngine
folder.
For this example, rename the class to MyUtilities
in the Solution browser and replace its code with the following:
using System;
using UnityEngine;
namespace DLLTest {
public class MyUtilities {
public int c;
public void AddValues(int a, int b) {
c = a + b;
}
public static int GenerateRandom(int min, int max) {
System.Random rand = new System.Random();
return rand.Next(min, max);
}
}
}
With the code in place, build the project to generate the DLL file along with its debug symbols.
Create a new Project in Unity and copy the built file <project folder>/bin/Debug/DLLTest.dll
into the Assets folder. Then, create a C# script called Test
in Assets
, and replace its contents with the following code:
using UnityEngine;
using System.Collections;
using DLLTest;
public class Test : MonoBehaviour {
void Start () {
MyUtilities utils = new MyUtilities();
utils.AddValues(2, 3);
print("2 + 3 = " + utils.c);
}
void Update () {
print(MyUtilities.GenerateRandom(0, 100));
}
}
Attach this script to a GameObject in the SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary and press Play, and Unity displays the output of the code from the DLL in the Console windowA Unity Editor window that shows errors, warnings and other messages generated by Unity, or your own scripts. More info
See in Glossary.
To enable support for compiling unsafe C# code go to Edit > Project SettingsA broad collection of settings which allow you to configure how Physics, Audio, Networking, Graphics, Input and many other areas of your project behave. More info
See in Glossary > Player. Expand the Other Settings panel and enable the Allow Unsafe Code checkbox.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.