- Home /
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"
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.
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!
That's because the correct version is "\r\n", not "\n"
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
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');
}
}