【Unity】DOTweenの使い方 基本編
DOTweenの使い方
前回記事の続きです。
前回の記事にて、DOTweenを使うのが最適なのではと結論をつけました。今回は、簡単な使い方を調査してみます。
自分は、Proを使用していますが、今回は、無償版でも可能な範囲となっていますのでよければ試してみてください。
はじめに
今回は、3Dキューブを使った動きを確認します。2D/GUIの場合も基本的には一緒です。(uGUIの場合、RectをTweenします)
Move
まずは、一番利用頻度が高いと思われる移動系です。
  DOMove(Vector3 to, float duration, bool snapping)
 
現在の位置(座標)から、Vector3(5.0f, 0.5f, 0)に2秒かけて移動する例です。
this.transform.DOMove(endValue: new Vector3(5.0f, 0.5f, 0), duration: 2.0f);
Local移動の場合は下記を利用します。 
 DOLocalMove(Vector3 to, float duration, bool snapping)
 
個々の座標移動は下記を利用します。
 DOMoveX/DOMoveY/DOMoveZ(float to, float duration, bool snapping)
 DOLocalMoveX/DOLocalMoveY/DOLocalMoveZ(float to, float duration, bool snapping)
Jump
ジャンプしながらの移動も用意されています。
 DOJump(Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping)
 
現在の位置から、Vector3(5.0f, 0.5f, 0)に向かって、高さ5.0で2回ジャンプを2秒かけてJumpする例です。
 
this.transform.DOJump(endValue: new Vector3(5.0f, 0.5f, 0),jumpPower: 5.0f,numJumps: 2,duration: 2.0f);
Localもありますね。
DOLocalJump(Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping)
Rotate
続いて回転です。 
 DORotate(Vector3 to, float duration, RotateMode mode)
 
x軸で360回転します。RotateMode modeは、初期値では近い方から回転します。ので、RotateMode.FastBeyond360をつけると順回転させます。
 
this.transform.DORotate(endValue: new Vector3(360f, 0, 0), duration: 1.0f ,mode: RotateMode.FastBeyond360);
 クオータニオンを使う場合は、下記みたいです。クオータニオンは苦手です。直観的じゃないんだよね。
 DORotateQuaternion(Quaternion to, float duration)
Scale
スケールの変更です。
 DOScale(float/Vector3 to, float duration)
2倍に拡大する例です。
 
this.transform.DOScale(endValue: new Vector3(2, 2, 2),duration: 1.0f);
 
Punch
パンチです。一定の間隔で揺れる動きをします。
 DOPunchPosition(Vector3 punch, float duration, int vibrato, float elasticity, bool snapping)
 DOPunchRotation(Vector3 punch, float duration, int vibrato, float elasticity)
 DOPunchScale(Vector3 punch, float duration, int vibrato, float elasticity)
例
IEnumerator PunchSample()
    {
        this.transform.DOPunchPosition(punch: new Vector3(1f, 0f, 1f),duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOPunchRotation(punch: new Vector3(20f, 0f, 20f),duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOPunchScale(punch: new Vector3(0.1f, 0.1f, 0.1f),duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
    }
Shake
シェイクです。ランダムに揺れる動きをします。  
 DOShakePosition(float duration, float/Vector3 strength, int vibrato, float randomness, bool snapping, bool fadeOut)
 DOShakeRotation(float duration, float/Vector3 strength, int vibrato, float randomness, bool fadeOut)
 DOShakeScale(float duration, float/Vector3 strength, int vibrato, float randomness, bool fadeOut)
IEnumerator ShakeSample()
    {
        this.transform.DOShakePosition(duration: 1.0f, strength: 0.5f, vibrato: 10, randomness: 10,snapping:false,fadeOut:true);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOShakeRotation(duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOShakeScale(duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
    }
まとめ
キューブのオブジェクトにAddComponentし、ButtonObjectを割り当てると動作します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class DOTween : MonoBehaviour
{
    public Button moveButton;
    public Button jumpButton;
    public Button rotateButton;
    public Button scaleButton;
    public Button punchButton;
    public Button shakeButton;
    public Button resetButton;
    // Use this for initialization
    void Start ()
    {
        moveButton.onClick.AddListener(() => {
            //Debug.Log("Move Clicked.");
            MoveSample();
        });
        jumpButton.onClick.AddListener(() => {
            //Debug.Log("Jump Clicked.");
            JumpSample();
        });
        rotateButton.onClick.AddListener(() => {
            //Debug.Log("Rotate Clicked.");
            RotateSample();
        });
        scaleButton.onClick.AddListener(() => {
            //Debug.Log("Scale Clicked.");
            ScaleSample();
        });
        punchButton.onClick.AddListener(() => {
            //Debug.Log("punch Clicked.");
            StartCoroutine( PunchSample());
        });
        shakeButton.onClick.AddListener(() => {
            //Debug.Log("Shake Clicked.");
            StartCoroutine(ShakeSample());
        });
        resetButton.onClick.AddListener(() => {
            //Debug.Log("Reset Clicked.");
            ResetSample();
        });
    }
    void MoveSample()
    {
        this.transform.DOMove(endValue: new Vector3(5.0f, 0.5f, 0), duration: 2.0f);
    }
    void JumpSample()
    {
        this.transform.DOJump(endValue: new Vector3(5.0f, 0.5f, 0),jumpPower: 5.0f,numJumps: 2,duration: 2.0f);
    }
    void RotateSample()
    {
        this.transform.DORotate(endValue: new Vector3(360f, 0, 0), duration: 1.0f ,mode: RotateMode.FastBeyond360);
    }
    void ScaleSample()
    {
        this.transform.DOScale(endValue: new Vector3(2, 2, 2),duration: 1.0f);
    }
    IEnumerator PunchSample()
    {
        this.transform.DOPunchPosition(punch: new Vector3(1f, 0f, 1f),duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOPunchRotation(punch: new Vector3(20f, 0f, 20f),duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOPunchScale(punch: new Vector3(0.1f, 0.1f, 0.1f),duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
    }
    IEnumerator ShakeSample()
    {
        this.transform.DOShakePosition(duration: 1.0f, strength: 0.5f, vibrato: 10, randomness: 10,snapping:false,fadeOut:true);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOShakeRotation(duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
        this.transform.DOShakeScale(duration: 1.0f);
        yield return new WaitForSeconds(1.0f);
    }
    void ResetSample()
    {
        this.transform.position = new Vector3(0,0.5f,0);
        this.transform.rotation = new Quaternion(0.0f,0.0f,0.0f,0.0f);
        this.transform.localScale = new Vector3(1, 1, 1);
    }
}













