66 lines
1.6 KiB
C#
66 lines
1.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class UIAnimationDOTween : MonoBehaviour
|
|
{
|
|
[Header("Animation Settings")]
|
|
public bool moveHorizontal = true;
|
|
public bool moveVertical = false;
|
|
public float moveDistance = 50f;
|
|
public float moveDuration = 1f;
|
|
|
|
[Header("Sprite Settings")]
|
|
public Sprite[] sprites;
|
|
public float changeInterval = 3f;
|
|
|
|
private RectTransform rectTransform;
|
|
private Image image;
|
|
private float timer;
|
|
|
|
void Start()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
image = GetComponent<Image>();
|
|
AnimateMovement();
|
|
timer = changeInterval;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
timer -= Time.deltaTime;
|
|
if (timer <= 0f)
|
|
{
|
|
ChangeRandomSprite();
|
|
timer = changeInterval;
|
|
}
|
|
}
|
|
|
|
void AnimateMovement()
|
|
{
|
|
if (moveHorizontal)
|
|
{
|
|
rectTransform.DOAnchorPosX(rectTransform.anchoredPosition.x + moveDistance, moveDuration)
|
|
.SetEase(Ease.InOutSine)
|
|
.SetLoops(-1, LoopType.Yoyo);
|
|
}
|
|
|
|
if (moveVertical)
|
|
{
|
|
rectTransform.DOAnchorPosY(rectTransform.anchoredPosition.y + moveDistance, moveDuration)
|
|
.SetEase(Ease.InOutSine)
|
|
.SetLoops(-1, LoopType.Yoyo);
|
|
}
|
|
}
|
|
|
|
void ChangeRandomSprite()
|
|
{
|
|
if (sprites.Length == 0) return;
|
|
|
|
int randomIndex = Random.Range(0, sprites.Length);
|
|
image.sprite = sprites[randomIndex];
|
|
}
|
|
|
|
} |