The previous Unity3d call C + + DLL (Win platform) introduced a simple Unity3d method to call C + + DLLs, but this is not enough, here is the way to let the C + + DLL callback Unity3d through the function pointers.
Turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
The functions in creating DLLs and calling DLLs in Unity3d are described in the previous article.
First of all, there is no function pointer in C #, we use Delegate.
Turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Let's create the DLL.
Modified in the works of the previous article.
Calculate.h
# define _dllexport __declspec (DLLExport)//marked as export function;//define function pointer; typedef void (__stdcall *cppcallback) (int tick); extern "C" void _dllexport Setcallback (Cppcallback callback); extern "C" Long Long _dllexport dlltest ();
Calculate.cpp
#include "Calculate.h" Long Long dlltest () {long Long a = 1;int B = 0;while (b<1000000000) {a=a+b;b++;} return A;} void Setcallback (Cppcallback callback) {int tick=1223;callback (tick);}
Define the function Setcallback () in the DLL to set the callback function.
Turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Using DLLs in Unity3d
Using unityengine;using system.collections;using system.runtime.interopservices;public class NewBehaviourScript: monobehaviour {[DllImport ("Testdll")]private static extern long dlltest (); [ DllImport ("Testdll")]private static extern void Setcallback (Cscallback callback);p ublic delegate void Cscallback (int tick), static cscallback callback;//Use this for initializationvoid Start () {callback = Cscallbackfuction;} static void cscallbackfuction (int tick) {Debug.Log ("cscallbackfuction" +tick. ToString ());} void Ongui () {if (GUI. button (new Rect (100,100,200,200), "Test DLL")) {long before=system.datetime.now.ticks;debug. Log ("dlltest=" + dlltest ());D Ebug. Log ("Take" + (System.datetime.now.ticks-before));} if (GUI. button (new Rect (100,300,200,200), "Setcallback")) {long before=system.datetime.now.ticks; Setcallback (callback);D Ebug. Log ("Take" + (System.datetime.now.ticks-before));} if (GUI. button (new Rect (300,300,200,200), "Test Mono")) {long before=system.datetime.now.ticks;debug. Log ("monotest=" + monotest ());D EBUG.Log ("Take" + (System.datetime.now.ticks-before));}} Update is called once per framevoid update () {}long monotest () {Long A = 1;int B = 0;while (b<1000000000) {a=a+b;b++;} return A;}}
turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Run successfully
Turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Sample Project Download:
Http://pan.baidu.com/s/1jG499HW
Turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
Unity3d call C + + DLL DLL callback Unity3d (c + + DLL callback C # function)