通过Xlua实现unity热更新的一个小例子

一.介绍

热更新是指在更新游戏资源或者逻辑的时候不需要开发者将游戏再打包、发布、玩家重新下载安装包,而是可以通过将需要更新的资源打包成AssetBundle文件发布到服务器,然后游戏程序通过下载服务器的AssetBundle替换本地文件来达到游戏更新的流程。

​ 打包出来的unity制作的游戏,如果在代码(代码用的是c#语言)上有需要更改的地方,必须重新打包,因为c#语言需要重新编译为dll文件,而如果是lua语言编写,则不需要重新编译。

​ 如果想让lua和c#之间互相调用,可以借用一些开源的Lua插件,比如xLua,toLua。在例子中使用的是xLua插件

二.准备工作

1.xLua

xLua下载方式:https://github.com/Tencent/xLua/releases

下载xlua_v2.1.14.zip

将Tools文件夹复制到工程项目,与Assets同级

将Assets文件夹中的文件复制到工程项目的Assets文件夹里

2.将自己的电脑变成一个本地的服务器

打开控制面板-选择管理工具-IIS-在网站处右键添加网站-填写相关项-启动新建的网站

其中应用程序池要选择DefaultAppPool,物理路径为你希望服务器所在的路径,端口可以填一个二位数字

设置完成后,在浏览器中搜索localhost:端口即可查看服务器存放的文件

3.安装执行Lua的IDE

下载地址:https://www.runoob.com/lua/lua-environment.html

4.在unity中加入HOTFIX_ENABLE标签

选择Edit->Project Setting->Player 右边Inspector列表中

三.打包AssetBundle

1.将需要打包成AB文件的资源添加AssetBundle标签

其中,要打在同一个AB包的资源,标签要求一致

2.编写打包代码

using UnityEditor;
using System.IO;public class CreateAssetBundles
{[MenuItem("Assets/BuildAssetBundles")]static void BuildAllAssetBundles(){//要写两个'/',一个是转义符,string dir = "F://Web Server//AssetBundles";if (Directory.Exists(dir) == false){Directory.CreateDirectory(dir);}//参数 打包路径,压缩方式,打包平台BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);}
}

这里我服务器的路径为F:Web Server

上述脚本可在Editor模式下运行,不需要挂在GameObject上

点击BuildAssetBundles即可将需要打包成AB文件的资源上传到服务器

四.热更新

给步骤三中的cube添加一个用来测试热更新的脚本,这个脚本会在游戏时让cube的scale扩大到原来的2倍,rotation的x,y,z都增加30。类上方添加特性Hotfix使其可热更新。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;[Hotfix]
public class ChangeSelf : MonoBehaviour
{public Vector3 currentScale;public Quaternion currentRotation;public Vector3 laterScale;public Quaternion laterRotation;// Start is called before the first frame updatevoid Start(){currentScale = this.transform.localScale;currentRotation = this.transform.localRotation;ChangeScale(currentScale);ChangeRotation(currentRotation);this.transform.localScale = laterScale;this.transform.localRotation = laterRotation;}public void ChangeScale(Vector3 scale){laterScale = scale * 2;//this.transform.localScale = laterScale;}public void ChangeRotation(Quaternion rotation){laterRotation = Quaternion.Euler(rotation.eulerAngles.x+30, rotation.eulerAngles.y+30, rotation.eulerAngles.z+30);//this.transform.localRotation = laterRotation;}
}

在服务器中添加热更新的lua脚本,路径:

代码:

--hotfix中的参数分别为要进行热更新的类,要更新的方法,方法所在的类(function中的第一个参数指方法所在的类,其他参数为要更新的方法的参数)
xlua.hotfix(CS.ChangeSelf,'ChangeScale',function(self,scale)
self.laterScale = scale * 0.5
end
)
local Quaternion = CS.UnityEngine.Quaternion
local rot = CS.UnityEngine.Quaternion() --新建一个对象
xlua.hotfix(CS.ChangeSelf,'ChangeRotation',function(self,rotation)
self.laterRotation = Quaternion.Euler(rotation.eulerAngles.x+60,rotation.eulerAngles.y+60,rotation.eulerAngles.z+60)
end
)

注意:在SciTE中编写完lua脚本,保存的后缀是lua,如果要被unity识别,需要增加txt的后缀

上述的热更新旨在让cube的scale缩小到原来的一半,rotation的x,y,z都增加60

五.加载

1.加载AssetBundle

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;public class LoadAssetBundles : MonoBehaviour
{// Start is called before the first frame updateIEnumerator Start(){//这里的地址是服务器端的地址 localhost:后的数字是端口string url = @"http://localhost:81/AssetBundles/luatestcube.unity3d";while (Caching.ready == false){yield return null;}using (var www = WWW.LoadFromCacheOrDownload(@"http://localhost:81/AssetBundles/luatestcube.unity3d", 2)){yield return www;if (!string.IsNullOrEmpty(www.error)){Debug.Log(www.error);yield return null;}var assetBundle = www.assetBundle;//var asset = assetBundle.mainAsset;GameObject cubePrefab = assetBundle.LoadAsset<GameObject>("luatestcube");Instantiate(cubePrefab);}  }
}

该脚本可挂在空物体上

2.加载lua脚本

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using XLua;public class HotFixTests : MonoBehaviour
{private LuaEnv m_kLuaEnv;// Start is called before the first frame updatevoid Start(){//xlua虚拟机m_kLuaEnv = new LuaEnv();    //查找指定路径下lua热更新文件string path = Application.persistentDataPath + "/ChangeSelf.lua.txt";//用协程下载读取文件内容StartCoroutine(DownloadFile(path));}public IEnumerator DownloadFile(string path){WWW www = new WWW(path);yield return www;if (www.isDone){System.IO.StreamReader sr = new System.IO.StreamReader(path, Encoding.UTF8);if (sr != null){//执行lua中的语句m_kLuaEnv.DoString(sr.ReadToEnd());}}}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;public class WWWTexts : MonoBehaviour
{private string urlPath = @"http://localhost:81/Xlua/ChangeSelf.lua.txt";//资源网络路径private string file_SaveUrl;//资源保存路径private FileInfo file;// Start is called before the first frame updatevoid Start(){file_SaveUrl = Application.persistentDataPath + "/ChangeSelf.lua.txt";file = new FileInfo(file_SaveUrl);StartCoroutine(DownFile(urlPath));}IEnumerator DownFile(string url){WWW www = new WWW(url);yield return www;if (www.isDone){Debug.Log("下载完成");byte[] bytes = www.bytes;CreatFile(bytes);}}/// <summary>/// 创建文件/// </summary>/// <param name="bytes"></param>void CreatFile(byte[] bytes){//将服务器的文件按字节写入保存路径所在文件Stream stream;stream = file.Create();stream.Write(bytes, 0, bytes.Length);stream.Close();stream.Dispose();}}

这两个脚本挂在同一个空物体上

3.运行

每次修改完c#脚本或者lua脚本后,都要先点击第一个,再点击第三个,然后才能运行。

如果成功执行会输出一下两条日志

然后就能运行unity了,运行后发现cube的scale和rotation都实现了lua脚本中的更改,而不是c#脚本中的更改。

C# 实现 rtc_通过Xlua实现unity热更新的一个小例子相关推荐

  1. unity案例 mysql lua_通过Xlua实现unity热更新的一个小例子

    通过Xlua实现unity热更新的一个小例子 一.介绍 ​ 热更新是指在更新游戏资源或者逻辑的时候不需要开发者将游戏再打包.发布.玩家重新下载安装包,而是可以通过将需要更新的资源打包成AssetBun ...

  2. 【Unity开源项目精选】xLua:Unity热更新首选

    洪流学堂,让你快人几步.你好,我是你的技术探路者郑洪智,你可以叫我大智. 今天给你分享一个Unity开源项目,我们一起来看看吧! xLua C#下Lua编程支持: xLua为Unity. .Net. ...

  3. [Unity热更新]04.卸载方式

    [Unity热更新]04.卸载方式 参考链接: https://www.jianshu.com/p/b37ee8cea04c 1.AssetBundle.Unload(false):释放AssetBu ...

  4. Xlua文件在热更新中调用方法

    Xlua文件在热更新中调用方法 public class news : MonoBehaviour { LuaEnv luaEnv;//定义Lua初始变量 void Awake() { luaEnv ...

  5. Unity热更新机制

    前言 游戏上线后,难免会有一些测试阶段没发现的bug,bug这东西,可大可小. 如果出现重大bug,而又没有热更技术,那么我们为了修复bug就只能强制玩家去商店下载新包,那造成的玩家流失是非常可怕的. ...

  6. Unity热更新方案探索与讨论

    热更新必要性 App Store审核周期长 应用更新频繁 更新版本对留存数据有很大影响 Lua相关 Lua:脚本,解释性语言 LuaJit:扩展高效版本,支持编译成二进制代码. Tolua++:C/C ...

  7. Unity 热更新方案之——ILRuntime

    文章目录 前言 一.ILRuntime是什么? 二.ILRuntime使用 1.跨域委托 2.跨域继承 3.CLR绑定与重定向 前言 做游戏离不开热更新,目前市面上热更新方案用的比较多的是Lua(XL ...

  8. ILRuntime Unity热更新

    在新的项目中,使用到了ILRuntime的热更新方式,不同于XLua等,这种方式的热更新是由纯C#实现的,所以就不需要客户端懂Lua的代码.更详细的介绍可以看官方的文档. 官方的介绍及文档为:http ...

  9. Unity热更新技术整理

    一.热更新学习介绍 1.什么是热更新 举例来说: 游戏上线后,玩家下载第一个版本(70M左右或者更大),在运营的过程中,如果需要更换UI显示,或者修改游戏的逻辑,这个时候,如果不使用热更新,就需要重新 ...

最新文章

  1. markdown to html
  2. esp32 python-ESP32教程:MicroPython支持
  3. 关于函数形参的一些讨论
  4. Shift Dot_JAVA
  5. sql2005导出Excel错误解决方法
  6. 使用HTML5开发离线应用 - cache manifest
  7. [js] 请使用 js 实现一个双向链表
  8. 《一天聊一个设计模式》 单例
  9. 将网卡中断分布到多个cpu上的方法
  10. java 人脸识别 性别识别
  11. 干货 | 云解析DNS之网站监控
  12. Android 视频直播的流程总览
  13. debian9.12的硬盘安装过程一
  14. Vue2 面试题总结1(笔记自用)
  15. 车牌识别算法 matlab,车牌识别算法及其MATLAB实现
  16. 普通美国人的词汇量究竟有多少?
  17. Java-栈的基本操作
  18. Apple M1 上安装tensorflow开发环境
  19. 在您所指定的角色服务器,指定 RD 会话主机服务器的远程桌面授权模式
  20. 海思HI3518e开发板 SDK安装使用

热门文章

  1. PyTorch入门-自然语言分类任务
  2. 100例Python代码带你从入门到进阶!
  3. Django框架——HttpRequest对象
  4. Ubuntu美化方案
  5. Python实现单向链表
  6. Python的gc模块
  7. ExtJs 备忘录(1)—— Form表单(一) [ 控件使用 ]
  8. Ext.data.SimpleStore的使用方法
  9. google地图 反向地址解析(地址查询)
  10. C#2.0中的泛型学