Unity热更新文件修改后缀并拷贝到指定路径的工具类

发布于:2025-02-10 ⋅ 阅读:(22) ⋅ 点赞:(0)

最近在学习Hybrid热更新。每次编译完,需要修改后缀名和拷贝到特定路径覆盖旧文件,就叫AI写了个工具类。现在记录一下,毕竟ai写完还需要修改。

代码如下,放到Assets/Editor/路径下即可。
可根据需求自行改变路径和文件名。

using ETModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;

/// <summary>
/// 拷贝热更程序集的工具类
/// </summary>
public class CopyHotfixDll
{
    private const string ScriptAssembliesDir = "HybridCLRData/HotUpdateDlls/iOS/";//编译后的热更程序集文件路径
    private const string sourDir = "HybridCLRData/AssembliesPostIl2CppStrip/iOS/";//编译后的AOT元数据的路径
    private const string CodeDir = "Assets/Res/Code/Hybrid/";//需要拷贝到项目下的某个路径

    private static List<string> dllName = new List<string>() { "Hotfix" };
    private static List<string> AOTName = new List<string>() { "mscorlib" , "System.Core", "Unity.Model", "Unity.ThirdParty", "ILRuntime", "Unity.Addressables", "Unity.ResourceManager" };

    //拷贝编译后的程序集文件到指定路径并修改后缀
    [MenuItem("Tools/CopyHotfixDll")]
    public static void HotFixDll2Bytes()
    {
        //copy 程序集
        foreach (var item in dllName)
        {
            File.Copy(Path.Combine(ScriptAssembliesDir, $"{item}.dll"), Path.Combine(CodeDir, $"{item}.dll.bytes"), true);
        }

        //copy AOT元数据
        foreach (var item in dllName)
        {
            File.Copy(Path.Combine(ScriptAssembliesDir, $"{item}.dll"), Path.Combine(CodeDir, $"{item}.dll.bytes"), true);
        }

        Log.Info($"Copy Hotfix dll & AOT to Res/Code complete");
        try
        {
            AssetDatabase.Refresh();
        }
        catch (Exception e)
        {
            //TODO
            Log.Info($"Copy Hotfix.dll fail");
        }
    }
    
}


网站公告

今日签到

点亮在社区的每一天
去签到