unity热更新框架Xlua--加载lua文件方式

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43679333/article/details/102748414

本视频根据51CTOscholl刘国柱老师网课编写,小白笔记,侵权必删

1.使用TextAsset方式加载文件

在unity中新建C#脚本

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

public class RunluaByfile : MonoBehaviour
{
    //lua环境
    LuaEnv env = null;

    private void Start()
    {
        env = new LuaEnv();

        //通过Resources.Load来加载lua文件
        TextAsset textAsset = Resources.Load<TextAsset>("simpleLua.lua");
        env.DoString(textAsset.ToString());
    }

    private void OnDestroy()
    {
        //释放env
        env.Dispose();
    }

}

在Resources目录中建立文件simpleLua.lua.txt(.txt可以不加,加.txt只是为了好编写),必须在Resources目录下
在这里插入图片描述
载新建的文件中输入以下内容

--通过单独的文件,保存lua内容。常用打开方式。
print("通过单独的lua文件来运行程序。")

然后挂上脚本执行,结果如下:
在这里插入图片描述
若出现乱码或其他错误将文件保存类型改为UTF8在这里插入图片描述

2.使用Require方法是进行加载(商业使用较多)

新建C#脚本:

/***
 * *
 * 使用Require加载文件
 * 
 * ***/

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

public class RunluaByRequire : MonoBehaviour
{

    //lua环境
    LuaEnv env = null;

    private void Start()
    {
        env = new LuaEnv();

        ///使用Require方式加载文件
        env.DoString("require'SimpleLua'");//不用加lua后缀
    }

    private void OnDestroy()
    {
        //释放env
        env.Dispose();
    }
}

require会加载以下路径寻找响应文件
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43679333/article/details/102748414
今日推荐