1.新建一個空白項(xiàng)目
2.為編輯器添加IL2CPP
3.為vs2019+添加c++開發(fā)環(huán)境
4.unity更改設(shè)置
5.獲取hybirdcrl插件,打開packagemanager,輸入url:
https://gitee.com/focus-creative-games/hybridclr_unity.git
6.創(chuàng)建熱更新文件夾,創(chuàng)建dll文件,在插件設(shè)置中放入
7.加載
8.代碼實(shí)現(xiàn):(注意代碼邏輯)
(1)在asstes建立StreamingAsstes文件夾,后面用于存放熱更的加載文件
最終目錄如圖
其中HybridCLRGenerate是installer自動生成的
(2)進(jìn)入代碼,在AOT目錄中建立Con**.cs和LoadDll.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) });
? ? }
}
*****************************************
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環(huán)境下,HotUpdate.dll.bytes已經(jīng)被自動加載,不需要加載,重復(fù)加載反而會出問題。
#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("Begin");
? ? ? ? type.GetMethod("Startit").Invoke(null, null);
? ? }
}
(3)分別在場景中掛載兩個腳本
(4)在HotUpdate目錄下寫腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Begin
{
? ? public static void Startit()
? ? {
? ? ? ? Debug.Log("Hello, HybridCLR");
? ? }
}
?
(泛型與單例模式,泛型不需要定義量的具體類型,先構(gòu)建方法,在使用的時候聲明其類型,單例模式下一個類只能存在唯一的一個實(shí)體,方便其他代碼訪問,地位相當(dāng)于中央銀行)
(5)生成打包一下 點(diǎn)擊All
10.unity打包游戲,完成后進(jìn)行文件替換,將編輯文件下的D:\unity\hytest\HybridCLRData\HotUpdateDlls\StandaloneWindows64/HotUpdate.dll,重命名為HotUpdate.dll.bytes,放到游戲生成后的目錄C:\Users\creator\Desktop\hy\hytest_Data\StreamingAssets下
11.當(dāng)對 Begin腳本內(nèi)容進(jìn)行修改時,點(diǎn)擊ActiveBuildTarget然后再次重復(fù)操作10即可
文章來源:http://www.zghlxwxcb.cn/news/detail-840731.html
12.手動熱更新begin的delog后,運(yùn)行游戲后打印信息會更新發(fā)生變化。文章來源地址http://www.zghlxwxcb.cn/news/detail-840731.html
到了這里,關(guān)于unity hybird熱更新實(shí)戰(zhàn)學(xué)習(xí) 小白(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!