Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created June 2, 2018 19:31
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/f3769fae46139d50b42f295ce3b4ec5e to your computer and use it in GitHub Desktop.
Save MattRix/f3769fae46139d50b42f295ce3b4ec5e to your computer and use it in GitHub Desktop.
Same as the other JonEffect but with multiple characters easing in at a time
using UnityEngine;
using System.Collections;
using TMPro;
public class FancyJonEffect : MonoBehaviour
{
TextMeshProUGUI text;
[Range(0f,1f)]
public float buildInPercent = 0;
public int numCharsAtATime = 5;
void Awake()
{
text = GetComponent<TextMeshProUGUI>();
}
public void Update()
{
buildInPercent = Mathf.Clamp01(buildInPercent);
numCharsAtATime = Mathf.Max(1,numCharsAtATime);
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 = Ease(Mathf.Clamp01((buildInPercent-charPercent)/(percentPerChar*(float)numCharsAtATime)));
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);
}
}
float Ease(float t)
{
return 1f - (1f-t) * (1f-t) * (2.70158f * (1f-t) - 1.70158f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment