表格加载完毕会调用lua脚本的初始化
从自定义的loader中加载
LuaManager完整代码
public class LuaManager : ManagerBase
{
///
/// 全局的xLua引擎
///
public static LuaEnv luaEnv;
///
/// 初始化
///
public void Init()
{
//1. 实例化 xLua引擎
luaEnv = new LuaEnv();
#if ASSETBUNDLE
//2.添加自定义Loader
luaEnv.AddLoader(MyLoader);
//3.加载Bundle
LoadLuaAssetBundle();
#else
//2.设置xLua的脚本路径
luaEnv.DoString(string.Format("package.path = '{0}/?.bytes'", Application.dataPath + "/Download/xLuaLogic/"));
DoString("require 'Main'");
#endif
}
///
/// 当前xLua脚本的资源包
///
private AssetBundle m_CurrAssetBundle;
///
/// 加载xLua的AssetBundle
///
private void LoadLuaAssetBundle()
{
//XLua的AssetBundle的存储路径
GameEntry.Resource.ResourceLoaderManager.LoadAssetBundle(ConstDefine.XLuaAssetBundlePath, onComplete: (AssetBundle bundle) =>
{
m_CurrAssetBundle = bundle;
//然后执行Main.lua,如果没有main.lua,就会去loader中加载
DoString("require 'Main'");
});
}
///
/// 自定义的Loader
///
///
///
private byte[] MyLoader(ref string filepath)
{
byte[] buffer = null;
//所有的lua脚本都会进bundle包,跟资源一样会取最后一个名称
string path = GameEntry.Resource.GetLastPathName(filepath);
//Debug.Log(path);
//加载资源,从资源包中加载
TextAsset asset = m_CurrAssetBundle.LoadAsset(path);
buffer = asset.bytes;
//如果加载出来前三位是239,187,192,说明有bom头,把bom头去掉
if (buffer[0] == 239 && buffer[1] == 187 && buffer[2] == 191)
{
//处理UTF-8 BOM头
buffer[0] = buffer[1] = buffer[2] = 32;
}
Debug.Log("加载成功 Lua文件=>" + asset.name);
return buffer;
}
///
/// 执行Lua脚本
///
///
public void DoString(string str)
{
luaEnv.DoString(str);
}
}
从lua中加载数据表
BaseAction,使用BaseAction委托把MMO_MemoryStream返回到lua中去
using XLua;
namespace YouYou
{
[CSharpCallLua]
public delegate void BaseAction();
[CSharpCallLua]
public delegate void BaseAction(T1 t1);
[CSharpCallLua]
public delegate void BaseAction(T1 t1, T2 t2);
[CSharpCallLua]
public delegate void BaseAction(T1 t1, T2 t2, T3 t3);
[CSharpCallLua]
public delegate void BaseAction(T1 t1, T2 t2, T3 t3, T4 t4);
[CSharpCallLua]
public delegate void BaseAction(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5);
[CSharpCallLua]
public delegate void BaseAction(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6);
[CSharpCallLua]
public delegate void BaseAction(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7);
[CSharpCallLua]
public delegate R BaseFunc();
[CSharpCallLua]
public delegate R BaseFunc(T1 t1);
}
Lua中进行改造,调用C#中的LoadDataTable加载表格,加载完之后返回MMO_MemoryStream,然后存入ChapterEntity中
加载完之后发送一个消息
在ProcedurePreload中有监听
数据表全部加载完毕
最后附上 GameInit.bytes和 ChatperDBModel.bytes脚本
print('启动GameInit')
require "Common/Define"
require "Common/SysEventId"
require "Common/UIFormId"
GameInit = {};
local this = GameInit;
function GameInit.Init()
this.InitCtrlAndViews();
this.LoadDataTables();
this.InitProto();
end
function GameInit.InitCtrlAndViews()
--一个模块
require('Modules/Task/UI_TaskCtrl');
require('Modules/Task/UI_TaskView');
--一个模块
require('Modules/Loading/UI_LoadingCtrl');
require('Modules/Loading/UI_LoadingView');
end
function GameInit.LoadDataTables()
--章表
require('DataNode/DataTable/Create/ChapterEntity');
require('DataNode/DataTable/Create/ChapterDBModel');
ChapterDBModel.LoadList();
--测试用的
local row=ChapterDBModel.GetEntity(1);
print(row.ChapterName);
--任务表
require('DataNode/DataTable/Create/TaskEntity');
require('DataNode/DataTable/Create/TaskDBModel');
TaskDBModel.LoadList();
end
local m_TotalTableCount = 0; --总共需要加载的表格数量
local m_CurrLoadTableCount =0; --当前已加载的表格数量
--总共需要加载的表格数量++
function GameInit.AddTotalLoadTableCount()
m_TotalTableCount = m_TotalTableCount + 1;
end
--单个数据表加载完毕回调
function GameInit.LoadOneTableComplete()
--当前已加载的表格数量++
m_CurrLoadTableCount = m_CurrLoadTableCount + 1;
if(m_TotalTableCount==m_CurrLoadTableCount) then
--所有数据表加载完毕
GameEntry.Event.CommonEvent:Dispatch(SysEventId.LoadLuaDataTableComplete);
end
end
--初始化通讯协议
function GameInit.InitProto()
require("Common/json");
require('DataNode/Proto/ProtoCodeDef');
require('DataNode/ProtoHandler/SocketProtoListenerForLua');
SocketProtoListenerForLua.AddProtoListener();
end
--数据访问
ChapterDBModel = { }
local this = ChapterDBModel;
local chapterTable = { }; --定义表格
function ChapterDBModel.LoadList()
--开始加载的时候总数目加1
GameInit.AddTotalLoadTableCount();
CS.YouYou.GameEntry.Lua:LoadDataTable("Chapter", this.LoadFormMS);
end
function ChapterDBModel.LoadFormMS(ms)
local rows = ms:ReadInt();
ms:ReadInt();
for i = 0, rows, 1 do
chapterTable[#chapterTable + 1] = ChapterEntity.New(
ms:ReadInt(),
ms:ReadUTF8String(),
ms:ReadInt(),
ms:ReadUTF8String(),
ms:ReadFloat(),
ms:ReadFloat()
);
end
--告诉GameInit某一个表加载完毕了,当前数目+1
GameInit.LoadOneTableComplete();
end
function ChapterDBModel.GetList()
return chapterTable;
end
function ChapterDBModel.GetEntity(id)
local ret = nil;
for i = 1, #chapterTable, 1 do
if (chapterTable[i].Id == id) then
ret = chapterTable[i];
break;
end
end
return ret;
end
最后事件系统进行过优化
对应的SocketEvent也做了对应的修改