1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

【Unity】DoTweenを使ってUIを上下ループさせて目立たせる

コード

UIPointer.cs
using UnityEngine;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;

public class UIPointer : MonoBehaviour
{
    [SerializeField] private float _durationSeconds = 0.2f;

    private float _initY = 0;
    private float _endY = 0;
    [SerializeField] private float _moveY = 3f;

    private TweenerCore<Vector3, Vector3, VectorOptions> _tweener;

    public void InitDisplay(Vector3 position)
    {
        // 指定した位置に移動する
        transform.position = position;

        // 初期化
        _initY = transform.position.y;
        _endY = _initY - _moveY;

        // 表示する
        gameObject.SetActive(true);

        _tweener = transform
            .DOMoveY(_endY, duration: _durationSeconds)
            .SetEase(Ease.InCubic)
            .SetLoops(-1, LoopType.Yoyo)
            .Play();
    }

    private void OnDisable()
    {
        _tweener.Kill();
    }

    [ContextMenu("TestDisplay")]
    void TestDisplay()
    {
        InitDisplay(Vector3.zero);
    }
}

エディタ

image.png

デモ

参考

[Unity] DOTweenを使ってUI要素を点滅させる

余談

地味にコピペですぐ使えるコードが見つからなかったので記事にしました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
1
Help us understand the problem. What are the problem?