Unity3D Tutorial: Calling DLL files in C++

1. Create the DLL

The main problem that plagues the Unity folks is probably that the Dll won't be generated. This is really troublesome. Here I will give a method of creating a DLL in vs 2008.

1) First open vs2008, of course 2005 or 2003 are similar. What I use here is 2008.

Select the Win32 program in the C++ project, and then choose one of the console program or the win32 program. Write the project name and click create.

Unity3D Tutorial: Calling DLL files in C++

2) The reason for choosing one is that it is not important here, what is important is the next step, and this panel will appear after creation. Select the next step to see the following options:

Note that the DLL is first selected to prove that our program is a dll project. The purpose of choosing "Empty Project" is to build a clean DLL program, the code inside is all handwritten by us, so we know what we have written.

Unity3D Tutorial: Calling DLL files in C++

3 ) Create a project so that we get an empty project. In this project, there is nothing but a few files plus. We right click on the folder and create a new item. Create a CPP file and a header file in the new item.

Unity3D Tutorial: Calling DLL files in C++

4) Get the following two files: it doesn't matter what they are named.

Unity3D Tutorial: Calling DLL files in C++

5) The following are the codes of the two files: the generation of DLL requires a lot of knowledge. Let me briefly talk about it here. The use of dll is different from the macro used for generation. The above code is mainly to distinguish between different environments You should use that macro. If you don’t understand it, don’t worry about it. You just need to change the function at the bottom.

head File

  

1

    #if defined (EXPORTBUILD) 

2

    # define _DLLExport __declspec (dllexport) 

3

    # else 

4

    # define _DLLExport __declspec (dllimport) 

5

    #endif 

6

7

    extern "C"  int _DLLExport MyADD(int x,int y); 

code file

  

01

 // macro definition 

02

    #define  EXPORTBUILD 

03

04

    // load the header file 

05

    #include "DLL.h" 

06

07

    // setting function 

08

    int _DLLExport MyADD(int x,int y) 

09

    {  

10

        return x+y; 

11

    } 

In this way, we have generated a MyAdd() function that passes in two numbers and returns their sum. Then compile this program, and the following dialog box will appear, which proves that you have succeeded! Then put this DLL file in the assert Plugins of your unity project (if you don't have this folder, create one manually).

Unity3D Tutorial: Calling DLL files in C++

6) Create a C# script in unity, call the dll we just generated, here our DLL name is called DLL, so write [DllImport (“DLL”)], if your name is Test.dll, then Just write [DllImport("Test")]

Write a GUI button to display the result of the operation. .

  

01

    using UnityEngine ;   

02

03

    using System.Collections;   

04

05

    using System.Runtime.InteropServices;   

06

07

    public class NewBehaviourScript : MonoBehaviour {  

08

09

          [DllImport ("DLL")]   

10

          private static extern int MyADD(int x,int y);   

11

          int i = MyADD(5,7);    

12

        // Update is called once per frame 

13

        void Update () {} 

14

        void OnGUI ()  

15

        {  

16

            GUI.Bu

Unity3D教程:调用C++中DLL文件

  

1

tton(new Rect(1,1,200,100),"this DLL  i = 5+7, i is '"+i+"'"); 

2

        } 

3

    }

4

5

运行通过就可以在界面上看到

Unity3D教程:调用C++中DLL文件

7)输出unity项目exe到目录,然后在目录中也添加Plugins目录,把DLL文件也考进去,然后点运行, 成功!

Guess you like

Origin blog.csdn.net/weixin_55688630/article/details/128104900