Unity + HybridCLR 从零开始

官方文档开始学习,快速上手 | HybridCLR (code-philosophy.com)是官方文档链接

1.建议使用2019.4.40、2020.3.26+、 2021.3.0+、2022.3.0+ 中任一版本至于其他2019-2022LTS版本可能出现打包失败情况

2.

  • Windows
    • Win下需要安装visual studio 2019或更高版本。安装时至少要包含 使用Unity的游戏开发 和 使用c++的游戏开发 组件。
    • 安装git  Git - 下载包 (git-scm.com) 官网下载git

vs添加组件选择这两个

3.安装 com.code-philosophy.hybridclr 包

主菜单中点击Windows/Package Manager打开包管理器。如下图所示点击Add package from git URL...,填入https://gitee.com/focus-creative-games/hybridclr_unity.githttps://github.com/focus-creative-games/hybridclr_unity.git

初始化 com.code-philosophy.hybridclr

打开菜单HybridCLR/Installer..., 点击安装按钮进行安装。 耐心等待30s左右,安装完成后会在最后打印 安装成功日志。

4.创建脚本 ConsoleToScreen

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) });
    }
}

5.

场景中创建一个空GameObject,将ConsoleToScreen挂到上面

Build Settings中添加main场景到打包场景列表

创建 Assets/HotUpdate 目录在目录下

右键 Create/Assembly Definition,创建一个名为HotUpdate的程序集模块

6.

7.

创建LoadDll.cs脚本 挂载到场景中

运行 屏幕上出现Hello, HybridCLR

8.

运行菜单 HybridCLR/Generate/All 进行必要的生成操作。

{proj}/HybridCLRData/HotUpdateDlls/StandaloneWindows64(MacOS下为StandaloneMacXxx)目录下的HotUpdate.dll复制到Assets/StreamingAssets/HotUpdate.dll.bytes注意,要加.bytes后缀!!!

复制到StreamingAssets里加.bytes

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

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

测试热更新

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

至此完成热更新体验!!!

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值