0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unity Dotween機能説明~これであなたもDotweenマスター~

Posted at

Dotweenとは

Unityでものをうごかす、はねる、大きくなるなど動きのあるアニメーションを付けられるAsset。
これを拡張して、変数を変化させる。アニメーションの組み合わせ、あるタイミングで関数を呼び出すこともできる。

使用準備

アセットストアからインストール。
セットアップでテキストのアニメーションがしたい場合TextMeshProにチェックを入れる。
image.png
インストール後は
image.png
ここからセットアップをよびだせる。

必要であればAssemblyにも追加する。
image.png

基本機能を使って見る

transform.DOMove(new Vector3(5f, 0f, 0f), 3f);

基本の形
Vec3で目標地点、3秒後に到達する。
Do~はいろいろある。試してみて。

停止させる

gameObject.transform.DOPause();

再開

gameObject.transform.DOPlay();

アニメーションをすぐに終わらす。

transform.DOComplete();

繰り返す

transform.DOMove(new Vector3(5f, 0f, 0f), 3f).
   SetLoops(-1,LoopType.Restart);

無限ループで初めから繰り返す。

発展機能

.で機能を組み合わせることができる

transform.DOMoveX(3f, 0).
    SetRelative(true);

移動する機能に[相対的に動く]を組み合わせいる。
これをメソッドチェーンという

transform.DOMoveX(3f, 0).
   SetRelative(true).
   SetUpdate(true);

好きなだけ組み合わせことができる。

Easeを使う

Easeは初めゆっくり、最後は速くなどアニメーションのうごきをいい感じにつけるもの。

transform.DOMove(new Vector3(5f, 0f, 0f), 5f).SetEase(Ease.Linear);

Easeにもいろいろある。

自分でつくるとかなり細かく設定できる

     [SerializeField] private AnimationCurve testAnimationCurve;
transform.DOMove(new Vector3(5, 0, 0), 5f).
   SetEase(testAnimationCurve).
   SetLoops(-1,LoopType.Restart);

Unityで動かしながらいい感じなるまでためそう。
AnimationCurveはレベルデザインにも使えるのが覚えておくと便利。

変数を操作

       [SerializeField] private float doTweenVal = 0;
//5秒かけてaを100まで変化させる
DOTween.To(() => doTweenVal, //開始時の値
    (x) => doTweenVal = x, //更新する変数
    100, //最大値
    5); //秒数

DoTween.Toを使用し変数を操作する。
0から100まで5秒かけて変数が動いていく。

文字列の操作

        [SerializeField] private TextMeshProUGUI testText;
string str = "文字列のテスト";
testText.DOText(str,2f);

2秒かけて文字が入力されていくようなアニメーション

2秒後に関数を実行する。

DOVirtual.DelayedCall(2f, () =>
{
    Debug.Log("DelayCallTest :2秒後によばれたよ");
});

処理の前、終わりに関数を実行する。

//OnStart()もあるよ
transform.DOLocalMove(new Vector3(0, 0, 10f), 0.5f)
        .OnComplete(() => {
            //完了後オブジェクトが消える
            gameObject.SetActive(false);
        });

複数の処理をまとめテンプレ化

Sequenceを使う

Sequence tseq = DOTween.Sequence();
tseq.Append(transform.DOJump(new Vector3(5f, 0, 0), 5f,3,3f));
tseq.Append(transform.DOMove(new Vector3(0f, 0, 0), 3f));

ジャンプしてから移動するアニメーションがまとめられる。
関数化すると使いやすくなる

private Sequence JumpAndMoveSeq()
{
    Sequence tseq = DOTween.Sequence();
    tseq.Append(transform.DOJump(new Vector3(5f, 0, 0), 5f,3,3f));
    tseq.Append(transform.DOMove(new Vector3(0f, 0, 0), 3f));

    return tseq;
}

使用箇所

JumpAndMoveSeq().Play();

Sequenceに対してPlayする。

ちょっと専門的な内容

DotweenはUpdateまでどう動くのかをためている。それまでうごかない。Updateで一度に動く仕組みとなっている。
実行タイミングがUpdateなので注意

Dotweenのメモリ確保のタイミングは初めにDotweenが実行された時と、
DoTween.Init()が呼ばれた時となっている。

ロード画面やScene切り替え時にInitしておくとかくつきをふせげる。

また、メモリの量は
DOTween.SetTweensCapacity(400,100);
(Tween数、Sequence数)
で設定できる。
途中でメモリ確保してかくつかない範囲で設定しよう

組み合わせのアイディア無限

これであなたもDoTweenマスター!!

参考ページ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?