使用したLua:lua-5.2.1_Win32_dll11_lib
使用したUnity:Unity Pro 4.3 体験版
まず初めにVisual Studioに解凍したLuaライブラリファイルを関連付ける。
C:\Lua\include インクルードファイルをここに置いてみた。
C:\Lua\lib ライブラリファイルをここに置いてみた。
C:\Lua\test 操作するluaスクリプトをここに置いてみた。
下記はC++のDLL用のスクリプト
#include <iostream>
#include "lua.hpp"
using namespace std;
#define DllExport extern "C" __declspec (dllexport)
DllExport int Func() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "C:\\Lua\\test\\sample.lua");
lua_getglobal(L, "lua_Func");//luaスクリプトの関数を登録
lua_call(L, 0, 1);//引数の数 戻り値の数
int n = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_close(L);
return n;
}
下記はsample.luaのスクリプト
function lua_Func()
return 3
end
C++のDLLを作成した後、UnityにDLLをPluginsフォルダに取り込む
下記はUnityのC#のスクリプト
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;//ココ大事
public class Plugin : MonoBehaviour {
[DllImport("Hoge")]
private static extern int Func();
// Use this for initialization
void Start () {
Debug.Log(Func());
}
// Update is called once per frame
void Update () {
}
}
成功!!
続き:
メモ:UnityをLuaで制御する その2