Lua--------------------unity3D與Slua融合使用
阿新 • • 發佈:2017-10-31
tar 對象 調用 pin dal etl 可能 turn defined
下載與安裝
- 下載地址 GitHub
- 安裝過程
1.下載最新版,這裏, 解壓縮,將Assets目錄裏的所有內容復制到你的工程中,對於最終產品,可以刪除slua_src,例子,文檔等內容,如果是開發階段則無所謂。
2.等待unity編譯完畢,如果一切順利的話,將出現slua菜單, 點擊slua菜單中 All->Make 命令 手動生成針對當前版本的U3d接口文件。
3.每次更新slua版本,務必記得clear all,然後make all,否則可能運行不正確
主要的內容包括
-
LuaState
狀態機對象執行Lua
字符串 -
LuaState
狀態機對象執行Lua
腳本 -
LuaState
Lua
腳本內的自定義函數 -
LuaState
狀態機對象註冊C#
自定義類 -
Lua
腳本中調用C#
中的自定義類
創建第一個可以使用Slua框架的Unity項目
-
在
MainCamera
對象上創建AppDelegate.cs
組件using UnityEngine; using System.Collections; public class AppDelegate : MonoBehaviour { void Start () { //在下方添加初始化代碼 } }
-
使用
LuaState
狀態機對象執行Lua字符串using UnityEngine; using System.Collections; using SLua; public class AppDelegate : MonoBehaviour { private static LuaState ls_state = new LuaState(); void Start () { //在下方添加初始化代碼 ls_state.doString("print(\"Hello Lua!\")"); } }
-
使用
LuaState
狀態機對象執行Lua腳本文件HelloLua.lua
-
在
Resources
文件夾下添加HelloLua.lua
文件print("Lua Scripts:Hello");
-
在
AppDelegate.cs
中設置LuaState.loaderDelegate
啟動文件委托代理using UnityEngine; using System.Collections; using SLua; using System.IO; public class AppDelegate : MonoBehaviour { void Start () { //設置腳本啟動代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); } }
-
在
AppDelegate.cs
中通過LuaState
對象執行HelloLua.lua
腳本using UnityEngine; using System.Collections; using SLua; using System.IO; public class AppDelegate : MonoBehaviour { void Start () { //設置腳本啟動代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); //設置執行腳本 LuaState ls_state = new LuaState (); ls_state.doFile ("HelloLua.lua"); } }
-
-
通過
LuaState
對象獲取並執行HelloLua.lua
腳本中的一個函數-
HelloLua.lua
function sum( v1,v2 ) -- body return v1 + v2 end function mul( v1,v2 ) -- body return v1 * v2 end
-
AppDelegate.cs
using UnityEngine; using System.Collections; using SLua; using System.IO; using LuaInterface; using System; public class AppDelegate : MonoBehaviour { //添加LuaState初始化時的回調函數特性函數 [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int init(IntPtr L) { //設置初始化LuaObject對象 LuaObject.init(L); return 0; } void Start () { //創建狀態機對象 LuaState ls_state = new LuaState (); //設置腳本啟動代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); //初始化LuaState狀態機與C#的轉換對象 LuaState.pcall (ls_state.L, init); //設置狀態機對象的執行腳本 ls_state.doFile ("HelloLua.lua"); //獲取腳本中的mul函數 LuaFunction mul = ls_state.getFunction ("mul"); //調用該函數並且接收返回值 double result = (double)mul.call (-2, 3); Debug.Log(result); } }
-
-
自定義C#對象
LOHuman.cs
,在HelloLua.lua
中-
LOHuman.cs
using System; using LuaInterface; using SLua; //該特性可以修飾以下類將會註冊到Slua執行環境中 [CustomLuaClass] public class HHHuman { //年齡成員 protected int age = 0; //姓名成員 protected string name = ""; //添加Lua代碼中的靜態函數 [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] [StaticExport] public static int CreateHuman(IntPtr l) { HHHuman item = new HHHuman (); LuaObject.pushObject (l, item); //只執行了1次LuaObject.push,返回值寫1 return 1; } public int Age { set; get; } public string Name{ set; get; } }
-
通過點擊菜單欄中的
SLua->Custom->Clear
將舊版本的自定義類刪除 - 通過點擊菜單欄中的
SLua->Custom->Make
重新制作適用於SLua的新的自定義類Lua_HHHuman.cs
- 默認存放目錄
Assets->SLua->LuaObject->Custom->
- 默認存放目錄
- 同時會自動生成一個
BindCustom.cs
類,代碼如下:using System; namespace SLua { [LuaBinder(3)] public class BindCustom { public static void Bind(IntPtr l) { Lua_HHHuman.reg(l); Lua_System_Collections_Generic_List_1_int.reg(l); Lua_System_Collections_Generic_Dictionary_2_int_string.reg(l); Lua_System_String.reg(l); } } }
- 在
AppDelegate.cs
的init
函數中,綁定自定義類HHHuman.cs
//添加LuaState初始化時的回調函數特性函數 [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int init(IntPtr L) { LuaObject.init(L); BindCustom.Bind (L); return 0; }
- 在
HelloLua.lua
腳本中,調用C#中的自定義類的靜態函數CreateHuman()
function testHuman() -- body local human = HHHuman.CreateHuman() -- local list = human:getList() print(human.Age) end
-
在
AppDelegate.cs
的Start
函數中,調用HelloLua.lua
腳本中的testHuman
函數static LuaState ls_state; void Start () { //創建狀態機對象 ls_state = new LuaState (); //設置腳本啟動代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); //初始化LuaState狀態機與C#的轉換對象 LuaState.pcall (ls_state.L, init); //設置執行腳本 ls_state.doFile ("HelloLua.lua"); //獲取testHuman函數 LuaFunction testHuman = ls_state.getFunction ("testHuman"); //無參函數的調用 testHuman.call (); }
-
以上主要的內容包括:
LuaState
狀態機對象執行Lua
字符串LuaState
狀態機對象執行Lua
腳本LuaState
狀態機對象調用Lua
腳本內的自定義函數LuaState
狀態機對象註冊C#
自定義類Lua
腳本中調用C#
中的自定義類
今天先寫到這裏,明天繼續
創建第一個可以使用Slua框架調用UnityEngine的Unity項目
作者:肖浩唄
鏈接:http://www.jianshu.com/p/2dc2b816f1a4
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請註明出處。
Lua--------------------unity3D與Slua融合使用