Unity 3D calls the DLL method

1. Create DLL

Probably the main problem that bothers the Unity people is that the Dll doesn't get generated. This is really troublesome, here I give a method to create a DLL in vs 2008 .

  1) First open vs2008, of course, 2005 or 2003 are similar. I am using 2008 here.

      Select the Win32 program in the C++ project , and then choose either the console program or the win32 program. Write the project name and click Create.

 

 

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

       Note, first select DLL 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 written by us, so we all know what has been written.

 

  3 ) Create the project, so 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 respectively in the new item.

 

 4) Get the following two files: it doesn't matter what the name is

 

  5) The following is the code of the two files: The generation of DLL is also a lot of knowledge. I will briefly say here that 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, leave it alone. You just need to change the function at the bottom.

head File

///////////////////  DLL.h /////////////////////////////

[cpp]view plaincopy 

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 

//////////////////   DLL.cpp //////////////////////////// 

[cpp]view plaincopy 

1.  // Macro definition  

2. #define  EXPORTBUILD  

3.   

4.  // Load header file  

5. #include "DLL.h"  

6.   

7.  // Setup function  

8. int _DLLExport MyADD(int x,int y)  

9. {  

10. return x+y;      

11. }  

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

 

 

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

 

[csharp]view plaincopy 

1. using UnityEngine;    

2.   

3. using System.Collections;    

4.   

5. using System.Runtime.InteropServices;    

6.   

7. publicclass NewBehaviourScript : MonoBehaviour {   

8.         

9.       [DllImport ("DLL")]    

10. privatestaticexternint 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.Button(new Rect(1,1,200,100),"this DLL  i = 5+7, i is '"+i+"'");  

17.     }  

18. }  

After running, you can see it on the interface

 

 

 

7 ) Output the unity project exe to the directory, then add the Plugins directory in the directory, put the DLL file into it, and then click Run, huh, it's successful!

 

The above is the case of unity outputting exe to win32 system, let's take a look at how to call DLL on other platforms .

Android environment:

[csharp]view plaincopy 

1. using UnityEngine;  

2. using System.Runtime.InteropServices;  

3.   

4. class SomeScript : MonoBehaviour {  

5. // This tells unity to look up the function FooPluginFunction     

6. // inside the plugin named "PluginName"     

7.    [DllImport ("PluginName")]  

8. privatestaticexternint MyADD(int x,int y);        

9.   

10. void Awake () {     

11. // Calls the FooPluginFunction inside the PluginName plugin        

12. // And prints 5 to the console        

13. int i = MyADD(5,7);           

14.    }  

15. }   


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324858580&siteId=291194637