1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening;
public class DoTweenController : MonoBehaviour { [SerializeField] private Vector3 targetLocation = Vector3.zero;
[SerializeField, Range(1.0f, 10.0f)] private float moveDuration = 1.0f;
[SerializeField] private Ease moveEase = Ease.Linear;
[SerializeField] private Color targetColor;
[SerializeField, Range(1.0f, 10.0f)] private float colorChangeDuration = 1.0f;
[SerializeField, Range(0.1f, 10.0f)] private float scaleMultiplier = 3.0f;
[SerializeField] private DoTweenType doTweenType = DoTweenType.MovementOneWay;
// Different types of animation private enum DoTweenType { MovementOneWay, MovementTwoWay, MovementOneWayColorChange, MomementOneWayColorChangeScaleChange }
// Start is called before the first frame update void Start() { if(targetLocation == Vector3.zero){ // stay at original position targetLocation = transform.position; }
if(doTweenType == DoTweenType.MovementOneWay){ transform.DOMove(targetLocation, moveDuration).SetEase(moveEase); } else if(doTweenType == DoTweenType.MovementTwoWay){ StartCoroutine(MoveWithBothWays()); } else if(doTweenType == DoTweenType.MovementOneWayColorChange){ // another way DOTween.Sequence() .Append(transform.DOMove(targetLocation, moveDuration).SetEase(moveEase)) .Append(transform.GetComponent<Renderer>().material.DOColor(targetColor, colorChangeDuration).SetEase(moveEase)); } else if(doTweenType == DoTweenType.MomementOneWayColorChangeScaleChange){ DOTween.Sequence() .Append(transform.DOMove(targetLocation, moveDuration).SetEase(moveEase)) .Append(transform.GetComponent<Renderer>().material.DOColor(targetColor, colorChangeDuration).SetEase(moveEase)) .Append(transform.DOScale(scaleMultiplier, moveDuration).SetEase(moveEase)); } }
private IEnumerator MoveWithBothWays(){ // save original position Vector3 originalLocation = transform.position;
// go to traget location transform.DOMove(targetLocation, moveDuration).SetEase(moveEase);
// wait yield return new WaitForSeconds(moveDuration);
// go to original location transform.DOMove(originalLocation, moveDuration).SetEase(moveEase); } }
|