[Unity]技術(shù)學(xué)習(xí)路線圖(長期更新)
接上篇《[Unity]使用Slua框架開發(fā)Unity項目的重要步驟》
下載與安裝
- 下載地址 GitHub
- 安裝過程
1.下載最新版,這里, 解壓縮,將Assets目錄里的所有內(nèi)容復(fù)制到你的工程中实昨,對于最終產(chǎn)品,可以刪除slua_src犀变,例子,文檔等內(nèi)容秋柄,如果是開發(fā)階段則無所謂获枝。
2.等待unity編譯完畢,如果一切順利的話骇笔,將出現(xiàn)slua菜單省店, 點擊slua菜單中 All->Make 命令 手動生成針對當(dāng)前版本的U3d接口文件。
3.每次更新slua版本笨触,務(wù)必記得clear all懦傍,然后make all,否則可能運行不正確
主要的內(nèi)容包括
-
LuaSvr
環(huán)境對象初始化LuaState
狀態(tài)機對象 -
Lua
中的import
關(guān)鍵字腳本中導(dǎo)入UnityEngine
設(shè)置游戲?qū)ο蠼M件屬性 -
Lua
中的require
關(guān)鍵字導(dǎo)入自定義模塊module
創(chuàng)建新的Unity工程并且導(dǎo)入SLua框架
- 使用已經(jīng)注入
UnityEngie
引擎的狀態(tài)機對象LuaSvr
調(diào)用Lua腳本
AppDelegate.cs
using UnityEngine; using System.Collections; using SLua; public class Main : MonoBehaviour { private LuaSvr lua_svr; // Use this for initialization void Start() { //創(chuàng)建一個已經(jīng)注入UnityEngine的狀態(tài)機對象.. lua_svr = new LuaSvr(); //通過Resources文件夾下的main.txt(lua)文件內(nèi)的main函數(shù)啟動程序 //當(dāng)然也可以通過修改LuaState.loaderDelegate來修改默認(rèn)的路徑 lua_svr.start("main"); } }
main.txt
funcion main() print("Hello LuaSvr...") end
- 在
main.txt
文件內(nèi)導(dǎo)入UnityEngine
包并且創(chuàng)建游戲?qū)ο?import "UnityEngine" function main() -- 創(chuàng)建Cube對象 local cube=UnityEngine.GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Cube) end
- 在
main.txt
文件內(nèi)導(dǎo)入UnityEngine
包并且創(chuàng)建空游戲?qū)ο?import "UnityEngine" function main() -- 創(chuàng)建空物體對象 local empty=UnityEngine.GameObject("HHEmptyObject") end
- 操作游戲?qū)ο笊系慕M件
- 獲取
Transform
組件import "UnityEngine" function main() -- 創(chuàng)建Cube對象 local cube=UnityEngine.GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Cube) -- 獲取transform組件上的position屬性 local pos = cube.transform.position pos.x = 10 -- 修改transform組件的position屬性 cube.transform.position = pos end
- 添加
Rigidbody
組件- 導(dǎo)入
import "UnityEngine"
- 添加剛體組件
cube:AddComponent(Rigidbody)
- 導(dǎo)入
- 獲取指定類型的組件,例如
BoxCollider
- 導(dǎo)入
import "UnityEngine"
- 自定類型字符串獲取
local collider = cube:GetComponent("BoxCollider")
- 導(dǎo)入
- 獲取
由于使用Lua
編寫Unity項目時芦劣,無法與C#相比的是對象函數(shù)或者對象屬性的智能提示不夠健全.
所以在此貼出Slua
框架中注入LuaState
狀態(tài)機對象內(nèi)的一些UnityEngine
的函數(shù)和屬性.
-
UnityEngine.GameObject
對象在LuaState
中注入的內(nèi)容
static public void reg(IntPtr l) {
getTypeTable(l,"UnityEngine.GameObject");
addMember(l,GetComponent);
addMember(l,GetComponentInChildren);
addMember(l,GetComponentInParent);
addMember(l,GetComponents);
addMember(l,GetComponentsInChildren);
addMember(l,GetComponentsInParent);
addMember(l,SetActive);
addMember(l,CompareTag);
addMember(l,SendMessageUpwards);
addMember(l,SendMessage);
addMember(l,BroadcastMessage);
addMember(l,AddComponent);
addMember(l,CreatePrimitive_s);
addMember(l,FindGameObjectWithTag_s);
addMember(l,FindWithTag_s);
addMember(l,FindGameObjectsWithTag_s);
addMember(l,Find_s);
addMember(l,"transform",get_transform,null,true);
addMember(l,"layer",get_layer,set_layer,true);
addMember(l,"activeSelf",get_activeSelf,null,true);
addMember(l,"activeInHierarchy",get_activeInHierarchy,null,true);
addMember(l,"isStatic",get_isStatic,set_isStatic,true);
addMember(l,"tag",get_tag,set_tag,true);
addMember(l,"gameObject",get_gameObject,null,true);
createTypeMetatable(l,constructor, typeof(UnityEngine.GameObject),typeof(UnityEngine.Object));
}
- **只有函數(shù)指針位置的部分粗俱,在Lua中定義成了Table變量內(nèi)的函數(shù),例如:`cube:AddCommponent`**
- **在函數(shù)指針名的末尾部分以`_s`結(jié)尾的,在Lua中定義成了元表變量內(nèi)的函數(shù)虚吟,例如:`GameObject.CreatePrimitive`**
- **在添加成員時寸认,包含了類似于`"transform"`字符串的签财,在Lua中定義成了Table變量內(nèi)的鍵值對屬性,例如:`cube.transform`**
-
UnityEngine.Transform
對象在LuaState
中注入的內(nèi)容static public void reg(IntPtr l) { getTypeTable(l,"UnityEngine.Transform"); addMember(l,SetParent); addMember(l,Translate); addMember(l,Rotate); addMember(l,RotateAround); addMember(l,LookAt); addMember(l,TransformDirection); addMember(l,InverseTransformDirection); addMember(l,TransformVector); addMember(l,InverseTransformVector); addMember(l,TransformPoint); addMember(l,InverseTransformPoint); addMember(l,DetachChildren); addMember(l,SetAsFirstSibling); addMember(l,SetAsLastSibling); addMember(l,SetSiblingIndex); addMember(l,GetSiblingIndex); addMember(l,Find); addMember(l,IsChildOf); addMember(l,FindChild); addMember(l,GetChild); addMember(l,"position",get_position,set_position,true); addMember(l,"localPosition",get_localPosition,set_localPosition,true); addMember(l,"eulerAngles",get_eulerAngles,set_eulerAngles,true); addMember(l,"localEulerAngles",get_localEulerAngles,set_localEulerAngles,true); addMember(l,"right",get_right,set_right,true); addMember(l,"up",get_up,set_up,true); addMember(l,"forward",get_forward,set_forward,true); addMember(l,"rotation",get_rotation,set_rotation,true); addMember(l,"localRotation",get_localRotation,set_localRotation,true); addMember(l,"localScale",get_localScale,set_localScale,true); addMember(l,"parent",get_parent,set_parent,true); addMember(l,"worldToLocalMatrix",get_worldToLocalMatrix,null,true); addMember(l,"localToWorldMatrix",get_localToWorldMatrix,null,true); addMember(l,"root",get_root,null,true); addMember(l,"childCount",get_childCount,null,true); addMember(l,"lossyScale",get_lossyScale,null,true); addMember(l,"hasChanged",get_hasChanged,set_hasChanged,true); createTypeMetatable(l,null, typeof(UnityEngine.Transform),typeof(UnityEngine.Component)); }