三国卡牌客户端基础资源仓库
yyl
2026-06-16 075274ac0fd7996528f3bd0bfdbf41e8f18f5b22
图集预载
2个文件已修改
2个文件已添加
209 ■■■■■ 已修改文件
Assets/Editor/Tool/UIAtlasPreloadMapBuildBridge.cs 63 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/UIAtlasPreloadMapBuildBridge.cs.meta 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/UpdateSpritePackingSetting.cs 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/YooAsset/YooAssetBuildTool.cs 131 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/UIAtlasPreloadMapBuildBridge.cs
New file
@@ -0,0 +1,63 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 跨程序集桥接调用 UIAtlasPreloadMapBuilder.Build。
/// 避免 Editor 默认程序集与 Main.asmdef 之间的静态引用问题。
/// </summary>
public static class UIAtlasPreloadMapBuildBridge
{
    private const string BuilderTypeName = "UIAtlasPreloadMapBuilder";
    private const string BuildMethodName = "Build";
    public static bool TryBuild(string source)
    {
        try
        {
            var builderType = FindBuilderType();
            if (builderType == null)
            {
                Debug.LogWarning($"[UIAtlasPreloadMapBuildBridge] 未找到类型 {BuilderTypeName},source={source}");
                return false;
            }
            var buildMethod = builderType.GetMethod(BuildMethodName, BindingFlags.Public | BindingFlags.Static);
            if (buildMethod == null)
            {
                Debug.LogWarning($"[UIAtlasPreloadMapBuildBridge] 未找到方法 {BuilderTypeName}.{BuildMethodName},source={source}");
                return false;
            }
            buildMethod.Invoke(null, null);
            AssetDatabase.Refresh();
            Debug.Log($"[UIAtlasPreloadMapBuildBridge] 已执行 {BuilderTypeName}.{BuildMethodName},source={source}");
            return true;
        }
        catch (Exception ex)
        {
            Debug.LogError($"[UIAtlasPreloadMapBuildBridge] 执行失败,source={source}, error={ex}");
            return false;
        }
    }
    private static Type FindBuilderType()
    {
        // 先用全局类型查找
        var type = Type.GetType(BuilderTypeName);
        if (type != null)
            return type;
        // 再遍历所有已加载程序集,兼容 asmdef 拆分
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        for (int i = 0; i < assemblies.Length; i++)
        {
            var t = assemblies[i].GetType(BuilderTypeName, throwOnError: false, ignoreCase: false);
            if (t != null)
                return t;
        }
        return null;
    }
}
Assets/Editor/Tool/UIAtlasPreloadMapBuildBridge.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 582fc0047d864aa47b9b066facbc363b
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Editor/Tool/UpdateSpritePackingSetting.cs
@@ -148,6 +148,8 @@
        AssetDatabase.Refresh();
        Debug.Log("打包BuiltIn图集执行结束");
        UIAtlasPreloadMapBuildBridge.TryBuild("UpdateSpritePackingSetting.SpritePack_BuiltIn");
    }
    [MenuItem("程序/Sprite/打包图集")]
@@ -199,6 +201,8 @@
        Debug.Log("打包图集执行结束");
        UIAtlasPreloadMapBuildBridge.TryBuild("UpdateSpritePackingSetting.SpritePack");
    }
Assets/Editor/YooAsset/YooAssetBuildTool.cs
@@ -14,6 +14,9 @@
public static class YooAssetBuildTool
{
    private static Action s_PendingDllBuildAction;
    private static string s_PendingDllBuildSource;
    private static string[] GetAllPackageNames()
    {
        var setting = AssetBundleCollectorSettingData.Setting;
@@ -432,6 +435,9 @@
    /// </summary>
    public static bool BuildAllPackagesCore(bool incremental, string streamingAssetsOutputPath = null)
    {
        // 打包前生成 UI -> 图集预加载映射,保证运行时预加载策略与最新资源一致。
        TryBuildUIAtlasPreloadMap("YooAssetBuildTool.BuildAllPackagesCore");
        CollectResBeforeUpdate.SyncCollectors();
        // ---- 打包前:HybridCLR DLL(增量模式跳过)----
@@ -509,6 +515,49 @@
        AssetDatabase.Refresh();
        return success;
    }
    private static bool TryBuildUIAtlasPreloadMap(string source)
    {
        try
        {
            const string builderTypeName = "UIAtlasPreloadMapBuilder";
            const string buildMethodName = "Build";
            var builderType = Type.GetType(builderTypeName);
            if (builderType == null)
            {
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                for (int i = 0; i < assemblies.Length; i++)
                {
                    builderType = assemblies[i].GetType(builderTypeName, throwOnError: false, ignoreCase: false);
                    if (builderType != null)
                        break;
                }
            }
            if (builderType == null)
            {
                Debug.LogWarning($"[YooAssetBuildTool] 未找到 {builderTypeName},跳过预加载映射生成。source={source}");
                return false;
            }
            var buildMethod = builderType.GetMethod(buildMethodName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
            if (buildMethod == null)
            {
                Debug.LogWarning($"[YooAssetBuildTool] 未找到 {builderTypeName}.{buildMethodName},跳过预加载映射生成。source={source}");
                return false;
            }
            buildMethod.Invoke(null, null);
            Debug.Log($"[YooAssetBuildTool] 预加载映射已生成。source={source}");
            return true;
        }
        catch (Exception ex)
        {
            Debug.LogWarning($"[YooAssetBuildTool] 预加载映射生成失败(继续打包)。source={source}, error={ex.Message}");
            return false;
        }
    }
    public static bool CopyStreamingAssetsToOutputPath(string outputPath)
@@ -1223,6 +1272,17 @@
            return;
        }
        TryBuildUIAtlasPreloadMap("YooAssetBuildTool.BuildDllAndPlayer");
        if (!EnsureEditorReadyForDllBuild("YooAssetBuildTool.BuildDllAndPlayer", BuildDllAndPlayerCore))
            return;
        BuildDllAndPlayerCore();
    }
    private static void BuildDllAndPlayerCore()
    {
        string outputPath = GetPlayerOutputPath();
        // Step 1: 拷贝 DLL 并打包 Dll Package
        EditorUtility.DisplayProgressBar("YooAsset 打包中...", "正在拷贝 HybridCLR DLL...", 0.1f);
        if (!CopyHybridCLRDlls())
@@ -1387,13 +1447,24 @@
    [MenuItem("YooAsset工具/打包单个Package/Dll (HybridCLR)", false, 316)]
    private static void BuildDll()
    {
        string version = GenerateVersion();
        string versionPreview = DateTime.Now.ToString("yyyy-MM-dd-HHmm");
        if (!EditorUtility.DisplayDialog("确认打包",
            $"打包 Package: Dll (HybridCLR)\n版本: {version}\n平台: {EditorUserBuildSettings.activeBuildTarget}\n\n将先拷贝 HybridCLR 生成的 DLL 到 {HYBRIDCLR_DLLS_ASSETS_PATH}",
            $"打包 Package: Dll (HybridCLR)\n版本(预计): {versionPreview}\n平台: {EditorUserBuildSettings.activeBuildTarget}\n\n将先拷贝 HybridCLR 生成的 DLL 到 {HYBRIDCLR_DLLS_ASSETS_PATH}",
            "开始", "取消"))
        {
            return;
        }
        TryBuildUIAtlasPreloadMap("YooAssetBuildTool.BuildDll");
        if (!EnsureEditorReadyForDllBuild("YooAssetBuildTool.BuildDll", BuildDllCore))
            return;
        BuildDllCore();
    }
    private static void BuildDllCore()
    {
        string version = GenerateVersion();
        EditorUtility.DisplayProgressBar("YooAsset 打包中...", "正在拷贝 HybridCLR DLL...", 0.2f);
        if (!CopyHybridCLRDlls())
@@ -1424,6 +1495,62 @@
        AssetDatabase.Refresh();
    }
    private static bool EnsureEditorReadyForDllBuild(string source, Action pendingAction)
    {
        // 代码生成后可能触发脚本重编译,DLL 打包必须在编译结束后执行。
        if (EditorApplication.isCompiling || EditorApplication.isUpdating)
        {
            s_PendingDllBuildAction = pendingAction;
            s_PendingDllBuildSource = source;
            EditorApplication.update -= TryRunPendingDllBuildWhenEditorReady;
            EditorApplication.update += TryRunPendingDllBuildWhenEditorReady;
            Debug.LogWarning($"[YooAssetBuildTool] 编辑器正在编译或刷新,DLL 打包已加入自动队列。source={source}");
            EditorUtility.DisplayDialog(
                "已加入自动队列",
                "检测到脚本正在编译或资源正在刷新。\n\n编译完成后将自动继续执行 DLL 打包,无需手动再次点击。",
                "确定");
            return false;
        }
        if (BuildPipeline.isBuildingPlayer)
        {
            Debug.LogWarning($"[YooAssetBuildTool] 当前正在构建 Player,跳过 DLL 打包。source={source}");
            return false;
        }
        return true;
    }
    private static void TryRunPendingDllBuildWhenEditorReady()
    {
        if (s_PendingDllBuildAction == null)
        {
            EditorApplication.update -= TryRunPendingDllBuildWhenEditorReady;
            return;
        }
        if (EditorApplication.isCompiling || EditorApplication.isUpdating || BuildPipeline.isBuildingPlayer)
            return;
        var action = s_PendingDllBuildAction;
        var source = s_PendingDllBuildSource;
        s_PendingDllBuildAction = null;
        s_PendingDllBuildSource = null;
        EditorApplication.update -= TryRunPendingDllBuildWhenEditorReady;
        try
        {
            Debug.Log($"[YooAssetBuildTool] 编译已结束,自动继续执行 DLL 打包。source={source}");
            action?.Invoke();
        }
        catch (Exception ex)
        {
            Debug.LogError($"[YooAssetBuildTool] 自动执行 DLL 打包失败。source={source}, error={ex}");
            EditorUtility.DisplayDialog("自动打包失败", "编译结束后自动执行 DLL 打包失败,请查看 Console 日志。", "确定");
        }
    }
    private static void BuildSingleWithDialog(string packageName)
    {
        string version = GenerateVersion();