UnityThreadUtils
Internal static utility that captures Unity's main thread context by invoking the non-public Unity.Services.Core.UnityThreadUtils.CaptureUnityThreadInfo() method via reflection. Required before calling UnityServices.InitializeAsync() to ensure async continuations execute on the correct thread.
Definition
Namespace: Paragon.Core.Network
Assembly: Paragon.dll
internal static class UnityThreadUtilsRemarks
Unity Gaming Services' internal async machinery relies on captured SynchronizationContext information to marshal continuations back to the Unity main thread. The method CaptureUnityThreadInfo() on the internal Unity.Services.Core.UnityThreadUtils class performs this capture, but it is not publicly accessible.
This utility uses reflection to locate and invoke that method:
Static constructor — Resolves the
Unity.Services.Core.UnityThreadUtilstype viaType.GetType()and caches theCaptureUnityThreadInfoMethodInfousingBindingFlags.Static | BindingFlags.NonPublic.CaptureUnityThreadInfo() — Invokes the cached method. Must be called on the Unity main thread, before
UnityServices.InitializeAsync().
This is called by UnityServiceAdapter.Initialize() as the very first step of SDK initialization.
Quick Lookup
Capture Unity thread context
UnityThreadUtils.CaptureUnityThreadInfo()
When to call
Before UnityServices.InitializeAsync(), on the main thread
Who calls this
Fields
captureUnityThreadInfoMethod
Cached MethodInfo for the non-public static method Unity.Services.Core.UnityThreadUtils.CaptureUnityThreadInfo.
Resolved in the static constructor via:
Methods
CaptureUnityThreadInfo
Invokes the cached reflection method to capture Unity's main thread synchronization context.
Behavior: Calls captureUnityThreadInfoMethod.Invoke(null, null) — a parameterless static method invocation.
Common Pitfalls
Must be called on the main thread
CaptureUnityThreadInfo() captures the current thread's SynchronizationContext. If called from a background thread, async continuations in UGS will not marshal back to the Unity main thread, causing threading errors.
Reflection dependency on internal API
This class depends on the internal Unity.Services.Core.UnityThreadUtils type. If Unity changes or removes this type in a future UGS package version, the static constructor will throw a NullReferenceException. Monitor UGS package updates for breaking changes.
Must be called before InitializeAsync
If UnityServices.InitializeAsync() is called without first capturing thread info, async operations may deadlock or fail silently. UnityServiceAdapter enforces this order.
See Also
UnityServiceAdapter — the only consumer of this utility
Unity Adapters — subsystem overview
Last updated