美文网首页
XLua_热更新学习三,搭建lua虚拟环境

XLua_热更新学习三,搭建lua虚拟环境

作者: 菜鸟的笔记 | 来源:发表于2019-05-21 16:32 被阅读0次

1.创建C#脚本HotFilx(关联脚本_挂相机上了)

2.编辑脚本HotFilx脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;

/// <summary>
/// 搭建lua虚拟环境
/// </summary>
public class HotFix : MonoBehaviour {
    //lua环境变量
    private LuaEnv luaenv;
    // Use this for initialization
    void Awake() {
        //实例化lua环境变量
        luaenv = new LuaEnv();
        //加载程序(自定义Loader)
        luaenv.AddLoader(MyLoader);
        //执行 fish.lua.txt 文件
        luaenv.DoString("require'fish'");
    }
    
    //Loader
    private byte[] MyLoader(ref string filePath)
    {
        //绝对路径
        //string absPath = @"E:\MyTest\Xlua_HotUpdate\PlayerGamePackage\" + filePath + ".lua.txt";
        //相对路径
        string absPath = Application.streamingAssetsPath+ @"\PlayerGamePackage\"+filePath +".lua.txt";
        //读取lua脚本内容
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath)); 
    }

    //不用的时候释放
    private void OnDestroy()
    {
        luaenv.Dispose();
    }
}

3.Lua脚本fish.lua.txt



4.lua脚本位置(这里用相对路径)


StreamingAssets
5.运行测试
永远不要忘了它 完成

相关文章

网友评论

      本文标题:XLua_热更新学习三,搭建lua虚拟环境

      本文链接:https://www.haomeiwen.com/subject/exujzqtx.html