zoukankan      html  css  js  c++  java
  • Unity热更新03-C#调用XLua-02-用户自定义加载Lua脚本

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using XLua;
    
    public class Lesson2_Loader : MonoBehaviour
    {
        void Start()
        {
            LuaEnv env = new LuaEnv();
    
            //xlua提供的一个 路径重定向 的方法
            //允许我们自定义 加载 Lua文件的规则
            //当我们执行Lua语言 require 时 相当于执行一个lua脚本
            //它就会 执行 我们自定义传入的这个函数
            env.AddLoader(MyCustomLoader);
            //最终我们其实 会去AB包中加载 lua文件 
    
            env.DoString("require('Main')");
        }
    
        //自动执行
        private byte[] MyCustomLoader(ref string filePath)
        {
            //通过函数中的逻辑 去加载 Lua文件 
            //传入的参数 是 require执行的lua脚本文件名
            //拼接一个Lua文件所在路径
            string path = Application.dataPath + "/Lua/" + filePath + ".lua";
            Debug.Log(path);
    
            //有路径 就去加载文件 
            //File知识点 C#提供的文件读写的类
            //判断文件是否存在
            if ( File.Exists(path) )
            {
                return File.ReadAllBytes(path);
            }
            else
            {
                Debug.Log("MyCustomLoader重定向失败,文件名为" + filePath);
            }
    
            
            return null;
        }
    }
    
    

  • 相关阅读:
    Java实现多线程的四种实现方式
    电梯调度算法[转]
    带黑洞的随机游走问题
    深度学习印象
    使用jupyterthemes插件定制jupyter notebook界面
    tf.gfile
    中国象棋残局库构建[抄]
    Android(Linux)线路规程的使用
    Remote Displayer for Android V1.2
    Android开发资源汇总
  • 原文地址:https://www.cnblogs.com/unitysir/p/13890898.html
Copyright © 2011-2022 走看看