-
大小: 4.19MB文件類型: .zip金幣: 2下載: 0 次發布日期: 2023-09-16
- 語言: 其他
- 標簽: unity3dlua??
資源簡介
在Unity中實現一個LuaComponent來整合LUA腳本邏輯。

代碼片段和文件信息
/**
?*?LUA組件:Lua游戲邏輯粘合層
?*?
?*?作者:燕良
?*?網址:http://blog.csdn.net/neil3d
?*?QQ群:264656505
?*?
?*/
using?UnityEngine;
using?System.Collections;
using?LuaInterface;
///?
///?Lua組件?-?它調用的Lua腳本可以實現類似MonoBehaviour派生類的功能
///?
[AddComponentMenu(“Lua/LuaComponent“)]
public?class?LuaComponent?:?MonoBehaviour
{
????private?static?LuaState?s_luaState;?//?全局的Lua虛擬機
????[Tooltip(“綁定的LUA腳本路徑“)]
????public?TextAsset?m_luascript;
????public?LuaTable?LuaModule
????{
????????get;
????????private?set;
????}
????LuaFunction?m_luaUpdate;????//?Lua實現的Update函數,可能為null
????///?
????///?找到游戲對象上綁定的LUA組件(Module對象)
????///?
????public?static?LuaTable?GetLuaComponent(Gameobject?go)
????{
????????LuaComponent?luaComp?=?go.GetComponent();
????????if?(luaComp?==?null)
????????????return?null;
????????return?luaComp.LuaModule;
????}
????///?
????///?向一個Gameobject添加一個LUA組件
????///?
????public?static?LuaTable?AddLuaComponent(Gameobject?go?TextAsset?luaFile)
????{
????????LuaComponent?luaComp?=?go.AddComponent();
????????luaComp.Initilize(luaFile);??//?手動調用腳本運行,以取得LuaTable返回值
????????return?luaComp.LuaModule;
????}
????///?
????///?提供給外部手動執行LUA腳本的接口
????///?
????public?void?Initilize(TextAsset?luaFile)
????{
????????m_luascript?=?luaFile;
????????RunLuaFile(luaFile);
????????//--?取得常用的函數回調
????????if?(this.LuaModule?!=?null)
????????{
????????????m_luaUpdate?=?this.LuaModule[“Update“]?as?LuaFunction;
????????}
????}
????///?
????///?調用Lua虛擬機,執行一個腳本文件
????///?
????void?RunLuaFile(TextAsset?luaFile)
????{
????????if?(luaFile?==?null?||?string.IsNullOrEmpty(luaFile.text))
????????????return;
????????if?(s_luaState?==?null)
????????????s_luaState?=?new?LuaState();
????????object[]?luaRet?=?s_luaState.DoString(luaFile.text?luaFile.name?null);
????????if?(luaRet?!=?null?&&?luaRet.Length?>=?1)
????????{
????????????//?約定:第一個返回的Table對象作為Lua模塊
????????????this.LuaModule?=?luaRet[0]?as?LuaTable;
????????}
????????else
????????{
????????????Debug.LogError(“Lua腳本沒有返回Table對象:“?+?luaFile.name);
????????}
????}
????//?MonoBehaviour?callback
????void?Awake()
????{
????????RunLuaFile(m_luascript);
????????CallLuaFunction(“Awake“?this.LuaModule?this.gameobject);
????}
????//?MonoBehaviour?callback
????void?Start()
????{
????????CallLuaFunction(“Start“?this.LuaModule?this.gameobject);
????}
????//?MonoBehaviour?callback
????void?Update()
????{
????????if?(m_luaUpdate?!=?null)
????????????m_luaUpdate.Call(this.LuaModule?this.gameobject);
????}
????///?
????///?調用一個Lua組件中的函數
????///?
????void?CallLuaFunction(string?funcName?params?object[]?args)
????{
????????if?(this.LuaModule?==?null)
????????????return;
????????LuaFunction?func?=?this.LuaModule[funcName]?as?LuaFunction;
????????if?(func?!=?null)
????????????func.Call(ar
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-04-11?11:30??LuaComponentDemo\
?????目錄???????????0??2015-04-11?11:32??LuaComponentDemo\Assets\
?????文件????????3365??2015-04-11?11:32??LuaComponentDemo\Assets\LuaComponent.cs
?????文件?????????178??2015-04-11?11:08??LuaComponentDemo\Assets\LuaComponent.cs.me
?????文件???????18636??2015-04-11?11:16??LuaComponentDemo\Assets\LuaComponentDemo.unity
?????文件??????????90??2015-04-11?11:08??LuaComponentDemo\Assets\LuaComponentDemo.unity.me
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\Android\
?????文件?????2108776??2014-03-06?04:48??LuaComponentDemo\Assets\Plugins\Android\libulua.so
?????文件??????????90??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\Android\libulua.so.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\Android.me
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\iOS\
?????文件??????349348??2014-03-06?04:48??LuaComponentDemo\Assets\Plugins\iOS\libulua.a
?????文件??????????90??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\iOS\libulua.a.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\iOS.me
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\
?????文件????????1782??2014-03-06?04:48??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Info.plist
?????文件??????????90??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Info.plist.me
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\MacOS\
?????文件??????977240??2014-03-06?04:48??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\MacOS\ulua
?????文件??????????90??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\MacOS\ulua.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\MacOS.me
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Resources\
?????目錄???????????0??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Resources\en.lproj\
?????文件??????????92??2014-03-06?04:48??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Resources\en.lproj\InfoPlist.strings
?????文件??????????90??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Resources\en.lproj\InfoPlist.strings.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Resources\en.lproj.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents\Resources.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle\Contents.me
?????文件?????????107??2015-04-11?11:07??LuaComponentDemo\Assets\Plugins\ulua.bundle.me
............此處省略503個文件信息
- 上一篇:redis開發與運維實踐指南
- 下一篇:頻率特性測試儀四篇
評論
共有 條評論