using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.UI; public class TweenColor : MonoBehaviour { public Color from = Color.white; public Color to = Color.white; public TweenStyle playStyle = TweenStyle.Once; public AnimationCurve curve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f)); public float timer = 1f; private Tween tweener = null; private SpriteRenderer spriteRender; private Image image; private Text text; void Awake() { spriteRender = GetComponent(); image = GetComponent(); text = GetComponent(); } void Start() { PlayForward(); } public void PlayForward() { ResetToBegining(); switch (playStyle) { case TweenStyle.Once: if (spriteRender != null) { tweener = spriteRender.DOColor(to, timer).SetEase(curve); } if (image != null) { tweener = image.DOColor(to, timer).SetEase(curve); } if (text != null) { tweener = text.DOColor(to, timer).SetEase(curve); } break; case TweenStyle.Loop: if (spriteRender != null) { tweener = spriteRender.DOColor(to, timer).SetEase(curve).SetLoops(-1); } if (image != null) { tweener = image.DOColor(to, timer).SetEase(curve).SetLoops(-1); } if (text != null) { tweener = text.DOColor(to, timer).SetEase(curve).SetLoops(-1); } break; case TweenStyle.PingPong: if (spriteRender != null) { tweener = spriteRender.DOColor(to, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); } if (image != null) { tweener = image.DOColor(to, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); } if (text != null) { tweener = text.DOColor(to, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); } break; } } public void PlayReverse() { ResetToEnding(); switch (playStyle) { case TweenStyle.Once: if (spriteRender != null) { tweener = spriteRender.DOColor(from, timer).SetEase(curve); } if (image != null) { tweener = image.DOColor(from, timer).SetEase(curve); } if (text != null) { tweener = text.DOColor(from, timer).SetEase(curve); } break; case TweenStyle.Loop: if (spriteRender != null) { tweener = spriteRender.DOColor(from, timer).SetEase(curve).SetLoops(-1); } if (image != null) { tweener = image.DOColor(from, timer).SetEase(curve).SetLoops(-1); } if (text != null) { tweener = text.DOColor(from, timer).SetEase(curve).SetLoops(-1); } break; case TweenStyle.PingPong: if (spriteRender != null) { tweener = spriteRender.DOColor(from, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); } if (image != null) { tweener = image.DOColor(from, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); } if (text != null) { tweener = text.DOColor(from, timer).SetEase(curve).SetLoops(-1, LoopType.Yoyo); } break; } } public void Stop() { if (tweener != null) { tweener.Kill(); } } public void Pause() { if (tweener != null) { tweener.Pause(); } } public void Play() { if (tweener != null) { tweener.Play(); } } public void ResetToBegining() { if (tweener != null) { tweener.Kill(); } if (spriteRender != null) { spriteRender.color = from; } if (image != null) { image.color = from; } if (text != null) { text.color = from; } } public void ResetToEnding() { if (tweener != null) { tweener.Kill(); } if (spriteRender != null) { spriteRender.color = to; } if (image != null) { image.color = to; } if (text != null) { text.color = to; } } }