Unity使用C#自定义loader

该代码是基于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 'xxxxxxx'");
	}
	
	// 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)
    {
        print(_filePath);
        string s = "print('123')";

        //返回值为null时unity会报错
        return System.Text.Encoding.UTF8.GetBytes(s);
    }

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

猜你喜欢

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