「Unity/Csharp/DoTween」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→星型に移動する) |
(→星型に移動する) |
||
| 行107: | 行107: | ||
参考:https://qiita.com/BEATnonanka/items/50cacac803f88f5074dd | 参考:https://qiita.com/BEATnonanka/items/50cacac803f88f5074dd | ||
| + | |||
| + | ===完了したら関数実行=== | ||
| + | <pre> | ||
| + | GameObject.Find("Image").transform.DOPath(movepath, 5f, PathType.Linear, PathMode.Sidescroller2D).OnComplete(CallbackComplete); | ||
| + | } | ||
| + | void CallbackComplete() | ||
| + | { | ||
| + | Debug.Log("CallbackComplete"); | ||
| + | } | ||
| + | </pre> | ||
2021年8月3日 (火) 01:21時点における版
目次
dotweenとは
アニメーションライブラリ
参考:https://qiita.com/broken55/items/df152c061da759ad1471
インストール
- unityassetsからdotweenを検索して、
- dotweenのfree ( https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676 ) をダウンロード
- installし、
- importする
- OpenDoTweenUtilityPanelを開き
- SetupDotweenボタンを押して、Applyを押す。
サンプル
x=2、y=3のrootから見た座標に1秒後に移動する。
using DG.Tweening;
using UnityEngine;
public class SampleScene : MonoBehaviour
{
void Start()
{
GameObject.Find("Image").transform.DOMove(new Vector3(2f, 3f, 0f), 1f);
}
}
繰り返し
無限繰り返し
GameObject.Find("Image").transform.DOMove(new Vector3(2f, 3f, 0f), 1f);
.SetLoops(-1, LoopType.Restart);
2回指定
GameObject.Find("Image").transform.DOMove(new Vector3(2f, 3f, 0f), 1f);
.SetLoops(2, LoopType.Restart);
遅延動作
5秒待ってから動作
.SetDelay(5f);
Easeの動き
.SetEase(Ease.Linear); // 線形 .SetEase(Ease.OutSine); // 最初速く後ゆっくり .SetEase(Ease.InExpo); // 最初ゆっくり後速く
https://github.com/Nightonke/WoWoViewPager/blob/master/Pictures/ease.png
動きを止める
オブジェクト指定
DOTween.Kill(this.transform);
tweenインスタンスを作って止める
Tween tween = GameObject.Find("Image").transform.DOMove(new Vector3(2f, 3f, 0f), 1f);
tween.Kill();
参考:https://qiita.com/broken55/items/df152c061da759ad1471
ローカル座標
DOLocalMoveとする
GameObject.Find("Image").transform.DOLocalMove(new Vector3(2f, 3f, 0f), 1f);
回転
5秒で、90度回転
.DORotate(Vector3.up * 90f, 5f)
色変更
5秒で青へ
Image image = GameObject.Find("Image").GetComponent<Image>();
image.DOColor(Color.brue, 5f).SetEase(Ease.Linear);
指定座標の上を移動する
- 2Dの場合は、PathMode.Sidescroller2Dを追加する。
- SetLookAtは、傾きが変わるx秒前に正面の向きを変える
Vector3[] movepath =
{
new Vector3(0f,0f,0f),
new Vector3(0f,100f,0f),
new Vector3(100f,100f,0f),
new Vector3(100f,0f,0f),
new Vector3(0f,0f,0f)
};
GameObject.Find("Image").transform.DOLocalPath(movepath, 5f, PathType.Linear, PathMode.Sidescroller2D)
.SetLookAt(0.1f)
.SetLoops(3, LoopType.Restart);
星型に移動する
- root座標のときは、DOPathを使う。
Vector3[] movepath = new Vector3[6];
movepath[0].Set(0f, 1f, 0f); // 1
movepath[1].Set(0.5878f, -0.8090f, 0f); // 5
movepath[2].Set(-0.9511f, 0.3090f, 0f); // 9
movepath[3].Set(0.9511f , 0.3090f, 0f); // 3
movepath[4].Set(-0.5878f, -0.8090f, 0f); // 7
movepath[5].Set(0f, 1f, 0f); // 1
// 星型頂点 https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q10137274530
GameObject.Find("Image").transform.DOPath(movepath, 5f, PathType.Linear, PathMode.Sidescroller2D)
.SetLookAt(0.01f)
.SetLoops(3, LoopType.Restart);
ローカル値を使う場合は、DOLocalPathを使う
参考:https://qiita.com/BEATnonanka/items/50cacac803f88f5074dd
完了したら関数実行
GameObject.Find("Image").transform.DOPath(movepath, 5f, PathType.Linear, PathMode.Sidescroller2D).OnComplete(CallbackComplete);
}
void CallbackComplete()
{
Debug.Log("CallbackComplete");
}
