uluaFramework--框架简单介绍

uluaFramework 下载地址:
git clone https://github.com/jarjin/LuaFramework_UGUI.git
框架配置
public class AppConst
{
    public const bool DebugMode = false;                       //调试模式-用于内部测试
    /// <summary>
    /// 如果想删掉框架自带的例子,那这个例子模式必须要
    /// 关闭,否则会出现一些错误。
    /// </summary>
    public const bool ExampleMode = true;                       //例子模式 

    /// <summary>
    /// 如果开启更新模式,前提必须启动框架自带服务器端。
    /// 否则就需要自己将StreamingAssets里面的所有内容
    /// 复制到自己的Webserver上面,并修改下面的WebUrl。
    /// </summary>
    public const bool UpdateMode = false;                       //更新模式-默认关闭 
    public const bool LuaByteMode = false;                       //Lua字节码模式-默认关闭 
    public const bool LuaBundleMode = true;                    //Lua代码AssetBundle模式

    public const int TimerInterval = 1;
    public const int GameFrameRate = 30;                        //游戏帧频

    public const string AppName = "LuaFramework";               //应用程序名称
    public const string LuaTempDir = "Lua/";                    //临时目录
    public const string AppPrefix = AppName + "_";              //应用程序前缀
    public const string ExtName = ".unity3d";                   //素材扩展名
    public const string AssetDir = "StreamingAssets";           //素材目录 
    public const string WebUrl = "http://localhost:6688/";      //测试更新地址

    public static string UserId = string.Empty;                 //用户ID
    public static int SocketPort = 0;                           //Socket服务器端口
    public static string SocketAddress = string.Empty;          //Socket服务器地址

    public static string FrameworkRoot
    {
        get
        {
            return Application.dataPath + "/" + AppName;
        }
    }
}
游戏主入口
/// </summary>
public class Main : MonoBehaviour
{
    void Start()
    {
        AppFacade.Instance.StartUp();   //启动游戏
    }
}

通过StartUp添加游戏控制组件
GameObject gameMgr = GameObject.Find("GlobalGenerator");
if (gameMgr != null)
{
    AppView appView = gameMgr.AddComponent<AppView>();
}
//-----------------关联命令-----------------------
AppFacade.Instance.RegisterCommand(NotiConst.DISPATCH_MESSAGE, typeof(SocketCommand));

//-----------------初始化管理器-----------------------
AppFacade.Instance.AddManager<LuaManager>(ManagerName.Lua);
AppFacade.Instance.AddManager<PanelManager>(ManagerName.Panel);
AppFacade.Instance.AddManager<SoundManager>(ManagerName.Sound);
AppFacade.Instance.AddManager<TimerManager>(ManagerName.Timer);
AppFacade.Instance.AddManager<NetworkManager>(ManagerName.Network);
AppFacade.Instance.AddManager<ResourceManager>(ManagerName.Resource);
AppFacade.Instance.AddManager<ThreadManager>(ManagerName.Thread);
AppFacade.Instance.AddManager<ObjectPoolManager>(ManagerName.ObjectPool);
AppFacade.Instance.AddManager<GameManager>(ManagerName.Game);
GameManager
游戏主要运行逻辑入口
检擦资源文件—首次运行时将StreamingAssets文件解压到PresidentData文件夹下,然后比对files.txt文件内容,与远程服务器版本信息比对,添加下载文件任务。
public void CheckExtractResource() 
{
    bool isExists = Directory.Exists(Util.DataPath) &&
      Directory.Exists(Util.DataPath + "lua/") && File.Exists(Util.DataPath + "files.txt");
    if (isExists || AppConst.DebugMode) {
        StartCoroutine(OnUpdateResource());
        return;   //文件已经解压过了,自己可添加检查文件列表逻辑
    }
    StartCoroutine(OnExtractResource());    //启动释放协成 
}
文件解压过程
IEnumerator OnExtractResource()
{
    string dataPath = Util.DataPath;  //数据目录
    string resPath = Util.AppContentPath(); //游戏包资源目录

    if (Directory.Exists(dataPath)) Directory.Delete(dataPath, true);
    Directory.CreateDirectory(dataPath);

    string infile = resPath + "files.txt";
    string outfile = dataPath + "files.txt";
    if (File.Exists(outfile)) File.Delete(outfile);

    string message = "正在解包文件:>files.txt";
    Debug.Log(infile);
    Debug.Log(outfile);
    if (Application.platform == RuntimePlatform.Android)
    {
        WWW www = new WWW(infile);
        yield return www;

        if (www.isDone)
        {
            File.WriteAllBytes(outfile, www.bytes);
        }
        yield return 0;
    }
    else File.Copy(infile, outfile, true);
    yield return new WaitForEndOfFrame();

    //释放所有文件到数据目录
    string[] files = File.ReadAllLines(outfile);
    foreach (var file in files)
    {
        string[] fs = file.Split('|');
        infile = resPath + fs[0];  //
        outfile = dataPath + fs[0];

        message = "正在解包文件:>" + fs[0];
        Debug.Log("正在解包文件:>" + infile);
        facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message);

        string dir = Path.GetDirectoryName(outfile);
        if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

        if (Application.platform == RuntimePlatform.Android)
        {
            WWW www = new WWW(infile);
            yield return www;

            if (www.isDone)
            {
                File.WriteAllBytes(outfile, www.bytes);
            }
            yield return 0;
        }
        else
        {
            if (File.Exists(outfile))
            {
                File.Delete(outfile);
            }
            File.Copy(infile, outfile, true);
        }
        yield return new WaitForEndOfFrame();
    }
    message = "解包完成!!!";
    facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message);
    yield return new WaitForSeconds(0.1f);

    message = string.Empty;
    //释放完成,开始启动更新资源
    StartCoroutine(OnUpdateResource());
}
文件下载
这里采用的是单线程同步下载,后期项目中可以根据需求改成多线程异步下载方式
IEnumerator OnUpdateResource()
{
    if (!AppConst.UpdateMode)
    {
        OnResourceInited();
        yield break;
    }
    string dataPath = Util.DataPath;  //数据目录
    string url = AppConst.WebUrl;
    string message = string.Empty;
    string random = DateTime.Now.ToString("yyyymmddhhmmss");
    string listUrl = url + "files.txt?v=" + random;
    Debug.LogWarning("LoadUpdate---->>>" + listUrl);

    WWW www = new WWW(listUrl); yield return www;
    if (www.error != null)
    {
        OnUpdateFailed(string.Empty);
        yield break;
    }
    if (!Directory.Exists(dataPath))
    {
        Directory.CreateDirectory(dataPath);
    }
    File.WriteAllBytes(dataPath + "files.txt", www.bytes);
    string filesText = www.text;
    string[] files = filesText.Split('\n');

    for (int i = 0; i < files.Length; i++)
    {
        if (string.IsNullOrEmpty(files[i])) continue;
        string[] keyValue = files[i].Split('|');
        string f = keyValue[0];
        string localfile = (dataPath + f).Trim();
        string path = Path.GetDirectoryName(localfile);
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        string fileUrl = url + f + "?v=" + random;
        bool canUpdate = !File.Exists(localfile);
        if (!canUpdate)
        {
            string remoteMd5 = keyValue[1].Trim();
            string localMd5 = Util.md5file(localfile);
            canUpdate = !remoteMd5.Equals(localMd5);
            if (canUpdate) File.Delete(localfile);
        }
        if (canUpdate)
        {   //本地缺少文件
            Debug.Log(fileUrl);
            message = "downloading>>" + fileUrl;
            facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message);
            /*
            www = new WWW(fileUrl); yield return www;
            if (www.error != null) {
                OnUpdateFailed(path);   //
                yield break;
            }
            File.WriteAllBytes(localfile, www.bytes);
             */
            //这里都是资源文件,用线程下载
            BeginDownload(fileUrl, localfile);
            while (!(IsDownOK(localfile))) { yield return new WaitForEndOfFrame(); }
        }
    }
    yield return new WaitForEndOfFrame();

    message = "更新完成!!";
    facade.SendMessageCommand(NotiConst.UPDATE_MESSAGE, message);

    OnResourceInited();
}
资源更新后回调,去加载Assetbunle中的AssetBundleManifest,同时执行LuaManager中的InitStart方法,确定lua加载路径,关于ulua如果映射C#到lua 可以去源码了解一下
public void OnResourceInited()
{
    ResManager.Initialize(AppConst.AssetDir, delegate ()
    {
        Debug.Log("Initialize OK!!!");
        this.OnInitialize();
    });
}
void OnInitialize()
{
    LuaManager.InitStart();
    LuaManager.DoFile("Logic/Game");         //加载游戏
    LuaManager.DoFile("Logic/Network");      //加载网络
    NetManager.OnInit();                     //初始化网络
    Util.CallMethod("Game", "OnInitOK");     //初始化完成
}
加载Game.lua 和Network.lua文件后,通过LuaMangager中的CallFunction 方法调用Lua脚本中的方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动生成绑定代码文件,非反射调用 大量内建基础类型支持,如枚举,委托,事件,Type, 数组,迭代器等 支持多种协同形式 支持所有unity内部类导出,支持委托类型导出 支持导出自定义,跳过某个空的基类,修改导出名称等 支持扩展函数自定义导出, 比如DoTween 支持值类型Nullable导出,包括Nullable等 支持Lua中function转委托,可以区分需要不同委托的参数的重载函数 支持c# LuaFunction对象转委托,简化调用方式。 支持无GC的多参数调用形式 支持重载函数自动折叠, 如:Convert.ToUInt32只导出double参数的函数 支持重载函数自动排序, 如:参数个数相同, object参数执行级最低, 不会出现错误匹配情况 支持导出函数重命名, 可以分离导出某个重载函数(可以导出被折叠掉的函数) 支持使用编辑器类改写导出规则 支持this数组访问,索引为int可以通过[]访问,其他可使用.get_Item或者.this:get()访问数组成员 支持委托(事件)+-lua function。支持通过函数接口的Add和Remove委托操作 支持静态反射操作, 形式同c# 支持peer表,可在lua端扩展导出的userdata 支持自定义struct压入和读取,做到无GC,并且结构成员无类型限制, 参考例子24 支持preloading, 可以通过requie后绑定wrap文件 支持int64, uint64 大量的lua数学类型,如Quaternion, Vector3, Mathf等 包含第三方lua扩展,包括luasocket, struct, lpeg, utf8, pb等库 当lua出现异常,能够同时捕获c#端和lua端堆栈,便于调试 print信息,在编辑器点击日志, 能自动打开对应lua文件 支持unity所有版本 支持Lua hook C#相代码实现,一定程度上支持利用Lua代码修改C#端代码的bug(暖更新使用说明)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值