redbluegames / unity-text-typer

Text typing effect for Unity uGUI Text components
MIT License
307 stars 28 forks source link

Modify TypedTextGenerator to use TextGenerator lines #12

Open FeeeshMeister opened 7 years ago

FeeeshMeister commented 7 years ago

The text generator currently moves an alpha tag to change what characters are visible, which is not optimal for garbage collection. A solution to this is to use the following code in place of the function that changes the location of the alpha tag, but some changes would likely need to be made since this is mostly unmodified from the system that I use. // textField is the text object that the player sees, and is also where the unformatted text starts // I don't remember what tempString does, I just know that you need it // I also don't remember what start or end index do , but again, they are very important public void InitializeText() { textField.color = Color.clear; textField.text = text; Canvas.ForceUpdateCanvases(); for (int i = 0; i < textField.cachedTextGenerator.lines.Count; i++) { int startIndex = textField.cachedTextGenerator.lines[i].startCharIdx; int endIndex = (i == textField.cachedTextGenerator.lines.Count - 1) ? textField.text.Length : textField.cachedTextGenerator.lines[i + 1].startCharIdx; int length = endIndex - startIndex; tempString += textField.text.Substring(startIndex, length) + "\n"; } textField.text = string.Empty; textField.color = Color.white; // Replace with whatever color you need text = tempString; } I'm sorry I don't know as much as I should about a script that I wrote, but I wrote this a very long time ago, and I never used to put comments in my code (I have since learned my lesson), so I hope you can make at least some sense of this. Despite all of these problems with it, I hope you get at least some use out of this script even though it was very poorly done, and i will probably need to redo it at some point

FeeeshMeister commented 7 years ago

It looks like Github completely messed up the code, so here it is

public void InitializeText() { textField.color = Color.clear; textField.text = text; Canvas.ForceUpdateCanvases(); for (int i = 0; i < textField.cachedTextGenerator.lines.Count; i++) { int startIndex = textField.cachedTextGenerator.lines[i].startCharIdx; int endIndex = (i == textField.cachedTextGenerator.lines.Count - 1) ? textField.text.Length : textField.cachedTextGenerator.lines[i + 1].startCharIdx; int length = endIndex - startIndex; tempString += textField.text.Substring(startIndex, length) + "\n"; } textField.text = string.Empty; textField.color = Color.white; // Replace with whatever color you need text = tempString; }

edwardrowe commented 7 years ago

Thanks for submitting the code sample! Some day soon I'll look to adapt the typer for Unity 2017 and Text Mesh Pro, and maybe this will come in handy.