- Home /
Get Lines of a UI text
Hi all,
How can I get the lines of Text that is showing in the game, not in the inspector window, as u see in the picture:
myText = GetComponent<Text>();
string[] lines = myText.text.Split('\n');
try{print(lines[0]);}catch{}
try{print(lines[1]);}catch{}
//real output :
//New Text
//What I expect:
//New
//Text
Honestly without some trial and error I'm not sure if I can provide the correct solution.
What I can tell you, though, is that I've used a StringBuilder in the past to populate the contents of a Text field. When I used stringBuilder.AppendLine(someText) multiple times, it did correctly populate the contents of the Text field on separate lines which indicates that the line endings do somehow come across, at least internally. Being able to expose that internal string, however, is a different story. Perhaps the "useRichText" boolean may change the getter of ".text". Have you tried setting this to false and then getting the text?
Answer by sarahnorthway · Dec 31, 2016 at 02:40 AM
Text.cachedTextGenerator has the info you need. It won't show correct line splitting until the next frame, but you can force it to update early by calling Canvas.ForceUpdateCanvases().
myText.text = "this is a very extremely super duper incredibly unbelievably very long sentence";
Canvas.ForceUpdateCanvases();
for (int i = 0; i < myText.cachedTextGenerator.lines.Count; i++) {
int startIndex = myText.cachedTextGenerator.lines[i].startCharIdx;
int endIndex = (i == myText.cachedTextGenerator.lines.Count - 1) ? myText.text.Length
: myText.cachedTextGenerator.lines[i + 1].startCharIdx;
int length = endIndex - startIndex;
Debug.Log(myText.text.Substring(startIndex, length));
}
Thank you very much! The way you get the endIndex is so clever! (And t?x:y conditional operator is so usefull!)
Revisiting this with TextMeshPro, use TextMeshProUGUI.ForceMeshUpdate(true, true) instead of Canvas.ForceUpdateCanvases(), then TextMeshProUGUI.textInfo to fetch line info.
Answer by KeraStudios · Oct 22, 2017 at 05:53 PM
text = myText.GetComponent(); lines = text.cachedTextGenerator.lineCount;
Answer by juicyz · Sep 27, 2016 at 07:03 PM
You need to first get the GameObject that it is associated with. Then you need to get the text component of it. Then once you have the Text component, you can use .text to get/set the text.
To get the Gameobject, you can do something like GameObject.Find(); //can take in a tag or game object name.
Then a .GetComponent(); to get the text component
Then .text.
// You also need the correct imports, think it's only these two.
// Might have a text one too, cant remember. You can figure that out
using UnityEngine;
using UnityEngine.UI;
GameObject textGO = GameObject.Find("Text");
Text textInfo = textGO.GetComponent<Text>();
string[] lines = textInfo.text.Split('\n');
try{print(lines[0]);}catch{}
try{print(lines[1]);}catch{}
For reference:
https://docs.unity3d.com/ScriptReference/UI.Text.html https://docs.unity3d.com/ScriptReference/GameObject.html
This is actually attached to the text object, and the "myText" is the Text component that is attached to the gameobject. my problem is the myText.text returns the value that has been entered in the inspector, but I want what player see in the game window.
I will edit my code in first post.
anyway thanks for the reply
ohhh I see your problem... haha, that's funny. So the text field is "New Text" as said in the inspector. The split isn't working because the text is "New Text". The reason the text on the screen is different is because the text box isn't big enough to support the string "New Text" on one line so it wraps it. There is no '\n'
You could split it on ' ' (space) ins$$anonymous$$d but I don't think that is what you want?
string[] lines = myText.text.Split(' ');
Will give you:
New
Text
No , that's not what I want, I need more general solution.
There must be a way to see how many characters can a line hold in UI/Text, then we can split the words and count the length of every word and see by adding which word the line length goes over that maximum length. then we can consider it one line.
but I can't find such a property for UI/Text.
Answer by Varaughe · Apr 05, 2020 at 04:41 PM
Did you try this? https://assetstore.unity.com/packages/tools/gui/fully-flexible-ui-text-148050 A single UIText is replaced by UITexts, one for each character. In your case, it seems that you need to separate by "\n", the basic idea should be the same.
Your answer
Follow this Question
Related Questions
Text UI is not being assigned 2 Answers
Multiple Rich Text Not Working 1 Answer
Selecting a part of string in input field 1 Answer
Add up updating float 2 Answers