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;
|
}
|
}
|