/// <summary> /// Plays an animator state. /// </summary> /// <param name="anim">Target animator instance.</param> /// <param name="stateName">The state name.</param> /// <returns>An asynchronous operation that can be used to track the animation progress.</returns> public static IAsyncOperation PlayAsync(this Animator anim, string stateName) { var result = new Helpers.PlayAnimatorResult(anim, stateName, -1, AsyncUtility.GetUpdateSource()); result.Start(); return(result); }
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); }
/// <summary> /// Creates an <see cref="Task{TResult}"/> wrapper for the Unity <see cref="AssetBundleRequest"/>. /// </summary> /// <param name="op">The source operation.</param> public static Task <T> ToTask <T>(this AssetBundleRequest op) where T : UnityEngine.Object { var result = new TaskCompletionSource <T>(op); AsyncUtility.AddCompletionCallback(op, () => result.TrySetResult(op.asset as T)); return(result.Task); }
/// <summary> /// Waits until current animation completes. /// </summary> /// <param name="anim">Target animator instance.</param> /// <returns>An asynchronous operation that can be used to track the animation progress.</returns> public static IAsyncOperation WaitAsync(this Animator anim) { var result = new Helpers.WaitAnimatorResult(anim, 0, AsyncUtility.GetUpdateSource()); result.Start(); return(result); }
/// <summary> /// Creates an <see cref="Task{TResult}"/> wrapper for the specified <see cref="WWW"/>. /// </summary> /// <param name="request">The source web request.</param> public static Task <T> ToTask <T>(this WWW request) where T : class { var result = new TaskCompletionSource <T>(request); AsyncUtility.AddCompletionCallback(request, () => OnTaskCompleted(result, request)); return(result.Task); }
/// <summary> /// Plays an animation without any blending. /// </summary> /// <param name="anim">Target animation instance.</param> /// <param name="animation">Name of the animation to play.</param> /// <param name="mode">The mode which lets you choose how this animation will affect others already playing.</param> /// <returns>An asynchronous operation that can be used to track the animation progress.</returns> public static IAsyncOperation PlayAsync(this Animation anim, string animation, PlayMode mode) { var result = new Helpers.PlayAnimationResult(anim, animation, mode, AsyncUtility.GetUpdateSource()); result.Start(); return(result); }
/// <summary> /// Creates an <see cref="Task"/> wrapper for the specified <see cref="WWW"/>. /// </summary> /// <param name="request">The source web request.</param> public static Task ToTask(this WWW request) { var result = new TaskCompletionSource <object>(request); AsyncUtility.AddCompletionCallback(request, () => OnTaskCompleted(result, request)); return(result.Task); }
/// <inheritdoc/> public void OnCompleted(Action continuation) { #if UNITY_2017_2_OR_NEWER || UNITY_2018 // Starting with Unity 2017.2 there is AsyncOperation.completed event _op.completed += o => continuation(); #else AsyncUtility.AddCompletionCallback(_op, continuation); #endif }
/// <inheritdoc/> protected override void OnStarted() { if (_www.isDone) { SetCompleted(); } else { AsyncUtility.AddCompletionCallback(_www, SetCompleted); } }
/// <summary> /// Creates an <see cref="Task"/> wrapper for the Unity <see cref="AsyncOperation"/>. /// </summary> /// <param name="op">The source operation.</param> public static Task ToTask(this AsyncOperation op) { if (op.isDone) { return(Task.CompletedTask); } else { var result = new TaskCompletionSource <object>(op); AsyncUtility.AddCompletionCallback(op, () => result.TrySetResult(null)); return(result.Task); } }
/// <inheritdoc/> protected override void OnStarted() { if (_op.isDone) { OnSetCompleted(_op); } else { #if UNITY_2017_2_OR_NEWER || UNITY_2018 // Starting with Unity 2017.2 there is AsyncOperation.completed event _op.completed += OnSetCompleted; #else AsyncUtility.AddCompletionCallback(_op, () => OnSetCompleted(_op)); #endif } }
/// <inheritdoc/> protected override void OnStarted() { if (_request.isDone) { SetCompleted(); } else if (_request.isModifiable) { #if UNITY_2017_2_OR_NEWER || UNITY_2018 _op = _request.SendWebRequest(); #else _op = _request.Send(); #endif AsyncUtility.AddCompletionCallback(_op, SetCompleted); } }
/// <summary> /// Plays an animation without any blending. /// </summary> /// <param name="anim">Target animation instance.</param> /// <param name="mode">The mode which lets you choose how this animation will affect others already playing.</param> /// <returns>An asynchronous operation that can be used to track the animation progress.</returns> public static IAsyncOperation PlayAsync(this Animation anim, PlayMode mode) { if (anim.clip) { var result = new Helpers.PlayAnimationResult(anim, anim.clip.name, mode, AsyncUtility.GetUpdateSource()); result.Start(); return(result); } return(AsyncResult.CanceledOperation); }
/// <inheritdoc/> public void OnCompleted(Action continuation) { AsyncUtility.AddCompletionCallback(_op, continuation); }