feat(topon): 优化主线程调用及增加控制器初始化配置支持

- ToponUnityThread新增初始化方法并封装主线程安全调用接口
- ADListenerAggregator中所有回调改用ToponUnityThread切换至主线程调用
- ToponAdController新增ToponControllerOptions配置支持,支持更多初始化参数
- 实现SDK初始化前后配置的应用,支持设置渠道、区域、经纬度等选项
- 支持初始化时自动检测区域并通过回调通知
- 新增ToponControllerOptions类,实现多种初始化参数来源解析和合并
- ShowAndroidTest方法根据DebuggerKey选择不同调试器UI展示模式
- 记录并公开最近一次区域检测结果及错误信息,方便外部查询和调试
This commit is contained in:
2026-03-18 16:48:32 +08:00
parent 8d4617f851
commit da3d20be8c
6 changed files with 507 additions and 23 deletions

View File

@@ -5,19 +5,28 @@ using UnityEngine;
public class ToponAdController : IAdController
{
public static string LastDetectedArea { get; private set; }
public static string LastAreaError { get; private set; }
private Action<bool> _maskAction;
private Action<string, string> _logEventAction;
private ADConfig _adConfig;
private ToponControllerOptions _controllerOptions;
public void Init(ADConfig adConfig, object[] args)
{
ToponUnityThread.Initialize();
_adConfig = adConfig;
// ATSdkUtil.
ATSDKAPI.setChannel(args[0].ToString());
var isDebug = args.Length > 1 && (bool)args[1];
_controllerOptions = ToponControllerOptions.Resolve(adConfig, args);
ApplyPreInitOptions(_controllerOptions);
var isDebug = _controllerOptions.Debug ?? false;
ATSDKAPI.setLogDebug(isDebug);
ATSDKAPI.initSDK(adConfig.Id , adConfig.Key);
ApplyPostInitOptions(_controllerOptions);
if (isDebug)
{
ShowAndroidTest ();
@@ -58,7 +67,16 @@ public class ToponAdController : IAdController
private void ShowAndroidTest ()
{
ATSDKAPI.showDebuggerUI ();
var debuggerKey = _controllerOptions?.DebuggerKey;
if (string.IsNullOrWhiteSpace(debuggerKey))
{
ATSDKAPI.showDebuggerUI();
}
else
{
ATSDKAPI.showDebuggerUI(debuggerKey);
}
// com.anythink.debug.api.ATDebuggerUITest.showDebuggerUI(this);
// #if UNITY_EDITOR
// return;
@@ -77,4 +95,93 @@ public class ToponAdController : IAdController
// }
// #endif
}
}
private void ApplyPreInitOptions(ToponControllerOptions options)
{
if (options == null)
{
return;
}
if (!string.IsNullOrWhiteSpace(options.Channel))
{
ATSDKAPI.setChannel(options.Channel);
}
if (!string.IsNullOrWhiteSpace(options.SubChannel))
{
ATSDKAPI.setSubChannel(options.SubChannel);
}
if (options.SDKArea.HasValue)
{
ATSDKAPI.setSDKArea(options.SDKArea.Value);
}
if (options.Longitude.HasValue && options.Latitude.HasValue)
{
ATSDKAPI.setLocation(options.Longitude.Value, options.Latitude.Value);
}
if (options.ExcludeBundleIds != null && options.ExcludeBundleIds.Length > 0)
{
ATSDKAPI.setExcludeBundleIdArray(options.ExcludeBundleIds);
}
}
private void ApplyPostInitOptions(ToponControllerOptions options)
{
if (options == null)
{
return;
}
var rewardedPlacementId = _adConfig?.BaseAwardAdKeyValue?.value;
if (!string.IsNullOrWhiteSpace(rewardedPlacementId) &&
options.RewardedExcludeAdSourceIds != null &&
options.RewardedExcludeAdSourceIds.Length > 0)
{
ATSDKAPI.setExcludeAdSourceIdArrayForPlacementID(rewardedPlacementId, options.RewardedExcludeAdSourceIds);
}
if (options.QueryAreaOnInit)
{
ATSDKAPI.getArea(new ToponAreaListener(this, options));
}
}
private sealed class ToponAreaListener : ATGetAreaListener
{
private readonly ToponAdController _controller;
private readonly ToponControllerOptions _options;
public ToponAreaListener(ToponAdController controller, ToponControllerOptions options)
{
_controller = controller;
_options = options;
}
public void onArea(string area)
{
ToponUnityThread.Post(() =>
{
LastDetectedArea = area;
LastAreaError = null;
_options?.OnAreaReceived?.Invoke(area);
_controller?._logEventAction?.Invoke("topon_area", area);
Debug.Log($"[Topon] Area detected: {area}");
});
}
public void onError(string message)
{
ToponUnityThread.Post(() =>
{
LastAreaError = message;
_options?.OnAreaError?.Invoke(message);
_controller?._logEventAction?.Invoke("topon_area_error", message);
Debug.LogWarning($"[Topon] Area detect failed: {message}");
});
}
}
}