Created
March 11, 2018 20:23
-
-
Save MattDahEpic/1e53582e3dd610b07a5ef5e904fefc96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using TMPro; | |
using UnityEngine; | |
//Adapted from the TMP vertex jitter example | |
public class TMPSquiggleTag : MonoBehaviour { | |
private TMP_Text m_TextComponent; | |
private bool hasTextChanged; | |
void Awake() { | |
m_TextComponent = GetComponent<TMP_Text>(); | |
} | |
void OnEnable() { | |
// Subscribe to event fired when text object has been regenerated. | |
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED); | |
} | |
void OnDisable() { | |
TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED); | |
} | |
void Start() { | |
StartCoroutine(DoSquiggle()); | |
} | |
void ON_TEXT_CHANGED(Object obj) { | |
if (obj == m_TextComponent) | |
hasTextChanged = true; | |
} | |
IEnumerator DoSquiggle () { | |
m_TextComponent.ForceMeshUpdate(); //force the mesh to be generated before the end of the frame | |
TMP_TextInfo textInfo = m_TextComponent.textInfo; | |
TMP_MeshInfo[] cachedMeshInfo = textInfo.CopyMeshInfoVertexData(); | |
Matrix4x4 matrix; | |
hasTextChanged = true; | |
List<int> squiggleChars = new List<int>(); | |
while (true) { | |
if (hasTextChanged) { | |
cachedMeshInfo = textInfo.CopyMeshInfoVertexData(); // Update the copy of the vertex data for the text object. | |
//make the squiggle tag characters invisible and record what characters should wiggle | |
squiggleChars.Clear(); | |
if (m_TextComponent.text.Contains(@"<squiggle>") && m_TextComponent.text.Contains(@"</squiggle>")) { | |
int beginSearch = 0; | |
for (int i = 0; i < Regex.Matches(m_TextComponent.text,@"<squiggle>").Count; i++) { | |
int beginBracket = m_TextComponent.text.IndexOf(@"<squiggle>",beginSearch); | |
int endBracket = m_TextComponent.text.IndexOf(@"</squiggle>",beginSearch); | |
//hide brackets | |
for (int j = beginBracket; j < beginBracket + 10; j++) textInfo.characterInfo[j].isVisible = false; | |
for (int j = endBracket; j < endBracket + 11; j++) textInfo.characterInfo[j].isVisible = false; | |
//put down the correct characters to be squiggled | |
for (int c = beginBracket+10; c < endBracket; c++) { | |
squiggleChars.Add(c); | |
} | |
//make sure you dont just keep doing this pair of squiggle tags if there's others to do | |
beginSearch = endBracket; | |
} | |
} | |
hasTextChanged = false; | |
} | |
int characterCount = textInfo.characterCount; | |
if (characterCount == 0) { yield return new WaitForSeconds(0.25f); continue; } //wait for text to be added, if its not already | |
for (int i = 0; i < characterCount; i++) { | |
TMP_CharacterInfo charInfo = textInfo.characterInfo[i]; | |
if (!charInfo.isVisible) continue; //skip invisible characters (the squiggle tags) | |
//offset the vertical position by sine(character index in string) | |
if (squiggleChars.Contains(i)) { | |
//... | |
} | |
} | |
yield return new WaitForSeconds(0.05f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment