Unity3d calling Lua script Luajit C + + DLL implementation

Source: Internet
Author: User
Tags lua

The use of functions in the C + + DLL and callbacks in Unity3d is described earlier in this article, but the final goal is to let the functions in the Lua script be called in Unity3d, which is now implemented.

First, focus on the previous two articles:

1.Unity3d call C + + DLL (Win platform)

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

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Mastering the method of using C + + plugin in Unity3d is easy to understand.

Here we go.

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Compiling Luajit

First download Luajit

Http://luajit.org/download.html

After the download is complete, unzip.

Open the VS2012 (or other version) command-line tool, navigate to the SRC directory, execute the command

Msvcbuild
article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn
To compile the Luajit.

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Compile successfully!


Compiling the build DLL

Reference Unity3d Call the C + + DLL (Win platform) to create a C + + DLL project, set the project's include directory and reference directory, add a Luajit reference

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Then add the code file

LuaDelegate.h

# define _dllexport __declspec (DLLExport)//marked as an export function; extern "C" Long Long _dllexport dlltest (); extern "C" Long long _dllex Port Calllua (char* path);

article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

LuaDelegate.cpp

#include "LuaDelegate.h" #include <src\lua.hpp>  #include <iostream>/  * Open the library required for LUA */  #pragma Comment (lib, "Lua51.lib")  extern "C"  {  #include <src\lua.h>  #include <src\lualib.h>  }  Long Long dlltest () {long Long a = 1;int B = 0;while (b<1000000000) {a=a+1;b=b+1;} return A;} A long long Calllua (char* path)  {             lua_state *l=lual_newstate ();/* Creates an interpreter handle */             lual_openlibs (L);             /* Open all LUA libraries *                            ///lual_loadfile (L, "Script1.lua");/* into the Lua script file *     ///lual_dofile (L, "C:/script.lua");   Lual_dofile (L,path);   Lua_getglobal (L, "test");   Lua_pushnumber (l,1);   Lua_pushnumber (l,2);   Lua_call (l,2,1);   Lua_call (l,0,1);   Long Long Result=lua_tonumber (l,-1);            Lua_close (L);       /* Close handle */  return result;}

Build the solution.

After the compilation succeeds, the generated DLL file is found in the build directory. Drag into the Assets/plugins directory in the Unity3d project.

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

The Lua script file is then placed in the specified folder.

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Unity3d Script calls Lua function

Create a Unity3d script

Using unityengine;using system.collections;using system.runtime.interopservices;public class NewBehaviourScript: monobehaviour {[DllImport ("Luadll")]private static extern long dlltest (); [ DllImport ("Luadll")]private static extern long Calllua (string path);//Use the For Initializationvoid Start () {DEBUG.L OG (application.datapath);} 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), "Test Lua")) {long before=system.datetime.now.ticks;debug. Log ("monotest=" + Calllua (application.datapath+ "/luasource/script.lua"));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+1;b=b+1;} return A;}}

Functions that have the same functionality as C + +, Lua, and Mono are called separately in the script. Used to make a simpleRecreational performance test, entertainment only。。

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

After successful operation:

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Sample Project Download:

http://download.csdn.net/detail/cp790621656/8706393

Article turn from Http://blog.csdn.net/huutu star Ring game http://www.thisisgame.com.cn

Unity3d calls the Luajit C + + DLL implementation to invoke the Lua script

Related Article

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.