Unity | HybridCLR 热更新(Windows端)

目录

一、准备工作

1.环境相关

2.Unity中配置

二、热更新

1.创建 HotUpdate 热更新模块

2.安装和配置HybridCLR

3.配置PlayerSettings

4.创建热更新相关脚本

5.打包dll

6.测试热更新


一、准备工作

1.环境相关

  • 安装git环境。
  • Win下需要安装visual studio 2019或更高版本。安装时至少要包含 使用Unity的游戏开发 和 使用c++的游戏开发 组件。
  • 本文涉及到的Unity版本是2022.3.14f1c1。unity模块必须安装 Windows端:Windows Build Support(IL2CPP)或Mac端:Mac Build Support(IL2CPP)

2.Unity中配置

  • 在unity中创建场景main,并配置好脚本ConsoleToScreen.cs,它可以打印日志到屏幕上,方便定位错误。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ConsoleToScreen : MonoBehaviour
{
	const int maxLines = 50;
    const int maxLineLength = 120;
    private string _logStr = "";

    private readonly List<string> _lines = new List<string>();

    public int fontSize = 15;

    void OnEnable() { Application.logMessageReceived += Log; }
    void OnDisable() { Application.logMessageReceived -= Log; }


    public void Log(string logString, string stackTrace, LogType type)
    {
        foreach (var line in logString.Split('\n'))
        {
            if (line.Length <= maxLineLength)
            {
                _lines.Add(line);
                continue;
            }
            var lineCount = line.Length / maxLineLength + 1;
            for (int i = 0; i < lineCount; i++)
            {
                if ((i + 1) * maxLineLength <= line.Length)
                {
                    _lines.Add(line.Substring(i * maxLineLength, maxLineLength));
                }
                else
                {
                    _lines.Add(line.Substring(i * maxLineLength, line.Length - i * maxLineLength));
                }
            }
        }
        if (_lines.Count > maxLines)
        {
            _lines.RemoveRange(0, _lines.Count - maxLines);
        }
        _logStr = string.Join("\n", _lines);
    }

    void OnGUI()
    {
        GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,
           new Vector3(Screen.width / 1200.0f, Screen.height / 800.0f, 1.0f));
        GUI.Label(new Rect(10, 10, 800, 370), _logStr, new GUIStyle() { fontSize = Math.Max(10, fontSize) });
    }

}
  • 在Build Settings中添加main场景到打包场景列表。

二、热更新

1.创建 HotUpdate 热更新模块

  • 创建 Assets/HotUpdate 目录(目录名称不做要求,可随便起)
  • 在HotUpdate 目录下右键 Create/Assembly Definition,创建一个名为HotUpdate(名称不做要求)的程序集模块。

        当自己创建一个新的程序集定义文件(.asmdef)时,该文件所在目录以及其子目录下的所有C#脚本都会被默认包含进这个新的程序集中。但是,如果子目录下有另一个.asmdef文件,则那个子目录将会成为另一个独立的程序集。

2.安装和配置HybridCLR

  • 主菜单中点击Windows/Package Manager打开包管理器。点击Add package from git URL...,填入https://gitee.com/focus-creative-games/hybridclr_unity.git 或 https://github.com/focus-creative-games/hybridclr_unity.git。
  • 打开菜单HybridCLR/Installer..., 点击安装按钮进行安装。安装完成后会在最后打印 安装成功日志。

  • 配置HybridCLR:打开菜单ProjectSetting / HybridCLR Settings, 在Hot Update Assemblies配置项中添加HotUpdate程序集。

3.配置PlayerSettings

  • 如果你用的hybridclr包低于v4.0.0版本,需要关闭增量式GC(Use Incremental GC) 选项
  • Scripting Backend 切换为 IL2CPP
  • Api Compatability Level 切换为 .Net 4.x(Unity 2019-2020) 或 .Net Framework(Unity 2021+)

4.创建热更新相关脚本

  • 创建 Assets/HotUpdate/Hello.cs 文件,该文件用于测试是否热更新:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hello : MonoBehaviour
{
    public static void Run()
    {
        Debug.Log("Hello, HybridCLR, V1.0.0");
    }
}
  • 创建Assets/LoadDll.cs脚本,用来加载热更新程序集:
using HybridCLR;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

public class LoadDll : MonoBehaviour
{
    void Start()
    {
        // Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
#if !UNITY_EDITOR
        Assembly hotUpdateAss = Assembly.Load(File.ReadAllBytes($"{Application.streamingAssetsPath}/HotUpdate.dll.bytes"));
#else
        // Editor下无需加载,直接查找获得HotUpdate程序集
        Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate");
#endif
        //通过反射来调用热更新代码
        Type type = hotUpdateAss.GetType("Hello");
        if (type == null)
        {
            Debug.Log("Hello assembly is null");
        }
        else
        {
            type.GetMethod("Run").Invoke(null, null);
        }

    }
}

        HybridCLR是原生运行时实现,因此调用Assembly Assembly.Load(byte[])即可加载热更新程序集。(为了简化演示,我们不通过http服务器下载HotUpdate.dll,而是直接将HotUpdate.dll放到StreamingAssets目录下

5.打包dll

        如果配置正确,Editor运行和打包后运行的效果一样。

  • 运行菜单 HybridCLR/Generate/All 进行必要的生成操作
  • 将{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下为StandaloneMacXxx)目录下的HotUpdate.dll复制到Assets/StreamingAssets/HotUpdate.dll.bytes,注意,要加.bytes后缀

  • 打开Build Settings对话框,点击Build And Run,打包并且运行热更新示例工程。

        如果打包成功,并且屏幕上显示 'Hello, HybridCLR, V1.0.0',表示热更新代码被顺利执行!

6.测试热更新

  • 修改Assets/HotUpdate/Hello.cs的Run函数中Debug.Log("Hello, HybridCLR, V1.0.0");代码,改成Debug.Log("Hello, HybridCLR, V1.1.0");。
  • 运行菜单命令HybridCLR/CompileDll/ActiveBulidTarget重新编译热更新代码
  • 将{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下为StandaloneMacXxx)目录下的HotUpdate.dll复制为刚才的打包输出目录的 XXX_Data/StreamingAssets/HotUpdate.dll.bytes。
  • 重新运行程序,会发现屏幕中显示Hello, HybridCLR, V1.1.0,表示热更新代码生效了!

三、官方文档

 

相关推荐

  1. [Unity--更新之增量更新介绍]

    2023-12-23 14:56:03       42 阅读
  2. 关于 android 更新技术

    2023-12-23 14:56:03       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-23 14:56:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-23 14:56:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-23 14:56:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-23 14:56:03       18 阅读

热门阅读

  1. gitee 怎么添加SSH密钥

    2023-12-23 14:56:03       43 阅读
  2. mybatis xml 文件 sql include 的用法

    2023-12-23 14:56:03       38 阅读
  3. 使用xuggle_5.4 实现视频加水印

    2023-12-23 14:56:03       38 阅读
  4. mockito-study-api

    2023-12-23 14:56:03       36 阅读
  5. Mac[M1]安装mongodb

    2023-12-23 14:56:03       33 阅读
  6. 掌握 Rust 中的建造者模式

    2023-12-23 14:56:03       46 阅读
  7. Harmonyos系统使用http访问网络和应用数据管理

    2023-12-23 14:56:03       31 阅读
  8. 数据处理演进:EtLT崛起,ELT正在告别历史舞台

    2023-12-23 14:56:03       33 阅读
  9. Golang leetcode59 螺旋矩阵

    2023-12-23 14:56:03       34 阅读
  10. 基于汉宁窗FIR滤波器实现语音信号加噪去噪

    2023-12-23 14:56:03       41 阅读
  11. MySQL数据库

    2023-12-23 14:56:03       32 阅读
  12. 理解计算机中的中断与中断处理

    2023-12-23 14:56:03       45 阅读
  13. log4j rename方法

    2023-12-23 14:56:03       35 阅读
  14. python使用selenium无法获取frame完整内容的问题

    2023-12-23 14:56:03       45 阅读