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文件,一般都事先安排好。
 }

猜你喜欢

转载自blog.csdn.net/qq_42368728/article/details/109815907