\n Not picked up when reading string from file's name
I have a bunch of images with various names. These images are loaded in, set as textures on a plane, then a script on each plane is set to hold the name of the file that the texture came from. I then am trying to display all these names via GUI.Label which works all just fine.
The only thing is, I wanted to have line breaks in some of the file names that are displayed via GUI.Label but when I put a \n into the filename, this doesn't come out as being an actual linebreak in the GUI.Label, instead the GUI.Label just shows \n.
Is there anyway to name a file with a \n in it, then read that file in, set a string equal to the file's.name, then when you display that string via GUI.Label have it actually use the \n to start a new line?
Answer by Owen-Reynolds · Aug 11, 2011 at 04:52 PM
'\n' for a linefeed only works for strings entered inside of a program (ex:string w="a\nb";
is 2 lines). In a text file or the Inspector you're normally expected to use the return key (in the Inspector the [TextArea] attribute allows multiple lines).
An old trick is to pick one char to stand for a linefeed, say $, then convert it into one later:
fName=fName.Replace('$','\n'); // converts "clown city$part I" into two lines.
Any symbol works. Often you pick one you'd never normally use: @, *, and so on. Or, if you already have \n's everywhere, you use Replace to turn that into a real linefeed. This says to turn the 2 letters "slash" and "n" into the single thing '\n':
fName=fName.Replace("\\n","\n");
Note that Replace doesn't change the string calling it. fName.Replace('$','\n');
does nothing. You have to catch the result using an =.
Tried everything but this $-trick was the only one that worked. Thanks!
I'm using yarn spinner for interactive storytelling in my game and i couldn't find a way to break lines within yarn "lines" (to print multiple-line paragraphs in one block).
If anyone has the same issue, open DialogUI script of the yarn spinner. On line 287 use your own replace method on the "text" variable to break lines with your custom character and not go to the next line of the node.
I love you @Owen-Reynolds. I was looking for this anwser for ages. Your description of the issue, why does it work in this way and your simple trick to solve it saved me. I'm using yarn spinner for interactive storytelling in my game and i couldn't find a way to break lines within yarn "lines" (to print multiple-line paragraphs in one block).
If anyone has the same issue, open DialogUI script of the yarn spinner on on the line 287 use your own replace method on the "text" variable to break lines with your custom character or even with "\n" wich will now work as line feed and not as "\" + "n".
Your answer
Follow this Question
Related Questions
Error FormatException: Input string was not in a correct format. 0 Answers
Parse string value to add to enum List 2 Answers
I am getting this error when i try to parse a string to get float value. 0 Answers
Format Exception when trying to parseFloat(String) 2 Answers
Help int.Parse "Input String was not in the correct format" PROBLEM 0 Answers