Пример #1
0
    public void Init()
    {
        if (luaEnv != null)
        {
            return;
        }

        luaEnv = new LuaEnv();
        //加载Lua脚本 重定向

        luaEnv.AddLoader((ref string filepath) =>
        {
            string path = Application.dataPath + "/Lua/" + filepath + ".lua";
            if (File.Exists(path))
            {
                return(File.ReadAllBytes(path));
            }
            else
            {
                Debug.LogError("文件不存在");
            }
            return(null);
        });

        //加载AB包的Lua脚本 重定向
        luaEnv.AddLoader((ref string filepath) =>
        {
            //Debug.Log("进入AB包重定向加载");
            ////从AB包中加载lua文件
            ////加载AB包
            //string path = Application.streamingAssetsPath + "/lua";
            //AssetBundle assetBundle = AssetBundle.LoadFromFile(path);

            ////加载Lua文件 返回 filepath + ".lua"是文件名
            //TextAsset tx = assetBundle.LoadAsset<TextAsset>(filepath + ".lua");
            ////加载lua文件 byte数组
            //return tx.bytes;
            //=====================================================================
            //通过AB包管理器加载的Lua脚本资源
            TextAsset lua = ABManager.GetInstance().LoadRes <TextAsset>("lua", filepath + ".lua");
            if (lua != null)
            {
                return(lua.bytes);
            }
            else
            {
                Debug.LogError("重定向失败,文件名为:" + filepath);
            }
            return(null);
        });
    }
Пример #2
0
    //要注意这里面要加入后缀,unity识别不了lua文件
    public override byte[] ReadFile(string fileName)
    {
        //如果想重新定义 解析lua的方式 那么只需要在该函数中去写逻辑
        Debug.Log("自定义解析方式");

        //如果没有lua后缀 加上最好 不管从Resources或者AB包中加载都不支持.lua后缀 所以toLua加上bytes后缀
        //我们自己可以加上.txt后缀
        if (!fileName.EndsWith(".lua"))
        {
            fileName += ".lua";
        }
        byte[] buffer = null;

        //进行热更行的lua代码 肯定是我们自己写的上层lua逻辑
        //第二种从AB包中加载文件

        //CSharpCallLua/Lesson2_Loader这样的名字 但是在AB包中我们需要文件名 所以需要拆分一下
        string[]  strs    = fileName.Split('/');
        TextAsset luaCode = ABManager.GetInstance().LoadRes <TextAsset>("lua", strs[strs.Length - 1]);

        if (luaCode != null)
        {
            buffer = luaCode.bytes;
            Resources.UnloadAsset(luaCode);
        }
        //tolua的自带逻辑和自带类 我们不需要去热更新 直接从resources中加载

        if (buffer == null)
        {
            //第一种从Resources
            string    path = "Lua/" + fileName;
            TextAsset text = Resources.Load <TextAsset>(path);
            if (text != null)
            {
                buffer = text.bytes;
                //卸载使用后文本资源
                Resources.UnloadAsset(text);
            }
        }


        //返回bytes资源
        return(buffer);
    }
Пример #3
0
    private void Start()
    {
        // 测试同步加载
        GameObject obj1 = ABManager.GetInstance().LoadResource("model", "Cube") as GameObject;

        obj1.transform.position = -Vector3.up;
        GameObject obj2 = ABManager.GetInstance().LoadResource("model", "Cube", typeof(GameObject)) as GameObject;

        obj2.transform.position = -Vector3.down;
        GameObject obj3 = ABManager.GetInstance().LoadResource <GameObject>("model", "Cube");

        obj3.transform.position = -Vector3.left;


        // 测试异步加载
        ABManager.GetInstance().LoadResourceAsync("model", "Cube",
                                                  ob1 => { (ob1 as GameObject).transform.position = Vector3.up * 3; });
        ABManager.GetInstance().LoadResourceAsync("model", "Cube", typeof(GameObject),
                                                  ob2 => { (ob2 as GameObject).transform.position = Vector3.down * 3; });
        ABManager.GetInstance().LoadResourceAsync <GameObject>("model", "Cube",
                                                               ob3 => { ob3.transform.position = Vector3.right * 3; });
    }