[Unity]技術(shù)學(xué)習(xí)路線圖(長(zhǎng)期更新)
下載與安裝
- 下載地址 GitHub
- 安裝過程
1.下載最新版,這里, 解壓縮霹期,將Assets目錄里的所有內(nèi)容復(fù)制到你的工程中,對(duì)于最終產(chǎn)品拯田,可以刪除slua_src历造,例子,文檔等內(nèi)容船庇,如果是開發(fā)階段則無所謂吭产。
2.等待unity編譯完畢,如果一切順利的話鸭轮,將出現(xiàn)slua菜單臣淤, 點(diǎn)擊slua菜單中 All->Make 命令 手動(dòng)生成針對(duì)當(dāng)前版本的U3d接口文件。
3.每次更新slua版本窃爷,務(wù)必記得clear all邑蒋,然后make all姓蜂,否則可能運(yùn)行不正確
主要的內(nèi)容包括
-
LuaState
狀態(tài)機(jī)對(duì)象執(zhí)行Lua
字符串 -
LuaState
狀態(tài)機(jī)對(duì)象執(zhí)行Lua
腳本 -
LuaState
狀態(tài)機(jī)對(duì)象調(diào)用Lua
腳本內(nèi)的自定義函數(shù) -
LuaState
狀態(tài)機(jī)對(duì)象注冊(cè)C#
自定義類 -
Lua
腳本中調(diào)用C#
中的自定義類
創(chuàng)建第一個(gè)可以使用Slua框架的Unity項(xiàng)目
-
在
MainCamera
對(duì)象上創(chuàng)建AppDelegate.cs
組件using UnityEngine; using System.Collections; public class AppDelegate : MonoBehaviour { void Start () { //在下方添加初始化代碼 } }
-
使用
LuaState
狀態(tài)機(jī)對(duì)象執(zhí)行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
狀態(tài)機(jī)對(duì)象執(zhí)行Lua腳本文件HelloLua.lua
-
在
Resources
文件夾下添加HelloLua.lua
文件print("Lua Scripts:Hello");
-
在
AppDelegate.cs
中設(shè)置LuaState.loaderDelegate
啟動(dòng)文件委托代理using UnityEngine; using System.Collections; using SLua; using System.IO; public class AppDelegate : MonoBehaviour { void Start () { //設(shè)置腳本啟動(dòng)代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執(zhí)行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); } }
-
在
AppDelegate.cs
中通過LuaState
對(duì)象執(zhí)行HelloLua.lua
腳本using UnityEngine; using System.Collections; using SLua; using System.IO; public class AppDelegate : MonoBehaviour { void Start () { //設(shè)置腳本啟動(dòng)代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執(zhí)行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); //設(shè)置執(zhí)行腳本 LuaState ls_state = new LuaState (); ls_state.doFile ("HelloLua.lua"); } }
-
-
通過
LuaState
對(duì)象獲取并執(zhí)行HelloLua.lua
腳本中的一個(gè)函數(shù)-
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初始化時(shí)的回調(diào)函數(shù)特性函數(shù) [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int init(IntPtr L) { //設(shè)置初始化LuaObject對(duì)象 LuaObject.init(L); return 0; } void Start () { //創(chuàng)建狀態(tài)機(jī)對(duì)象 LuaState ls_state = new LuaState (); //設(shè)置腳本啟動(dòng)代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執(zhí)行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); //初始化LuaState狀態(tài)機(jī)與C#的轉(zhuǎn)換對(duì)象 LuaState.pcall (ls_state.L, init); //設(shè)置狀態(tài)機(jī)對(duì)象的執(zhí)行腳本 ls_state.doFile ("HelloLua.lua"); //獲取腳本中的mul函數(shù) LuaFunction mul = ls_state.getFunction ("mul"); //調(diào)用該函數(shù)并且接收返回值 double result = (double)mul.call (-2, 3); Debug.Log(result); } }
-
-
自定義C#對(duì)象
LOHuman.cs
,在HelloLua.lua
中LOHuman.cs
using System; using LuaInterface; using SLua; //該特性可以修飾以下類將會(huì)注冊(cè)到Slua執(zhí)行環(huán)境中 [CustomLuaClass] public class HHHuman { //年齡成員 protected int age = 0; //姓名成員 protected string name = ""; //添加Lua代碼中的靜態(tài)函數(shù) [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] [StaticExport] public static int CreateHuman(IntPtr l) { HHHuman item = new HHHuman (); LuaObject.pushObject (l, item); //只執(zhí)行了1次LuaObject.push,返回值寫1 return 1; } public int Age { set; get; } public string Name{ set; get; } }
- 通過點(diǎn)擊菜單欄中的
SLua->Custom->Clear
將舊版本的自定義類刪除 - 通過點(diǎn)擊菜單欄中的
SLua->Custom->Make
重新制作適用于SLua的新的自定義類Lua_HHHuman.cs
- 默認(rèn)存放目錄
Assets->SLua->LuaObject->Custom->
- 默認(rèn)存放目錄
- 同時(shí)會(huì)自動(dòng)生成一個(gè)
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
函數(shù)中,綁定自定義類HHHuman.cs
//添加LuaState初始化時(shí)的回調(diào)函數(shù)特性函數(shù) [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int init(IntPtr L) { LuaObject.init(L); BindCustom.Bind (L); return 0; }
- 在
HelloLua.lua
腳本中,調(diào)用C#中的自定義類的靜態(tài)函數(shù)CreateHuman()
function testHuman() -- body local human = HHHuman.CreateHuman() -- local list = human:getList() print(human.Age) end
- 在
AppDelegate.cs
的Start
函數(shù)中钱慢,調(diào)用HelloLua.lua
腳本中的testHuman
函數(shù)
static LuaState ls_state; void Start () { //創(chuàng)建狀態(tài)機(jī)對(duì)象 ls_state = new LuaState (); //設(shè)置腳本啟動(dòng)代理 LuaState.loaderDelegate = ((string fn) => { //獲取Lua文件執(zhí)行目錄 string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn; Debug.Log(file_path); return File.ReadAllBytes(file_path); }); //初始化LuaState狀態(tài)機(jī)與C#的轉(zhuǎn)換對(duì)象 LuaState.pcall (ls_state.L, init); //設(shè)置執(zhí)行腳本 ls_state.doFile ("HelloLua.lua"); //獲取testHuman函數(shù) LuaFunction testHuman = ls_state.getFunction ("testHuman"); //無參函數(shù)的調(diào)用 testHuman.call (); }
以上主要的內(nèi)容包括:
-
LuaState
狀態(tài)機(jī)對(duì)象執(zhí)行Lua
字符串 -
LuaState
狀態(tài)機(jī)對(duì)象執(zhí)行Lua
腳本 -
LuaState
狀態(tài)機(jī)對(duì)象調(diào)用Lua
腳本內(nèi)的自定義函數(shù) -
LuaState
狀態(tài)機(jī)對(duì)象注冊(cè)C#
自定義類 -
Lua
腳本中調(diào)用C#
中的自定義類
今天先寫到這里,明天繼續(xù)