xlua热更新简单demo

1.参考资料

参考视频教学
环境下载

2.实现的内容

点击鼠标生成一个立方体,然后用xlua热更新,使得其点击鼠标能生成一个球体

3.环境安装

从github下载xlua的开发框架,然后把框架Assets里面的内容复制到自己新建的工程的Assets目录下,然后复制Tools里面的内容到根目录。
设置Player Setting里面的Scripting Define Symbols的值为HOTFIX_ENABLE
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CbIw03Zh-1586317505950)(en-resource://database/1495:0)]

4.实现步骤

4.1完成基础功能

1.新建一个Cube(命名为AA),然后拖到Resources文件夹(没有就新建一个)下,生成一个prefab
2.新建xluaTest脚本,修改update函数
3.打上[Hotfix]标签来支持热更新

[Hotfix]
public class xluaTest : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GameObject preGo = Resources.Load("AA") as GameObject;
            GameObject go = GameObject.Instantiate(preGo);       
        }
    }
}

3.新建一个空物体,挂载xluaTest脚本

4.2准备资源文件

因为我们需要将cube替换成sphere,然后还需要进行热更新,因此我们需要把执行热更新的lua文件以及sphere打成AB包,然后进行加载。

4.2.1.新建一个编辑器脚本进行AB打包。

public class MyTools : Editor
{
    [MenuItem("Tools/CreatBundle")]
    static void CreateAssetBundle()
    {
        string path = "AB";
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
        DateTime now = DateTime.Now;
        string ver = now.ToString("yyyyMMddff");
        File.WriteAllText(path+@"/version.txt",ver);
        Debug.Log("finish");
    }
}

值得注意的是,我们还打包了一个版本信息文件,版本信息传入了当前的时间作为参数,因为加载包的时候,如果版本一样的话,工程将会在缓存中进行读取,而不会进行加载,所以每次打包需要传入不一样的值,从而使得每次都能下载新的AB包而不是从缓存中进行读取。

4.2.2 新建一个球以及一个txt文件,并打包

新建一个球体(命名为BB)以及一个txt文件(命名为demo1.lua.txt),放到Resources文件夹下面
在这里插入图片描述
然后编写lua脚本
脚本内容是用这个lua脚本替换xluaTest里面的Update函数
当然这里面涉及到了一个download组件,后面将进行叙述,downl组件里面的assetBundle变量存储下载的AB包

xlua.private_accessible(CS.xluaTest)
xlua.hotfix(CS.xluaTest,"Update",
function(self)
       local cc = CS.UnityEngine
       if cc.Input.GetMouseButtonDown(0) then
              local dGo = cc.GameObject.Find("tst")
              local t = dGo:GetComponent("download").assetBundle
              local obj = t:LoadAsset("BB")
              local go = cc.GameObject.Instantiate(obj)
              go.transform.position = cc.Vector3(5,5,5)
       end
end
)

接下来执行Tools->CreatBundle进行程序打包。
可以在根目录发现AB文件夹,里面内容如下:
在这里插入图片描述

4.3 编写download脚本

上面打包好的AB包实际上是应该放到服务器的,我们假定自己的服务器的D盘的根目录,也就是我们把上面的AB文件夹复制到D盘跟目录中,这个脚本的作用是首先读取版本号,如果版本号一样则不进行读取,否则读取新的AB包,并把AB包中的lua脚本存储到本地。

using UnityEngine;
using System.Collections;
using System.IO;
public class download : MonoBehaviour
{
    string path = @"D:\AB\aabb.u3d";//@可以去转义字符
    string pathVer = @"D:\AB\version.txt";
    public AssetBundle assetBundle;
    private void Awake()
    {
        StartCoroutine(loadFile());
    }
    IEnumerator loadFile()
    {
        WWW wwwVersion = new WWW(pathVer);
        yield return wwwVersion;
        if (!string.IsNullOrEmpty(wwwVersion.error))
        {
            Debug.Log(wwwVersion.error);
            yield break;
        }
        int verValue = int.Parse(wwwVersion.text);
        //Caching.ClearCache();
        WWW w1 = WWW.LoadFromCacheOrDownload(path, verValue);//读取版本号信息
        yield return w1;
        assetBundle = w1.assetBundle;
        TextAsset hot = assetBundle.LoadAsset("demo1.lua.txt") as TextAsset;
        string newpath = Application.persistentDataPath + @"/demo1.lua.txt";
        if (!File.Exists(newpath))
        {
            File.Create(newpath);
        }
        File.WriteAllText(newpath,hot.text);
    }
}

4.4 编写excuteHotfix脚本

这个脚本用来执行读取的lua脚本,lua.DoString(str)可直接传入lua脚本进行执行

using UnityEngine;
using XLua;
using System.IO;
public class excuteHotfix : MonoBehaviour
{
    private void Awake()
    {
        LuaEnv lua = new LuaEnv();
        lua.AddLoader(myLoader);
        lua.DoString("require'demo1'");
    }
    public byte[] myLoader(ref string filepath)
    {
        string newpath = Application.persistentDataPath + @"/" + filepath + 
".lua.txt";
        string txtstring = File.ReadAllText(newpath);
        return System.Text.Encoding.UTF8.GetBytes(txtstring);
    }
}

4.5 生成以及注入代码

依次点击Xlua->Generate Code
Xlua->Hotfix Inject In Editor
把前面的Download脚本和ExcuteHotfix脚本挂到tst上,即可。

发布了31 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43813453/article/details/105383993
今日推荐