Current location - Quotes Website - Personality signature - How to write Unity native plug-ins
How to write Unity native plug-ins
Unity can import code written (and compiled) in other languages, which is called a native plug-in. Today, I will share with you how to create a Unity native plug-in.

Managed and unmanaged plug-ins in Unity

Connecting different codes was not initiated by Unity. If you are a Windows user, you may have heard of DLL, the abbreviation of dynamic link library. Similar to stand-alone applications, they are compiled software. The difference is that they cannot be directly executed because they are specially designed for other applications.

Unity supports two plug-ins: managed and unmanaged. The former is a bytecode language written in C# and compiled into CIL. Managed plug-ins are as powerful as C# scripts and come with compiled source code. Unmanaged (or native) plug-ins are software written in other languages, usually C++. They are almost unlimited in function and are usually faster than traditional scripts because they are all compiled into machine code.

Step 1: Create a new C++ project.

In this example, I used Visual Studio 2015; ; You can choose any IDE to compile C++ code. First, create an unmanaged C++ library, that is, create a new project. Open Visual Studio, click File | New Project, and then select visual c++| win32 Console Application.

After naming the project (TestDLL in this case), make sure that the application type is set to DLL, and select an empty project under Additional options.

At this point, the Visual C++ solution is ready and we can start writing code.

Step 2: Write the library.

C++ code is usually divided into two files. Function definition (header file) and function implementation (implementation file). The implementation file is a. cpp file, which is placed in the ResourceFiles directory, and the header file has the same name. H is placed in the HeaderFiles directory. In this example, we created a header file and an implementation file; The implementation file will contain all the functions to be saved to the DLL. You can right-click the corresponding folder to create a new file, and then select Add >; NewItem (new option).

Implementation part: TestDLLSort.cpp

Start coding and sort the array.

[AppleScript] View the copied code as plain text?

0 10203040506070809 10 1 1 12 # include " testdllsort . h " # include & lt; Algorithm & gtextern "c" {void testsort (int a [], int length) {std::sort(a, a+length); } }

5 ~ 7 use the array sorting function std:sort in the algorithm library. Be familiar with C++ 1 1. Only the external "c" block is added, which must be used to export the reference of TestSort to the DLL.

Header file: TestDLLSort.h

The definition in the implementation file must be exactly the same as the header file. It must contain the TestSort prototype, that is, the function signature.

[AppleScript] View the copied code as plain text?

123456 # defines testdllsort _ API _ _ declspec (dll export) extern "c" {testdllsort _ API void testsort (int a [], int length); }

The rest is the code necessary to create the DLL. TESTDLLSORT_API can be any name that marks all export functions. In more complex software, TESTDLLSORT_API should be bound to __declspec(dllimport) as needed. But in this case, it is not necessary.

Step 3: Compile

The last step is to compile our DLL in Visual Studio. Please ensure that the publishing platform is set correctly (32-bit or 64-bit). Then select build >; Build a solution.

On the console at the bottom of the screen, you will see the following output log:

[AppleScript] View the copied code as plain text?

0 10203040506070809 10 1 12 13 14 1 & gt; -Rebuild All Started: Project: TestDLL, Configuration: Version x64-1> testdllsort . CPP 1 & gt; Creating library C:\ Users \ Alan Zucconi \ documents \ Visual Studio 2015 \ Projects \ TestDLL \ x64 \ Release \ TestDLL.lib and object c: \ users \ Alan Zucconi \ Documents \ Visual Studio 20 15 \ Projects \ TestDLL \ x64 \ Release \ TestDLL . exp 1 & gt; Generate code1> All 30 functions were compiled because IPDB/IOBJ was not found in the previous compilation. 1 & gt; Finished generating code1> testdll . vcx proj-& gt; C: \ Users \ Alan Zucconi \ Documents \ Visual Studio 20 1 5 \ Projects \ TestDLL \ x64 \ Release \ TestDLL. DLL = = = = = = = Rebuild All:1Success, 0 Failure, 0 Skip = =

If you find a warning like warning c4273: Inconsistent DLLINGAGE, it may indicate that the compiler is not sure whether to use __declspec(dllimport) or __declspec(dllexport). If you want to create a local plug-in for Unity, use the latter.

Step 4: Import Unity

According to the above compilation log, find the compiled DLL in the project file. In this example, it is located in the folder x64\Release. We just want this document. The first step in using it in Unity is to copy it to the Plugins folder.

Native plug-ins are usually related to the operating system or platform. You can check the view panel on the right to ensure that each DLL is included in the correct platform.

Step 5: Unified use

After importing, using DLL is relatively simple. The first step is to define an entry point using DLLImport. You need to specify a DLL name and a function name. An alias is provided so that it can be called like any other function.

[AppleScript] View the copied code as plain text?

01020304050607080911213141516/kloc-0. Runtime. InteropServices public class testdll: monobehavior {//imported function [DllImport("TestDLL ",EntryPoint = "TestSort")] public static extern void TestSort(int [] a, int length); public int[]a; void Start() { TestSort(a,a . Length); } }

The string of the entry must be the same as the name in the C++ library. But you can use the following methods to call the function; C# will call it that in the future.

You should notice that Unity can't detect unmanaged DLL in the editor; You must run the game to check whether the connection is successful. For managed dlls, static detection can be performed.