• 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 5errorui

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 : MonoBehaviour { 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

State machine with multiple player inputs? 1 Answer

ERROR, please help !! "ReleaseForRunning" 0 Answers

Почему после перехода на другую версию Unity при создании кнопки она не откликается на геймпад и клавиатуру? 0 Answers

How to create a turn based battle system? 2 Answers

Dialouge isnt working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges