Thermal frame design updates lua

       Currently the big game project contains part of VR and AR project, we need hot update and modify Bug online and other functions to achieve, although Xlua and other plug-ins have been given about the two-way seamless calls between Lua and C # language, but it is hot update the architecture has not put forward, which requires the majority of the game's developers to design their own set of lua framework.
      Early hot update the concept and implementation of mechanisms to achieve the country has just passed the time, a lot of companies using pure lua script way to build the entire project. In this way the development is slow and error-prone, development efficiency is not high. So then the companies on pure lua hot update technology, to carry out their own architecture, such as the introduction of the concept of hierarchical MVC, and then by way of mapping between C # and lua, lua files to make the acquisition life cycle function unity, in order to achieve higher development efficiency.
      I think the best way should be relatively stable business functions with features for developing C #, but it is only part of the frame processing lua game project business requirements with the event frequently updated business needs, such as the game's "Daily Bulletin", " daily tasks "and so on. The above problem is more complex, the author of this article, it is plain language to develop a simple lua lua framework of systems, business functions to achieve the above-described implementation.
     First, in order to avoid direct ocean depth code, first give an overall design framework I designed it for reference.

以上架构图从LuaStartGame这个C#脚本入口开始,然后后续业务基本采用lua语言编写。LuaStartGame.cs 脚本如下。
namespace LuaFramework{
    public class LuaGameStart : MonoBehaviour{
        void Start()
        {
            LuaHelper.GetInstance().DoString("require 'StartGame'");
        }
    }//Class_end
}//namespace_end

    以上项目中的LuaHelper封装了Xlua的环境上下文(LuaEnv)、自定义lua文件的存取路径(不再使用xlua默认的Resources)以及常用lua方法等。
   StartGame.lua 脚本基本没有太多业务,主要起到承上启下,便于日后扩充的中间“过渡”脚本使用,代码如下:

---
---  Lua项目开始入门脚本
---
--引入项目常量与“枚举”
require("SysDefine")
--引入项目初始化核心脚本
require("ProjectInit")

--项目开始
ProjectInit.Init()

       以上脚本SysDefine中以“表”(Table)的形式定义了项目中可能用到的所有“控制层”与“显示层”的lua脚本,以方便在后续lua脚本中使用,这个机制类似于C#中使用一个类来集中定义项目中所有的常量与枚举等类型。
      ProjectInit.lua 是本lua框架的一个核心,负责缓存项目中所有的“视图层”与“控制层”脚本。本脚本通过加载首个业务窗体(UIRootCtrl.lua)实现后续业务开发的持续运行。这里需要特别说明的是CtrlMgr.lua 脚本是负责缓存项目中所有控制层脚本的实例,以及提供控制层脚本访问的入口方法。


---
---  “lua框架”项目初始化
---
---   功能:
---      1: 引入项目中所有的视图层脚本
---      2: 通过CtrlMgr.lua (控制层)脚本,来缓存系统中所有其他控制层脚本。
---      3: 提供访问其他控制层脚本的入口函数。
---      4: 调用项目中第一个UI预设控制层脚本。
---
---
--引入控制层管理器脚本
require("CtrlMgr")

ProjectInit={}
local this=ProjectInit

function ProjectInit.Init()
    --导入引入项目中所有的视图层脚本
    this.ImportAllViews()
    --lua控制器初始化
    CtrlMgr.Init()
    --加载UI‘根窗体’控制脚本
    CtrlMgr.StartProcess(CtrlName.UIRootCtrl)
end

--导入引入项目中所有的视图层脚本
function ProjectInit.ImportAllViews()
    for i = 1, #ViewNames do
        require(tostring(ViewNames[i]))
    end
end

       再往后的lua代码都与具体的业务功能实现有关系。基本都按照设计成对出现,例如本演示项目中的 UIRootCtrl.lua 与UIRootView.lua ,表示加载与显示UI根窗体。*Ctrl.lua定义玩家看不见的业务逻辑,例如加载与解析从服务器端传来的ab包资源,然后关于UI窗体内部的控件显示、显示方位、控件的事件注册等业务,都有*View.lua负责处理。这样实现了架构设计中的分层设计原理。
      最后值得说明的是,*View.lua 是负责显示具体3D/2D游戏预设资源的脚本。其内部定义预设显示的“方式”、“具体内容”与“行为”(包含事件注册)等。而本部分我们采用了xlua的映射技术,使得“lua显示”脚本具备了Unity的常用生命周期函数,进一步大大简化了lua编写业务的难度,例如定义了常见的:Awake()、Start()、Update()、OnDestroy()等函数。
      为了避免编写超长的技术博客文章,关于其他代码的具体实现部分,笔者在最后提供lua架构的演示项目下载链接,供广大有兴趣学员学习研究,共同进步。

下载链接与二维码:
    链接:https://pan.baidu.com/s/1BobsN0c_4bBr1LSwF2li3Q
    提取码:bquu

 

Guess you like

Origin www.cnblogs.com/LiuGuozhu/p/10984871.html