前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SLua About

SLua About

作者头像
bering
发布2019-12-02 13:47:15
5540
发布2019-12-02 13:47:15
举报
文章被收录于专栏:游戏开发之旅游戏开发之旅

版权声明:本文为博主原创文章,遵循 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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SLua结合项目演练
    • 程序启动入口
      • Lua入口执行文件
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档