在这里我们采用HybridCLR+YooAsset的方案进行热更新
(不建议Addressable方案资源管理,个人感觉坑有亿点多)
这里使用VS2022,Unity编辑器版本为2022.3.20f1cf1
https://gitee.com/focus-creative-games/hybridclr_unity.git
ConsoleToScreen.cs
(用途是输出日志)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();
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 = 10 });
}
}
HotUpdate
文件夹HotUpdate
玩家
,进行如下配置编辑/项目设置/包管理器
添加如下信息Name: yooasset
URL: https://package.openupm.com
Scope(s): com.tuyoogame.yooasset
2. 在主菜单中点击 窗口/包管理器 切换到 我的注册表
安装 YooAsset