- Home /
Newline problem in GUI.Box when text comes from database
I am having a string problem, that really doesn't make any sense to me.
The following works as expected, including the newlines...
string Text="This is\na Test";
GUI.Box(new Rect(DialogWindow), Text, MySkin.box);
The above displays a box at the DialogWindow location with the following...
This is
a Test
Which is perfect.
Now, if instead of setting the string in code like that, I pull the exact same string from a SQLite database, it displays like...
This is\na Test
Debugging out both the string created in code, and the string pulled from the database, they appear to be identical.
I cannot figure out why, when using the string from the database, the newline character is displayed instead of a linefeed being inserted.
Anyone else ever see this problem? Any ideas how to get this to work as expected?
Any help is greatly appreciated.
-Larry
EDIT: As a test, I just copied the text from the SQLite table, and pasted it into the hard coded string, just to make sure they were truly identical. Same results. The hard coded string works, the string pulled from the database does not.
Answer by Berenger · Feb 05, 2012 at 05:52 PM
Here is an experiment for you to understand what's happening here :
Create a script with that code, put it on any object.
var text : String;
function OnGUI(){
GUI.Label( Rect( 10, 100, 500, 50 ), "First line\nSecond line" );
GUI.Label( Rect( 10, 200, 500, 50 ), text );
}
In the inspector, fill the variable text with : First line\nSecond line. Then run, and you'll notice that the first label is ok, when the second one isn't. I know more or less where that comes from, but you'll need a pro for details. I think it's about texte formatting done by the editor (monodevelop in my case) which isn't done by unity editor, neither by your SQL apparently. To fix that, I suggest you split the sentence in your SQL into so many lines, then assemble it in your code like such :
str = line1fromSQL + "\n" + line2fromSQL.
You are absolutely correct. I just tried what you suggested, and the first line displayed as expected. The 2nd displayed the newline character the same as when I pulled it from the database. So apparently the database is not the problem.
I will try to figure out the best way to break this into multiple lines in the database, and assemble them. Gonna be a bit of a trick with the way I have it setup though.
I am basically creating a data driven dialog system, and splitting this into multiple lines is going to cause some duplication in the players possible responses. So I will have to work around that also.
I think I am going to play around with string formatting a bit, and see if I can figure out what is different, and duplicate the formatting in code. Would be a lot easier all the way around if I could just get the newlines to work ;)
Thanks! -Larry
Ok, I figured out a work-around based on your information. Just in case anyone else needs to workaround a similar problem, here is what I did.
First I substituted the | character for the newline in the database, then after getting the string from the database, I split the string on the | character, then concatenated each string in the returned array into a new string, appending "\n" to the end of each.
Using this new concatenated string, I got the results that I was expecting.
Again thanks for pointing me in the right direction!
-Larry
Answer by Banemus · Aug 30, 2013 at 09:05 AM
Just do this:
string
rumor_compilation;
rumor_compilation = RumorTable[ rumor_index ].Replace( "\\n", "\n" );
GUI.Box( new Rect( half_screen_width - 150, half_screen_height - 130 + 90 * rumor_index, 300, 90 ), rumor_compilation );
unity automatically adds an extra \\ to the \\n in there