DEV Community

Cover image for 【Unity】How to convert DoTween to UniTask and wait to complete all sequences.
KENTO⚽️XR Engineer😎
KENTO⚽️XR Engineer😎

Posted on • Updated on

【Unity】How to convert DoTween to UniTask and wait to complete all sequences.

Intro

I thought that I wanted to treat as UniTask and wait for all of sequences when I use DoTween.

I write the way down on this article so I won't forget.


Environments

name version
Unity 2020.3.4f1
DOTween (HOTween v2) 1.2.632
UniTask 2.2.5

Setting up

First we’ll import DoTween and UniTask to our unity project.

Next set UNITASK_DOTWEEN_SUPPORT on Scripting Define Symbols and select Apply.

Alt Text

But under my environment it wasn't applied to change settings for unknown reasons.

So I opened whatever assembly definition assets in the project, modified them once, and revert them.

In my case, it worked that way.


Sample Code

using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Sample of DoTween which treats as UniTask and wait to finish the task. 
/// </summary>
public class WaitDoTweenTaskSample : MonoBehaviour
{
    /// <summary>
    /// For task text
    /// </summary>
    [SerializeField] private Text[] _taskTexts;

    /// <summary>
    /// For completion text
    /// </summary>
    [SerializeField] private Text _completeTaskText;

    async void Start()
    {
        //Wait to complete task gave to argument.
        await UniTask.WhenAll(FadeTasks(this.GetCancellationTokenOnDestroy()));
        //When completed, fade in the text.
        _completeTaskText.DOFade(1.0f, 2.0f).Play();
    }

    /// <summary>
    /// Return task list that converted the fade sequences.
    /// </summary>
    /// <param name="ct">CancelToken </param>
    /// <returns>Task list</returns>
    private List<UniTask> FadeTasks(CancellationToken ct)
    {
        var taskList = new List<UniTask>();

        foreach (var taskText in _taskTexts)
        {
            var randomValue = Random.Range(1.0f, 5.0f); 
            taskList.Add(  taskText.DOFade(1.0f, randomValue).Play().ToUniTask(cancellationToken: ct));
        }

        return taskList;
    }
}
Enter fullscreen mode Exit fullscreen mode

Demo

After all of fade sequences completed, a completion text starts to fade in.

Alt Text

Other Resources

UniTaskでDoTweenを使う In 2020/11

Latest comments (0)