private static void Initialize() { var context = SynchronizationContext.Current; if (context == null) { // Create custom SynchronizationContext for the main thread. context = new Helpers.MainThreadSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(context); } // Set main thread context as default for all continuations. This saves allocations in many cases. AsyncResult.DefaultSynchronizationContext = context; // Create helper GameObject _go = new GameObject("UnityFx.Async") { hideFlags = HideFlags.HideAndDontSave }; GameObject.DontDestroyOnLoad(_go); // Initialize library components. AsyncUtility.Initialize(_go, context); AsyncWww.Initialize(_go, context); }
private static void OnTaskCompleted <T>(TaskCompletionSource <T> tcs, UnityWebRequest request) where T : class { try { #if UNITY_5 if (request.isError) #else if (request.isHttpError || request.isNetworkError) #endif { tcs.TrySetException(new Helpers.WebRequestException(request.error, request.responseCode)); } else if (request.downloadHandler != null) { var result = AsyncWww.GetResult <T>(request); tcs.TrySetResult(result); } else { tcs.TrySetResult(null); } } catch (Exception e) { tcs.TrySetException(e); } }
private static void OnTaskCompleted <T>(TaskCompletionSource <T> tcs, WWW www) where T : class { try { if (string.IsNullOrEmpty(www.error)) { var result = AsyncWww.GetResult <T>(www); tcs.TrySetResult(result); } else { tcs.TrySetException(new Helpers.WebRequestException(www.error)); } } catch (Exception e) { tcs.TrySetException(e); } }