Advertisement
Demigiant

DOTween TextMesh Pro per-character animation example

Mar 13th, 2020 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System.Collections;
  2. using DG.Tweening;
  3. using TMPro;
  4. using UnityEngine;
  5.  
  6. public class TextMeshPro_CharAnimation : MonoBehaviour
  7. {
  8.     public float timeScale = 1;
  9.     public float offsetDuration = 1;
  10.     public float rotateDuration = 1;
  11.     public float scaleDuration = 1;
  12.     public float colorDuration = 1;
  13.     public float fadeDuration = 1;
  14.     public Gradient colorGradient;
  15.     public TMP_Text tfMain, tfWSprites;
  16.  
  17.     IEnumerator Start()
  18.     {
  19.         // Increase tweens capacity because we're gonna create lots of nested tweens for all characters
  20.         DOTween.SetTweensCapacity(1000, 50);
  21.  
  22.         // Create Sequences
  23.         // ► Main text
  24.         DOTweenTMPAnimator tmpMain = new DOTweenTMPAnimator(tfMain);
  25.         // Shift vertices test
  26.         for (int i = 24; i < 32; ++i) {
  27.             float shiftY = i % 2 == 0 ? 10 : -10;
  28.             tmpMain.ShiftCharVertices(i, new Vector3(0, shiftY, 0), new Vector3(0, 0, 0), new Vector3(0, shiftY, 0), new Vector3(0, 0, 0));
  29.         }
  30.         for (int i = 24; i < 32; ++i) tmpMain.SetCharOffset(i, new Vector3(0, i % 2 == 0 ? -4 : 4, 0));
  31. //        yield return new WaitForSeconds(2);
  32.         //
  33.         int charCount = tmpMain.textInfo.characterCount;
  34.         Sequence sMain = DOTween.Sequence().Pause();
  35.         sMain.timeScale = timeScale;
  36.         for (int i = 0; i < charCount; ++i) {
  37.             Vector3 currCharOffset = tmpMain.GetCharOffset(i);
  38.             sMain.Insert(i * 0.1f, tmpMain.DOFadeChar(i, 0, fadeDuration).From());
  39.             sMain.Insert(i * 0.1f, tmpMain.DOOffsetChar(i, currCharOffset + new Vector3(0, 30, 0), offsetDuration))
  40.                 .Insert(i * 0.1f + offsetDuration, tmpMain.DOOffsetChar(i, currCharOffset + Vector3.zero, offsetDuration).SetEase(Ease.OutBack));
  41.             sMain.Insert(i * 0.1f, tmpMain.DOScaleChar(i, new Vector3(2, 5, 2), scaleDuration).From());
  42.             sMain.Insert(i * 0.1f, tmpMain.DORotateChar(i, new Vector3(-90, 60, 90), rotateDuration).From());
  43.             sMain.Insert(2 + i * 0.1f, tmpMain.DOColorChar(i, colorGradient.Evaluate(1f / charCount * i), colorDuration));
  44.             sMain.Insert(2 + i * 0.1f, tmpMain.DOPunchCharScale(i, 0.5f, colorDuration, 5));
  45.             sMain.Insert(2 + i * 0.1f, tmpMain.DOPunchCharRotation(i, new Vector3(0, 0, 30), colorDuration, 5));
  46.             sMain.Insert(2 + i * 0.1f, tmpMain.DOPunchCharOffset(i, new Vector3(0, 18f, 0), colorDuration, 5));
  47.             int delay = 5;
  48.             sMain.Insert(delay + i * 0.1f, tmpMain.DOShakeCharOffset(i, colorDuration, 30f));
  49.             sMain.Insert(delay + 2f + i * 0.1f, tmpMain.DOShakeCharScale(i, colorDuration, 5f));
  50.             sMain.Insert(delay + 4f + i * 0.1f, tmpMain.DOShakeCharRotation(i, colorDuration, new Vector3(180f, 180f, 180f)));
  51.         }
  52.         // ► Text with inline sprites
  53.         DOTweenTMPAnimator tmpSprites = new DOTweenTMPAnimator(tfWSprites);
  54.         charCount = tmpSprites.textInfo.characterCount;
  55.         Sequence sSprites = DOTween.Sequence().Pause();
  56.         for (int i = 0; i < charCount; ++i) {
  57.             sSprites.Insert(i * 0.05f, tmpSprites.DOFadeChar(i, 0, 0.3f).From());
  58.             sSprites.Insert(i * 0.05f, tmpSprites.DOScaleChar(i, 4f, 0.8f).From().SetEase(Ease.OutBack, 3));
  59.         }
  60.         yield return new WaitForSeconds(1);
  61.  
  62.         // Play all
  63.         sMain.Play();
  64.         sSprites.Play();
  65.         yield return new WaitForSeconds(4);
  66.  
  67.         // Start looping single word in main textField
  68.         for (int i = 24; i < 32; ++i) {
  69.             LoopCharScale(tmpMain, i, 1.2f, i * 0.1f);
  70.             tmpMain.DOColorChar(i, Color.yellow, 0.5f).SetEase(Ease.InOutSine).SetLoops(-1, LoopType.Yoyo).SetDelay(i * 0.1f);
  71.         }
  72.         // Start looping emoticons in inline sprites text
  73.         LoopCharScale(tmpSprites, new []{ 8, 19, 35, 45, 54 }, 1.5f);
  74.     }
  75.  
  76.     void LoopCharScale(DOTweenTMPAnimator animator, int[] charIndexes, float scale, float delay = 0)
  77.     {
  78.         for (int i = 0; i < charIndexes.Length; ++i) LoopCharScale(animator, charIndexes[i], scale, delay);
  79.     }
  80.  
  81.     void LoopCharScale(DOTweenTMPAnimator animator, int charIndex, float scale, float delay = 0)
  82.     {
  83.         animator.DOScaleChar(charIndex, scale, 0.5f).SetEase(Ease.InOutSine).SetLoops(-1, LoopType.Yoyo).From(new Vector3(1, 1, 1)).SetDelay(delay);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement