- Home /
How can I break an UIText on OnClick of an button without scripts ?
Hi,
I'm looking here on the answers and on the google, but on my searches I can't reach nothing.
I'm trying to replace an UIText content on button event OnClick.
It work, but, without break lines.
It's possible ?
To explain:
I have an simple scene, with 2 buttons and 1 text.
each button change the text, and I can't have more than one UIText, I need to put all in the same UIText.
My UIText was created like this:
But, on the text I have an big area to write and here I can hit ENTER.
Then, I put the event OnClick to change the text, bu here I have only one line to write...
And, when I hit the button, my text change to:
How can I do the break line here ? Or it's impossible and I need to really use script ?
My unity version is:
Very thanks for your time and your help.
Answer by TheBlackBox · Mar 22, 2018 at 11:03 PM
I don't believe that it is possible to do this within the editor as the typical line break notifier "\n" is recognised only as text to the inspector.
I believe that your best bet is probably to create a script that reads your texts and replaces any "\n" text with \n which should count as a return key.
For example:
string newString;
Text textBox;
void ReadText()
{
newString = textBox.text.Replace("\\n", "\n");
textBox.text = newString;
}
The above code will read your string and replace any "\\n" with "\n".
So if you want to add a return to your text, type "\\n" in the place you want a line break, and this script will go in and replace that via code with "\n" which counts as a line break.
It's stupid I know, but that's just how the text is interpreted.
Answer by David_Farina_BR · Mar 23, 2018 at 12:23 PM
Very Thanks, @TheBlackBox, I don't want to use scripts, but an generic script changing an word key to line breaks already help-me. I try here and with "\\n" my text return with line break and a "\" in my text, then I only change the keyword and work perfect. Thanks.
Your answer
Follow this Question
Related Questions
Canvas Button persistence 0 Answers
Disable/Enable Buttons and Text plz help jc 1 Answer
Use Canvas Buttons as Input Axis 1 Answer
How to attach a canvas to a game object properly? 1 Answer
Unity UI button reacting whole screen 0 Answers