Unity3d 调用 Luajit C++ DLL 实现调用 Lua脚本
2015-05-16 14:14
579 查看
前面介绍了在 Unity3d 中通过 Plugin 功能来调用 C++ DLL 中的函数 以及回调的使用方法,但最终目的还是让 Unity3d 中可以调用 Lua 脚本中的函数,现在来实现。
首先关注前面两篇文章:
1、Unity3d 调用C++ DLL (Win平台)
2、Unity3d 调用 C++ DLL之 DLL回调Unity3d (C++ DLL回调 C#函数)
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
掌握 Unity3d 中使用 C++ Plugin的方法才方便理解。
下面我们开始了。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
编译Luajit
首先下载 Luajit
http://luajit.org/download.html 下载完毕之后,解压。
打开VS2012 (或其他版本) 命令行工具,定位到 src目录,执行 命令
msvcbuild文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn 来编译 Luajit。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
编译成功!
编译生成DLL
参考 Unity3d 调用C++ DLL (Win平台) 创建 C++ DLL项目,设置项目的包含目录 和引用目录 ,添加 Luajit 引用
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
然后添加代码文件
LuaDelegate.h
# define _DLLExport __declspec (dllexport) //标记为导出函数;
extern "C" long long _DLLExport dlltest();
extern "C" long long _DLLExport CallLua(char* path);
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
LuaDelegate.cpp
#include "LuaDelegate.h"
#include <src\lua.hpp>
#include <iostream>
/*打开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;
}
long long CallLua(char* path)
{
lua_State *L=luaL_newstate(); /*创建一个解释器句柄*/
luaL_openlibs(L); /*打开所有的Lua库*/
//luaL_loadfile(L,"script1.lua"); /*调入Lua脚本文件*/
//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); /*关闭句柄*/
return result;
}
生成解决方案。
编译成功后到 生成目录 找到生成的 DLL 文件。拖入到 Unity3d 项目中的 Assets/Plugins 目录中。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
然后把 Lua 脚本文件放入到 指定 文件夹中。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
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 this for initialization
void Start () {
Debug.Log (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());
Debug.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"));
Debug.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());
Debug.Log("take "+(System.DateTime.Now.Ticks-before));
}
}
// Update is called once per frame
void Update () {
}
long monotest()
{
long a = 1;
int b = 0;
while(b<1000000000)
{
a=a+1;
b=b+1;
}
return a;
}
}
在脚本中分别调用了 C++、Lua、Mono的同一功能的函数。用来做简单的娱乐性能测试,仅供娱乐。。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
运行成功后:
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
示例工程下载: http://download.csdn.net/detail/cp790621656/8706393
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
首先关注前面两篇文章:
1、Unity3d 调用C++ DLL (Win平台)
2、Unity3d 调用 C++ DLL之 DLL回调Unity3d (C++ DLL回调 C#函数)
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
掌握 Unity3d 中使用 C++ Plugin的方法才方便理解。
下面我们开始了。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
编译Luajit
首先下载 Luajithttp://luajit.org/download.html 下载完毕之后,解压。
打开VS2012 (或其他版本) 命令行工具,定位到 src目录,执行 命令
msvcbuild文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn 来编译 Luajit。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
编译成功!
编译生成DLL
参考 Unity3d 调用C++ DLL (Win平台) 创建 C++ DLL项目,设置项目的包含目录 和引用目录 ,添加 Luajit 引用文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
然后添加代码文件
LuaDelegate.h
# define _DLLExport __declspec (dllexport) //标记为导出函数;
extern "C" long long _DLLExport dlltest();
extern "C" long long _DLLExport CallLua(char* path);
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
LuaDelegate.cpp
#include "LuaDelegate.h"
#include <src\lua.hpp>
#include <iostream>
/*打开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;
}
long long CallLua(char* path)
{
lua_State *L=luaL_newstate(); /*创建一个解释器句柄*/
luaL_openlibs(L); /*打开所有的Lua库*/
//luaL_loadfile(L,"script1.lua"); /*调入Lua脚本文件*/
//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); /*关闭句柄*/
return result;
}
生成解决方案。
编译成功后到 生成目录 找到生成的 DLL 文件。拖入到 Unity3d 项目中的 Assets/Plugins 目录中。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
然后把 Lua 脚本文件放入到 指定 文件夹中。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
Unity3d 脚本 调用 Lua 函数
创建 Unity3d 脚本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 this for initialization
void Start () {
Debug.Log (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());
Debug.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"));
Debug.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());
Debug.Log("take "+(System.DateTime.Now.Ticks-before));
}
}
// Update is called once per frame
void Update () {
}
long monotest()
{
long a = 1;
int b = 0;
while(b<1000000000)
{
a=a+1;
b=b+1;
}
return a;
}
}
在脚本中分别调用了 C++、Lua、Mono的同一功能的函数。用来做简单的娱乐性能测试,仅供娱乐。。
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
运行成功后:
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
示例工程下载: http://download.csdn.net/detail/cp790621656/8706393
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
相关文章推荐
- lua调用C++写的DLL实现“热更新”
- Lua与C/C++交互——C/C++调用Lua脚本
- Java调用C/C++实现的DLL动态库——JNI
- lua笔记 --- 一个C++调用lua函数的类的实现
- LUA与C++交互(在LUA脚本里调用C++函数传参)
- C++中使用Lua脚本 和lua中调用c的方法
- Unity3d 调用C++的DLL
- Unity3D教程:调用C++中DLL文件
- cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密
- 关于C#中调用C++编写的DLL(非托管的DLL)的实现和问题记录
- Matlab 脚本编译成dll被c++调用
- C++调用C#的DLL实现方法
- Lua移植到arm上 并实现在arm上 可以让lua脚本调c语言,C语言调用lua脚本
- (如何在C++代码中调用Lua脚本的函数?)
- Cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密
- 函数编译lua笔记 --- 一个C++调用lua函数的类的实现
- 一种实现 Java调用C++的DLL的方法
- Unity3d 调用C++写的DLL
- lua学习笔记 2 android调用Lua。Lua脚本中实现添加Button,并为Button增加Listener
- cocos 使用Luajit将Lua脚本编译为bytecode,从而实现加密