Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SorenaCoder · Sep 27, 2016 at 05:37 PM · uitextstring

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:

alt text

 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

text.png (157.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Leuthil · Sep 27, 2016 at 08:50 PM 0
Share

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?

4 Replies

· Add your reply
  • Sort: 
avatar image
6

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));
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ManuA · Nov 19, 2017 at 10:10 PM 0
Share

Thank you very much! The way you get the endIndex is so clever! (And t?x:y conditional operator is so usefull!)

avatar image sarahnorthway · Oct 17, 2021 at 07:49 PM 0
Share

Revisiting this with TextMeshPro, use TextMeshProUGUI.ForceMeshUpdate(true, true) instead of Canvas.ForceUpdateCanvases(), then TextMeshProUGUI.textInfo to fetch line info.

avatar image
1

Answer by KeraStudios · Oct 22, 2017 at 05:53 PM

text = myText.GetComponent(); lines = text.cachedTextGenerator.lineCount;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SorenaCoder · Sep 27, 2016 at 07:21 PM 0
Share

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

avatar image juicyz SorenaCoder · Sep 27, 2016 at 07:30 PM 0
Share

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

avatar image SorenaCoder juicyz · Sep 28, 2016 at 11:33 AM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

85 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add up updating float 2 Answers

Remove the & at the end of concatenated string 0 Answers

Changing just the name of person in UI Text 0 Answers

Ui text to string ? 1 Answer

rich text issue 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges