You've already forked Commercialization.topon
feat(topon): 优化主线程调用及增加控制器初始化配置支持
- ToponUnityThread新增初始化方法并封装主线程安全调用接口 - ADListenerAggregator中所有回调改用ToponUnityThread切换至主线程调用 - ToponAdController新增ToponControllerOptions配置支持,支持更多初始化参数 - 实现SDK初始化前后配置的应用,支持设置渠道、区域、经纬度等选项 - 支持初始化时自动检测区域并通过回调通知 - 新增ToponControllerOptions类,实现多种初始化参数来源解析和合并 - ShowAndroidTest方法根据DebuggerKey选择不同调试器UI展示模式 - 记录并公开最近一次区域检测结果及错误信息,方便外部查询和调试
This commit is contained in:
55
Topon_Adapter/Runtime/Scripts/ToponUnityThread.cs
Normal file
55
Topon_Adapter/Runtime/Scripts/ToponUnityThread.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
public static class ToponUnityThread
|
||||
{
|
||||
private static readonly object SyncRoot = new object();
|
||||
|
||||
private static SynchronizationContext _unityContext;
|
||||
private static int _mainThreadId = -1;
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
if (SynchronizationContext.Current == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (SyncRoot)
|
||||
{
|
||||
_unityContext = SynchronizationContext.Current;
|
||||
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Post(Action action)
|
||||
{
|
||||
if (action == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var context = _unityContext;
|
||||
var currentThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||
if (context == null || currentThreadId == _mainThreadId)
|
||||
{
|
||||
SafeInvoke(action);
|
||||
return;
|
||||
}
|
||||
|
||||
context.Post(_ => SafeInvoke(action), null);
|
||||
}
|
||||
|
||||
private static void SafeInvoke(Action action)
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Debug.LogException(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user