madalaski

Rainbow Word Wobble

Aug 16th, 2020
5,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class WordWobble : MonoBehaviour
  7. {
  8.     TMP_Text textMesh;
  9.  
  10.     Mesh mesh;
  11.  
  12.     Vector3[] vertices;
  13.  
  14.     List<int> wordIndexes;
  15.     List<int> wordLengths;
  16.  
  17.     public Gradient rainbow;
  18.  
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.         textMesh = GetComponent<TMP_Text>();
  23.  
  24.         wordIndexes = new List<int>{0};
  25.         wordLengths = new List<int>();
  26.  
  27.         string s = textMesh.text;
  28.         for (int index = s.IndexOf(' '); index > -1; index = s.IndexOf(' ', index + 1))
  29.         {
  30.                 wordLengths.Add(index - wordIndexes[wordIndexes.Count - 1]);
  31.                 wordIndexes.Add(index + 1);
  32.         }
  33.         wordLengths.Add(s.Length - wordIndexes[wordIndexes.Count - 1]);
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         textMesh.ForceMeshUpdate();
  40.         mesh = textMesh.mesh;
  41.         vertices = mesh.vertices;
  42.  
  43.         Color[] colors = mesh.colors;
  44.  
  45.         for (int w = 0; w < wordIndexes.Count; w++)
  46.         {
  47.             int wordIndex = wordIndexes[w];
  48.             Vector3 offset = Wobble(Time.time + w);
  49.  
  50.             for (int i = 0; i < wordLengths[w]; i++)
  51.             {
  52.                 TMP_CharacterInfo c = textMesh.textInfo.characterInfo[wordIndex+i];
  53.  
  54.                 int index = c.vertexIndex;
  55.  
  56.                 colors[index] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index].x*0.001f, 1f));
  57.                 colors[index + 1] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index + 1].x*0.001f, 1f));
  58.                 colors[index + 2] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index + 2].x*0.001f, 1f));
  59.                 colors[index + 3] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index + 3].x*0.001f, 1f));
  60.  
  61.                 vertices[index] += offset;
  62.                 vertices[index + 1] += offset;
  63.                 vertices[index + 2] += offset;
  64.                 vertices[index + 3] += offset;
  65.  
  66.                
  67.             }
  68.         }
  69.  
  70.        
  71.  
  72.         mesh.vertices = vertices;
  73.         mesh.colors = colors;
  74.         textMesh.canvasRenderer.SetMesh(mesh);
  75.     }
  76.  
  77.     Vector2 Wobble(float time) {
  78.         return new Vector2(Mathf.Sin(time*3.3f), Mathf.Cos(time*2.5f));
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment