Unity3d call C + + DLL DLL callback Unity3d (c + + DLL callback C # function)

Source: Internet
Author: User
Tags define function

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.