Unity Xlua 简洁明了的热更教程(三)

手动搭建服务器

服务器用我之前教程中的Wampserver64来搭建
教程链接
下载好之后,我们把工程文件根目录下的AssetBundles文件夹复制到服务器或者自己电脑上的www文件夹中。
在这里插入图片描述
在这里插入图片描述
现在服务器就搭建完成了!

从服务器上下载ab包并加载到场景中

先打开脚本HotFixScript,输入下面的代码。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;
using UnityEngine.Networking;
/// <summary>
/// 演示脚本
/// </summary>
public class HotFixScript : MonoBehaviour
{
    
    
    private LuaEnv luaEnv;//lua虚拟环境
    private Dictionary<string, GameObject> prefabs = new Dictionary<string, GameObject>();//加载ab包转为gameobject后存入字典
    private void Awake()
    {
    
    
        luaEnv = new LuaEnv();//开启lua虚拟环境
        luaEnv.AddLoader(MyLoader);
        luaEnv.DoString("require 'Test'");
    }
    private byte[] MyLoader(ref string filePath)
    {
    
    
        string absPath = @"H:\xiangmu\Txt\Assets\Lua\" + filePath + ".lua.txt";//在任何地方都可以新建一个存放lua的文件夹,存放Lua代码的路径
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
    }

    private void OnDisable()
    {
    
    
        luaEnv.DoString("require 'LuaDispose'");
    }

    private void OnDestroy()
    {
    
    
        luaEnv.Dispose();//关闭
    }

    [LuaCallCSharp]
    public void LoadResource(string resName,string filePath)//开始加载资源
    {
    
    
        StartCoroutine(Load(resName, filePath));
    }

    IEnumerator Load(string resName, string filePath)//下载资源
    {
    
    
        //UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundles/"+filePath);//本机网址
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(@"http://服务器IP地址/AssetBundles/" + filePath);//输入网址和路径
        yield return request.SendWebRequest();//等待下载完成
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//获取ab包
        GameObject go = ab.LoadAsset<GameObject>(resName);
        if (!prefabs.ContainsKey(resName))//存入字典
            prefabs.Add(resName, go);
        else
            prefabs[resName] = go;
        Debug.Log(prefabs[resName]);
    }

    [LuaCallCSharp]
    public GameObject GetPrefab(string resName)//获取物体
    {
    
    
        return prefabs[resName];
    }
}

新建Load脚本,把代码拖到摄像机,输入一下代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[Hotfix]
public class Load : MonoBehaviour
{
    
    
    public HotFixScript hot;
    // Start is called before the first frame update
    void Start()
    {
    
    
       
    }
    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

把摄像机拖入Hot字段中。
在这里插入图片描述
然后打开Test.lua.txtlua文本,输入一下代码

local UnityEngine=CS.UnityEngine
xlua.hotfix(CS.Cube,'Update',function(self)
    if(UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.S)) then
        self.rigidbody:AddForce(UnityEngine.Vector3.up*200);
    end
end)

xlua.hotfix(CS.Load,'Start',function(self)

self.hot:LoadResource('Sphere','gameobject\\sphere.unity3d')--ab在服务器上的路径

end)

xlua.hotfix(CS.Load,'Update',function(self)

    if(UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.U)) then
        UnityEngine.Object.Instantiate(self.hot:GetPrefab('Sphere'))--生成物体在游戏场景中
    end

end)

然后重新生成并注入代码,然后点击运行。控制台会打印出游戏物体的名字说明已经加载出来了。在这里插入图片描述
然后点击U键,Sphere游戏物体就在场景中被加载出来了。
在这里插入图片描述
此时我们会发现有一个报错
在这里插入图片描述
在Lua文件夹中新建LuaDispose.lua.txt文本文件讲它存为UTF8编码,打开保存

xlua.hotfix(CS.Cube,'Update',nil)
xlua.hotfix(CS.Load,'Start',nil)
xlua.hotfix(CS.Load,'Update',nil)

在HotFixScript.cs脚本中加入一下代码,保存
在这里插入图片描述
然后重新生成注入代码。再运行发现错误消失!

猜你喜欢

转载自blog.csdn.net/weixin_43298513/article/details/134782813