Unity hot update base HybridCLR: Windows platform use (HybridCLR notes 2)

The project is modified according to the sample project on the official website. The version parameters are as follows:

Unity: 2020.3.30 (Updated to the latest version of wolong on 2023.5.29, still valid after testing)

wolong: v2.3.0

The official website address has changed and updated to: HybridCLR (focus-creative-games.github.io)

Relevant project sample warehouse address: wolong2.0 version test project: This is a test demo for learning wolong, see CSDN (gitee.com) for specific tutorial operations

-------------------------------------------------------------

1. Installation: Reference: The first article Unity Hot Update Basics HybridCLR: Installation and Deployment (HybridCLR Notes 2)

2. Import the example project:

Please visit the official file warehouse: github or gitee, here is the sample project URL of gitee:

hybridclr_trial: Example projects for HybridCLR (gitee.com)

download it

Import the project when done:

Import it to the same project directory as your project, the import content is: (the picture is the downloaded sample project), and the arrow is the import project

(1 prompt is the directory)

Step 1 (2 steps in total)

 and then there is

Step 2 (2 steps in total)

 A stream file reading plug-in is imported here. In order to ensure stability, because the official solution is to read through this stream file plug-in, but the hot update stream file directory is a read-only directory. In order to comply with the specification, the WWW solution is still selected. , he will be modified later, and imported here first

In this way, the sample project is imported, enter the project

Check again whether these options are consistent:

 

 

 1. Briefly explain what is going on here. Simply put, this is the selected hot update program set, and it is him that we update.

What I wrote here is because my script belongs to this assembly (as shown in the figure)

 Of course, you can also directly build the assembly yourself for hot update. If you want to build it yourself and use the above, it is this

 Just drag and drop it directly, if you input it manually, please remove the suffix

Then come here and reinstall:

 

 These two go down again. According to the platform, I am Win64 here, Generate->ALL

Scene selection: main

Generate->ALL: It is the stage where errors are most likely to be reported. There are official documents, and the writing is also very detailed. Generally, we only need to find a place to generate a package first (regardless of whether the package is successful or not, this is mainly for Generate) , you must choose:

Do not use code to do this 

Then: export the .dll package file

At this time, we just click to run, and the sample project is completed

But it needs hot update, so some things need to be adjusted to meet the hot update needs:

1. Create a new folder in the project directory and create a code. The code in this folder will be the script code we want to update (probably like this)

 (Because wolong is hot updated through the .dll file)

Note: The assembly of the new dll program has changed , and this example has been updated to: HotUpdate.dll

2. Find the code in the project project: LoadDll

Next, he needs to be modified: the modified code inside is as follows: (the commented version is provided by ChatGPT)

Please pay attention to the directory, and create a directory under the corresponding file for use (updated)

using HybridCLR;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

 
public class LoadDll : MonoBehaviour
{
    //记录添加的dll文件名称
   static  List<string> aotMetaAssemblyFiles = new List<string>()
    {
        "mscorlib.dll",
        "System.dll",
        "System.Core.dll",
    };
 
   private List<string> aotFiles = new List<string>();
   private List<string> aotFil= new List<string>();
 
   private Dictionary<string, WWW> _dic = new Dictionary<string, WWW>();

   private static string _path;
    void Start()
    {
        //通过www访问加载
        Loadwww();
        StartGame();
    }
 
    void Loadwww()
    {
        foreach (var aotDllName in aotMetaAssemblyFiles)
        {
            aotFiles.Add(aotDllName);
        }
        //添加额外的dll文件
        aotFiles.Add("HotUpdate.dll");
        aotFiles.Add("Main.dll");
        //循环遍历所有dll文件
        foreach (var item in aotFiles)
        {
            if (item.EndsWith(".dll"))
            {
                //添加后缀,方便热更
                string str = item + ".bytes";
                aotFil.Add(str);
            }
        }
        //将prefabs问价加入
        aotFil.Add("prefabs");
        
        //提取dll
        foreach (var item in aotFil)
        {
            string dllBytes = Application.dataPath;
            Directory.SetCurrentDirectory(Directory.GetParent(dllBytes).FullName);
            dllBytes=Directory.GetCurrentDirectory();
            _path = dllBytes+("/HotUpdate/HotDllFix/");//保存加载的目录
            dllBytes=dllBytes+("/HotUpdate/HotDllFix/" + item);
            
            //进行加载
            WWW www = new WWW("file://"+dllBytes);
            if (www.error!=null)
            {
                
            }
           //放入等待加载
            _dic.Add(item,www);
        }
        
    }
    
 
 
    void StartGame()
    {
        LoadMetadataForAOTAssemblies();
 
#if !UNITY_EDITOR
// //Assembly-CSharp.dll.bytes
        System.Reflection.Assembly.Load(_dic["HotUpdate.dll.bytes"].bytes);
#endif

        AssetBundle prefabAb = AssetBundle.LoadFromMemory(_dic["prefabs"].bytes);
        GameObject testPrefab = Instantiate(prefabAb.LoadAsset<GameObject>("Cube.prefab"));
    }
 
 
    
    
    
 
    /// <summary>
    /// 为aot assembly加载原始metadata, 这个代码放aot或者热更新都行。
    /// 一旦加载后,如果AOT泛型函数对应native实现不存在,则自动替换为解释模式执行
    /// </summary>
    private static void LoadMetadataForAOTAssemblies()
    {
        
        /// 注意,补充元数据是给AOT dll补充元数据,而不是给热更新dll补充元数据。
        /// 热更新dll不缺元数据,不需要补充,如果调用LoadMetadataForAOTAssembly会返回错误
 
        HomologousImageMode mode = HomologousImageMode.SuperSet;
        foreach (var aotDllName in aotMetaAssemblyFiles)
        {
            //根据路径进行加载
            byte[] dllBytes =
                File.ReadAllBytes(_path + aotDllName + ".bytes");
            // 加载assembly对应的dll,会自动为它hook。一旦aot泛型函数的native函数不存在,用解释器版本代码
            LoadImageErrorCode err = RuntimeApi.LoadMetadataForAOTAssembly(dllBytes, mode);
            Debug.Log($"LoadMetadataForAOTAssembly:{aotDllName}. mode:{mode} ret:{err}");
        }
    }
}

Tip: After the update script file is released, the code here requires you to manually move the file from the stream directory to the specified directory for loading, so that it can run normally. Please analyze the code yourself for details.

After modifying the code, please perform these three steps again:

End of hot update wolong module

 There are no VIPs or fees for this article, give likes, collections and attention~ (嘤嘤~)

Guess you like

Origin blog.csdn.net/qq_46043095/article/details/130438113