Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

TextMesh - Need to add a newline character

Discussion in 'Scripting' started by melmonkey, May 7, 2010.

  1. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    I have text to be rendered in a TextMesh, coming from an external file. I need to break apart long text into multiple lines. I naturally assumed that \n or \r would work, but alas, it is not so.

    The docs say to hold ALT when hitting RETURN to add a new line, that doesn't work in the inspector, and even if it did, it wouldn't help with my issue. :)

    Anyone know how to accomplish this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually, \n does work. You can also use System.Environment.NewLine.

    --Eric
     
  3. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    Well, I have tried multiple things, and nothing gets the string to split.

    My input data looks like this

    data1,data2,data3, etc.

    No quotes around the data. When I read it in, I split it based on the comma, and store it in a String[]. Shoving the data into the array is the only thing that defines it as a string, and I think that may be the problem.

    I have tried modifying the data file to include quotes around the data members, but that doesn't fix it.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, now I'm confused, I thought we were talking about newline characters?

    --Eric
     
  5. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    We are. I was just using that as an example of what the data looks like in the file I am reading in. Feel free to insert a newline character anywhere in the data:

    data1,dat\na2,d\nata3

    Using this has no effect:

    "data1","dat\na2","d\nata3"

    nor this:

    "data1","dat\ra2","d\rata3"

    I will set up a test project to illustrate.
     
  6. RElam

    RElam

    Joined:
    Nov 16, 2009
    Posts:
    375
    In code seems to work fine, but I had to do some replacement from inspector entered text to get it to work, but it's pretty simple...

    Code (csharp):
    1. text=text.Replace("\\n","\n");
     
  7. jpatinop80

    jpatinop80

    Joined:
    Jul 29, 2012
    Posts:
    55
    Thanks this is the best solution if you read from external file!!!
     
    iwillbenice likes this.
  8. iwillbenice

    iwillbenice

    Joined:
    Jun 7, 2013
    Posts:
    21
    simple as it should be, thanks!!!