版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/CJB_King/article/details/101784611
SLua结合项目演练
程序启动入口
代码语言:javascript复制public class GameMain : MonoBehaviour {
LuaSvr Svr;
LuaTable lua_Main;
LuaFunction lua_OnDestroy;
LuaFunction lua_Update;
LuaFunction lua_LateUpdate;
LuaFunction lua_FixedUpdate;
[CustomLuaClass]
public delegate void Updatedelegate(object self);
Updatedelegate updateDele;
[CustomLuaClass]
public delegate void LateUpdatedelegate(object self);
LateUpdatedelegate lateUpdateDele;
[CustomLuaClass]
public delegate void FixedUpdatedelegate(object self);
FixedUpdatedelegate fixedUpdateDele;
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
private void Start()
{
InitLua();
}
void InitLua()
{
Svr = new LuaSvr();
Svr.init(null, GameStartUp);
}
void GameStartUp()
{
lua_Main = (LuaTable)Svr.start("lua/main");
lua_OnDestroy = ((LuaFunction)lua_Main["OnDestroy"]);
lua_Update = ((LuaFunction)lua_Main["Update"]);
updateDele = lua_Update.cast<Updatedelegate>();
lua_LateUpdate = ((LuaFunction)lua_Main["LateUpdate"]);
lateUpdateDele = lua_LateUpdate.cast<LateUpdatedelegate>();
lua_FixedUpdate = ((LuaFunction)lua_Main["FixedUpdate"]);
fixedUpdateDele = lua_FixedUpdate.cast<FixedUpdatedelegate>();
}
private void OnDestroy()
{
if(null!= lua_OnDestroy)
{
lua_OnDestroy.call();
}
}
private void Update()
{
if (null != updateDele) updateDele(lua_Main);
}
private void LateUpdate()
{
if (null != lateUpdateDele) lateUpdateDele(lua_Main);
}
private void FixedUpdate()
{
if (null != fixedUpdateDele) fixedUpdateDele(lua_Main);
}
}
Lua入口执行文件
代码语言:javascript复制import "UnityEngine"
if not UnityEngine.GameObject or not UnityEngine.UI then
error("Click Make/All to generate lua wrap file",1)
end
local class={}
webUrl="http://ser/WebServer/StreamingAssets/files.txt"
function main()
print(ApplicationHelper.GetPlatformName())
local www=UnityEngine.WWW(webUrl)
while true do
if www.isDone then
print(www.text)
class.InitFileInfo(www.text)
break
end
end
collectgarbage("collect");
return class
end
function class.InitFileInfo(result)
txtLines={}
txtLines=StringSplit(result,'n')
for k,v in pairs(txtLines) do
if #(v)~=0 then
end
end
end
function StringSplit(input,parttern)
input=tostring(input);
parttern=tostring(parttern);
if (parttern==" ") then return false end
local pos,array=0,{}
for st,ed in function() return string.find(input,parttern,pos,true) end do
table.insert(array, string.sub(input,pos,st-1))
pos=ed 1
end
table.insert(array, string.sub(input,pos))
setmetatable(array,{__tostring=function (array)
str=""
for k,v in pairs(array) do
str=str .. ' ' ..v
end
return str
end})
return array
end
local function callGloablFunc(funcName,...)
local func=rawget(_G,funcName);
if func then
func(...)
end
end
function class.OnDestroy( )
callGloablFunc("GOnDestroy")
end
function class.Update()
callGloablFunc("GUpdate")
end
function class.LateUpdate()
callGloablFunc("GLateUpdate")
end
function class.FixedUpdate()
callGloablFunc("GFixedUpdate")
end