Created
June 2, 2018 19:24
-
-
Save MattRix/b41aa80059d230f93e5693bcf29ae5f7 to your computer and use it in GitHub Desktop.
Thing that makes text scale in on the y-axis.
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 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