Skip to content

Instantly share code, notes, and snippets.

@firebellys
Created November 13, 2016 04:01
Show Gist options
  • Select an option

  • Save firebellys/80e92c4f4140b9a890ec7ddbb7e312b5 to your computer and use it in GitHub Desktop.

Select an option

Save firebellys/80e92c4f4140b9a890ec7ddbb7e312b5 to your computer and use it in GitHub Desktop.
A slow reveal script with paging support for TextMeshPro
// Copyright (C) 2014 - 2016 Stephan Bouchard - All Rights Reserved
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System.Collections;
using TMPro;
using UnityEngine;
namespace Assets.Scripts
{
public class SlowRevealScript : MonoBehaviour
{
private TextMeshPro _mTextMeshPro;
// Sets the reveal speed in seconds.
public float RevealSpeed = 0.01f;
// The current page of the text, needs to be changed when you display the next page.
public int CurrentPage = 0;
// Lets other scripts know when to allow the next page to load.
public bool IsWriting;
void Awake()
{
IsWriting = true;
_mTextMeshPro = GetComponent<TextMeshPro>();
}
IEnumerator Start()
{
// Force and update of the mesh to get valid information.
_mTextMeshPro.ForceMeshUpdate();
var totalVisibleCharacters = _mTextMeshPro.textInfo.characterCount; // Get # of Visible Character in text object
var counter = 0;
var visibleCount = 0;
while (true)
{
visibleCount = counter % (totalVisibleCharacters + 1);
_mTextMeshPro.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?
if (_mTextMeshPro.textInfo.pageInfo[CurrentPage].lastCharacterIndex >= visibleCount)
{
IsWriting = true;
counter += 1;
}
else
{
IsWriting = false;
}
yield return new WaitForSeconds(0.01f);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment