三国卡牌客户端基础资源仓库
yyl
5 天以前 47921ce315e894bbbed712cbf04ee358be345113
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
    }
}