Unity使用C#通过自定义Loader加载指定目录的Lua脚本

该代码是基于XLua,XLua插件下载链接:https://github.com/Tencent/xLua

代码如下:

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

public class CreateLoader : MonoBehaviour {

    private LuaEnv m_luaEnv;

	// Use this for initialization
	void Awake () {

        m_luaEnv = new LuaEnv();

        m_luaEnv.AddLoader(MyLoader);
        m_luaEnv.DoString("require 'test001'");
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    /// <summary>
    /// 自定义loader,如果返回为null则调用内置的loader
    /// </summary>
    /// <param name="_filePath"></param>
    /// <returns></returns>
    private byte[] MyLoader(ref string _filePath)
    {
        string luaPath = Application.streamingAssetsPath + "/" + _filePath + ".lua.txt";

        return System.Text.Encoding.UTF8.GetBytes(System.IO.File.ReadAllText(luaPath));
    }

    private void OnDestroy()
    {
        m_luaEnv.Dispose();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34818497/article/details/79465236