Version: 2020.1
プラグインインスペクター
ネイティブプラグイン

マネージプラグイン

Managed plug-ins 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 scripts can access the managed code. Therefore, there isn’t a lot of difference in usage between managed plug-in 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 GameObjects 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.

Creating a managed plug-in

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/Unity.app/Contents/Managed/UnityEngine

On Windows the path to the Unity DLLs are:

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 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/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.

Using the managed plug-in

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.

展開された DLL。複数クラスを表示
展開された DLL。複数クラスを表示

Visual Studio の利用

ここでは、Visual Studioで簡単な DLL をビルドして統合する方法と、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:

  • Name is the namespace (for this example use DLLTest as the name).
  • Location はプロジェクトの親フォルダーです。
  • Solution 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.

コード

このコードサンプルでは、ソリューションブラウザーでクラスの名前を MyUtilities に変更し、コードを以下のものに置き換えます。

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);
        }
    }
}

最後にプロジェクトをビルドして DLL ファイルとそのデバッグシンボルを生成します。

DLL を Unity で使用

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 Scene and press Play, and Unity displays the output of the code from the DLL in the Console window.

‘unsafe’ C# コードのコンパイル

To enable support for compiling unsafe C# code go to Edit > Project Settings > Player. Expand the Other Settings panel and enable the Allow Unsafe Code checkbox.

プラグインインスペクター
ネイティブプラグイン