Version: 2017.3
Plugin Inspector
原生插件

托管插件

通常,脚本作为源代码文件保存在项目中,并在每次源代码更改时由 Unity 进行编译。但是,也可以使用外部编译器将脚本编译为动态链接库 (DLL)。然后可以将生成的 DLL 添加到项目中,并且它包含的类可以像普通脚本一样附加到对象。

It is generally much easier to work with scripts than DLLs in Unity. However, you may have access to third party Mono code which is supplied in the form of a DLL. When developing your own code, you may be able to use compilers not supported by Unity (F#, for example) by compiling the code to a DLL and adding it to your Unity project. Also, you may want to supply Unity code without the source (for an Asset Store product, say) and a DLL is an easy way to do this.

创建 DLL

为了创建 DLL,首先需要一个合适的编译器。并非所有能生成 .NET 代码的编译器都能保证与 Unity 协同工作,因此在使用编译器进行重要工作之前,建议使用一些可用的代码来测试该编译器是否兼容。如果 DLL 不包含依赖于 Unity API 的代码,则可以使用适当的编译器选项将其直接编译为 DLL。如果确实想使用 Unity API,则需要将 Unity 自己的 DLL 提供给编译器。在 Mac 上,这些都包含在应用程序包中(可以使用上下文菜单中的 Show Package Contents 命令来查看这个包的内部结构:右键单击或按住 Ctrl 键并单击 Unity 应用程序):

Unity DLL 的路径通常是

/Applications/Unity/Unity.app/Contents/Frameworks/Managed/

…并有两个名为 UnityEngine.dll 和 UnityEditor.dll 的 DLL。

在 Windows 上,可以在 Unity 应用程序附带的文件夹中找到这些 DLL。路径通常为

C:\Program Files\Unity\Editor\Data\Managed

…对于 Mac OS,这些 DLL 的名称也是一样的。

编译 DLL 的具体选项将根据使用的编译器而有所不同。例如,Mono C# 编译器的命令行 mcs 在 Mac OS 上可能如下所示:

mcs -r:/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll -target:library ClassesForDLL.cs 

这里,-r 选项指定要包含在构建中的库的路径,在本例中为 UnityEngine 库的路径。-target 选项指定需要的构建类型;“library”一词用于选择 DLL 构建。最后,要编译的源文件的名称是 _ClassesForDLL.cs_(假设此文件位于当前工作文件夹中,但可以根据需要使用完整路径来指定文件)。假设一切顺利,生成的 DLL 文件将很快出现在与源文件相同的文件夹中。

使用 DLL

编译完成后,可以像任何其他资源一样将 DLL 文件直接拖入 Unity 项目。DLL 资源有一个折叠三角形,可用于显示库中的单独类。从 MonoBehaviour 派生的类可以像普通脚本一样拖到游戏对象上。非 MonoBehaviour 类可以按常规方式直接在其他脚本中使用。

具有可见类并可折叠的 DLL
具有可见类并可折叠的 DLL

Step by Step Guide for MonoDevelop and Visual Studio

This section explains how to build and integrate a simple DLL example with MonoDevelop and Visual Studio and also how to prepare a debugging session for the DLL.

设置项目

First, open MonoDevelop or Visual Studio and create a new project. In MonoDevelop, you do this by selecting File > New > Solution and then choosing C# > Library. In Visual Studio, you should select File > New > Project and then choose Visual C# > Class Library.

然后,需要填写新库的信息:

  • Name 是命名空间(在此示例中使用“DLLTest”作为名称)。
  • Location 是项目的父文件夹。
  • Solution name 是项目的文件夹。

Next, you should add references to the Unity DLLs. In MonoDevelop, you should open the contextual menu For References in the Solution Browser and choose Edit References. Then, choose the option .Net Assembly tab > File System > select file. In Visual Studio, open the contextual menu For References in the Solution Explorer and choose Add Reference. Then, choose the option Browse > Browse > select file.

At this stage, you will have the option to select the required DLL file. On Mac OSX, the file can be found at

    Applications/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll

…while on Windows, the path is

    Program Files\Unity\Editor\Data\Managed\UnityEngine.dll

代码

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

代码准备好后,请构建项目以便生成 DLL 文件及其调试符号。

在 Unity 中使用 DLL

For this example, 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));
     }
}

将此脚本附加到场景中的对象并按 Play 时,Console 窗口中将显示 DLL 代码的输出。

Setting Up a Debugging Session for the DLL

Firstly, you should prepare the debug symbols for the DLL. In MonoDevelop, copy the built file <project folder>/bin/Debug/DLLTest.dll.mdb into the Assets/Plugins folder. In Visual Studio, execute

Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb.exe

in the command prompt, passing <project folder>\bin\Debug\DLLTest.pdb as a parameter. Then, copy the converted file <project folder>\bin\Debug\DLLTest.dll.mdb into Assets/Plugins.

Next, open the “Test” script in MonoDevelop. Make sure the Unity debugger is enabled from the Tools menu (Windows) or MonoDevelop-Unity menu (MacOS). The option you need from this menu is Add-in Manager > Installed tab > Unity > select Mono Soft Debugger Support for Unity > Enable.

完成此设置后,可以按常规方式在 Unity 中调试使用 DLL 的代码。有关调试的更多信息,请参阅脚本工具部分。

Plugin Inspector
原生插件