Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created June 2, 2018 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/b41aa80059d230f93e5693bcf29ae5f7 to your computer and use it in GitHub Desktop.
Save MattRix/b41aa80059d230f93e5693bcf29ae5f7 to your computer and use it in GitHub Desktop.
Thing that makes text scale in on the y-axis.
using UnityEngine;
using System.Collections;
using TMPro;
public class JonEffect : MonoBehaviour
{
TextMeshProUGUI text;
[Range(0f,1f)]
public float buildInPercent = 0;
void Awake()
{
text = GetComponent<TextMeshProUGUI>();
}
public void Update()
{
buildInPercent = Mathf.Clamp01(buildInPercent);
var textInfo = text.textInfo;
text.ForceMeshUpdate();
for(int c = 0; c<textInfo.characterInfo.Length; c++)
{
var charInfo = textInfo.characterInfo[c];
if(!charInfo.isVisible) continue;
int matIndex = charInfo.materialReferenceIndex;
int vertIndex = charInfo.vertexIndex;
var verts = textInfo.meshInfo[matIndex].mesh.vertices;
Vector3 midPoint = (verts[vertIndex + 0] + verts[vertIndex+2])/2f;
Vector3 size = verts[vertIndex + 2] - midPoint;
float percentPerChar = 1f/(float)textInfo.characterInfo.Length;
float charPercent = c * percentPerChar;
float squashY = Mathf.Clamp01((buildInPercent-charPercent)/percentPerChar);
size.y *= squashY;
verts[vertIndex + 0] = midPoint + new Vector3(-size.x,-size.y,size.z);
verts[vertIndex + 1] = midPoint + new Vector3(-size.x,+size.y,size.z);
verts[vertIndex + 2] = midPoint + new Vector3(+size.x,+size.y,size.z);
verts[vertIndex + 3] = midPoint + new Vector3(+size.x,-size.y,size.z);
textInfo.meshInfo[matIndex].mesh.vertices = verts;
}
//NOTE: ideally you don't want to update the geometry every frame, so you could disable this when the build-in is complete.
for(int m = 0; m<textInfo.meshInfo.Length; m++)
{
text.UpdateGeometry(textInfo.meshInfo[m].mesh,m);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment