xLua热更新学习总结

一.xLua配置

1.GitHub 下载xLua压缩包 地址: xLua. 点击Code下载压缩包。
2.解压后,将Assets中四个文件复制到Unity项目Assets文件夹下在这里插入图片描述
3.将Tool文件复制到Assets同级目录下在这里插入图片描述
4.在这里插入图片描述点击Build Settings.
在这里插入图片描述点击Player Settings
在这里插入图片描述OtherSettings的Scripting Define Symbols设置为HOTFIX_ENABLE
然后回车
5.找到Unity安装位置,将Editor\Data\Managed目录下3个dll文件
在这里插入图片描述复制到Unity工程目录Assets\XLua\Src\Editor下

注意:工程目录不能存在中文

二. xLua使用

删除工程中xLua文件夹下的Examples文件夹
1.需要热更新的类加上[Hotfix]标签
在这里插入图片描述
2.需要热更的方法加上 [LuaCallCSharp]标签
在这里插入图片描述
3.编写lua脚本
例如: fish.lua.txt 文件
xlua.hotfix(CS.Gun,‘Attack’,function(self)
—用Lua实现业务逻辑
end)
fishDispose.lua.txt文件
xlua.hotfix(CS.Gun,‘Attack’,nil)

访问类的私有成员和方法xlua.private_accessible(CS.Gun)
4.在C#中和Xlua交互。编写HotFixScript脚本

using UnityEngine;
using XLua;
using System.IO;
public class HotFixScript : MonoBehaviour 
{
    private LuaEnv luaEnv;

    private void Start()
    {
        luaEnv = new LuaEnv(); //创建xLua虚拟环境
        luaEnv.AddLoader(MyLoader);//加载txt中xLua代码
        luaEnv.DoString("require 'fish'");
    }

    private byte[] MyLoader(ref string filePath) {//自定义加载器
        string absPath = @"E:\unity3d progect\XluaProjects\PlayerGamePackage\"+filePath+".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
    }
    private void OnDisable()
    {
        luaEnv.DoString("require 'fishDispose'");//注销引用
    }

    private void OnDestroy()
    {
        luaEnv.Dispose();//注销xLua虚拟环境
    }
}

三.AB包

1.在工程Assets目录下创建Editor文件夹,新建CreateAB类放入其中

using UnityEditor;
using System.IO;
public class CreateAB
{
    [MenuItem("Assets/Build AssetBundle")]
    static void BuildAllAssetBundles() {
        string dir = "AssetBundles";
        if (!Directory.Exists(dir)) {
            Directory.CreateDirectory(dir);
        }
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

运行该代码后,就能将需要打包的资源打包到相对路径的AssetBundles目录下。
2.
(1)本地加载资源

public static Dictionary<string, GameObject> prefabDict = new Dictionary<string, GameObject>();

public static void LoadResource(string resName,string filePath) {
        AssetBundle ab = AssetBundle.LoadFromFile(@"E:\unity3d progect\XluaProjects\FishingJoy\AssetBundles\" + filePath);
        GameObject gameObject = ab.LoadAsset<GameObject>(resName);
        prefabDict.Add(resName, gameObject);
}
public static GameObject GetGameObject(string goName) {

        return prefabDict[goName];
}

(2)本地服务器加载资源

public static Dictionary<string, GameObject> prefabDict = new Dictionary<string, GameObject>();

[LuaCallCSharp]
public void LoadResource(string resName,string filePath) {
    StartCoroutine(LoadResourceCoroutine(resName, filePath));
}

IEnumerator LoadResourceCoroutine(string resName, string filePath)
{
    UnityWebRequest webRequest = UnityWebRequest.GetAssetBundle(@"http://localhost/AssetBundles/" + filePath);
    yield return webRequest.Send();//发送请求
    if (webRequest.isHttpError || webRequest.isNetworkError)
        Debug.Log(webRequest.error);
    else
    {
        Debug.Log(webRequest.downloadHandler.text);
    }
    AssetBundle ab = (webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;  
    GameObject gameObject = ab.LoadAsset<GameObject>(resName); 
    prefabDict.Add(resName, gameObject);
}

[LuaCallCSharp]
public static GameObject GetGameObject(string goName) {

    return prefabDict[goName];
}

3.本地服务器加载lua文件

IEnumerator LoadResourceCorotine()
{        
     UnityWebRequest request = UnityWebRequest.Get(@"http://localhost/work.lua.txt");
     yield return request.Send();
     string str = request.downloadHandler.text;//获取处理器的文本 一堆资源的文本(字节)
     File.WriteAllText(@".../work.lua.txt",str);
     //若还有的话继续加载...注意这些都是内定的lua文件,一般都事先安排好。
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值