Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ClauGabriel19 · Jan 27, 2017 at 06:39 PM · c#unity 5uierror

How can I make a new line in Unity UI text?

Hello. How can I make a new line in Unity UI text? I tried many methods, but every time I have no results.

This is the code:

 void Start()
 {
     StartCoroutine(Check());

     
     TextGameObject.text = "Press The Quotes Button";

 }



 public void SwitchText(){
     QuoteToShow = lineArray [Random.Range (0, lineArray.Length)];
 

     TextGameObject.text = QuoteToShow; 

 }

 private IEnumerator Check()
 {
     WWW w = new WWW("http://site.com/quotes.txt");
     yield return w;
     if (w.error != null)
     {
         Debug.Log("Error .. " +w.error);
         textFromServer = textFromLocal;
         lineArray = textFromServer.Split("\n"[0]);

     }
     else
     {
         Debug.Log("Found ... ==>" +w.text +"<==");
         textFromServer = w.text;
         lineArray = textFromServer.Split("\n"[0]); 

     }


 }



And this is the text that I want to be on different lines:

“ Start by doing what's necessary, "\n" then do what's possible, \n and suddenly you are doing the impossible. ” \r\n\r\n - Francis of Assisi - “ The best and most beautiful ; things in the world cannot be ; seen or even touched, they must be felt with the heart. ” \n\r ; - Helen Keller -

This text is located in "quotes.txt"

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FortisVenaliter · Jan 27, 2017 at 08:23 PM

If you have the server return actual codes for \r\n, which is the correct method, then the String value will actually be "\\r\\n". So, you just need to do a String.Replace() to make the "\\r\\n"s into "\r\n"s.

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 dimmu1234 · Jan 27, 2017 at 08:32 PM 1
Share

Hi FortisVanaliter!

I am co-worker with ClauGabriel19 , even if we try without taking the text from the server, we still cannot split the text using /n. If we copy paste the text in the public string textExample = "what's necessary, "\n" then do what's possible"; the text still don`t splits.

I think that is something from Unity UI? I couldn`t find any solutions in the last days. Thats why we posted here.

Thanks!

avatar image FortisVenaliter dimmu1234 · Jan 27, 2017 at 08:42 PM 0
Share

That's because the correct version is "\r\n", not "\n"

avatar image ClauGabriel19 FortisVenaliter · Jan 27, 2017 at 09:31 PM 0
Share

Hi Fortis!

We tried with your sample, and it works, but it works in a weird way :D. I`ve attached a image to see whats about.

What we want to do, its to take a txt file from the WWW from a server, after that store the text into a String, after that, when player random presses a button, to display a quotte. But we want the quotte to be splitted in different lines (for styling purpose).

Taking the txt file from server works, but we are having problems with this split thing.

Imagine this: - We have this text: Abdi hamem lem dims. - We want to split this to show it in the Unity UI Text component in this way (on the server will be saved: Line one: Abdi Line two: hamem Line tree lem dims.

How we can do this? We tryed to add in the Unity UI Text Game object this code:

**public class TextSplitterByChar : $$anonymous$$onoBehaviour { Text txt; // Use this for initialization void Start () { txt = this.gameObject.GetComponent < Text > (); }

 // Update is called once per frame
 void Update(){
     txt.text.Split("\\r\\n"[0]);  
 }**


 

++++Late update:

Here is the txt file stored: http://bull-software.com/claudiu/quotes.txt

alt text

Show more comments
Show more comments
avatar image
0

Answer by ClauGabriel19 · Jan 28, 2017 at 08:47 AM

SOLUTION HERE

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 using UnityEngine.UI;
 public class RandomTextSwitch : MonoBehaviour {
     public string[] lineArray; 
     public string QuoteToShow;
     public Text TextGameObject; 
     public string textFromServer;
     public string textFromLocal = "“ Start by doing what's necessary, \"\\n\" then do what's possible, \\n and suddenly you are doing the impossible. ” \\r\\n\\r\\n - Francis of Assisi -\n“ The best and most beautiful ; things in the world cannot be ; seen or even touched, they must be felt with the heart. ” \\n\\r ; - Helen Keller -";
     string[] textFromServerTest;
     string[] Elements;
     string multiLine;
 
     void Start()
     {
         StartCoroutine(Check());
         TextGameObject.text = "Press The Quotes Button";
 
     }
 
 
 
     public void SwitchText(){
         Elements = new string[textFromServerTest.Length]; //set array lenght 8 here
         Elements[0] = textFromServerTest [Random.Range(0,textFromServerTest.Length)];
         multiLine = Elements [0].Replace ("***", "\r\n");
         TextGameObject.text = multiLine;
     }
 
     private IEnumerator Check()
     {
         WWW w = new WWW("http://bull-software.com/claudiu/quotes.txt");
         yield return w;
         if (w.error != null)
         {
             Debug.Log("Error .. " +w.error);
             textFromServer = textFromLocal;
             lineArray = textFromServer.Split("\n"[0]);
         }
         else
         {
             textFromServer = w.text;
 
         }
     
           textFromServerTest = w.text.Split('\n');
 
         }
 
 }
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

9 People are following this question.

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

Related Questions

Dragable UI Around the Player Using a Radius 1 Answer

[C#] Unity asks for a } for a reason I don't know ( '}' expected ) 1 Answer

How to set a string to a UI text? 1 Answer

PrefabUtility does not exist? 1 Answer

I keep getting compiler errors even though my script has no errors? 2 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