Search Unity

TextMesh Pro TMPro Input Field saving text to text file

Discussion in 'UGUI & TextMesh Pro' started by jordano00, Oct 16, 2019.

  1. jordano00

    jordano00

    Joined:
    Jul 3, 2016
    Posts:
    10
    Hello everyone, I'm currently working on a script where the user enters 1st sentence then press enter or return and then enter 2nd sentence and I want to save it to a .txt file. (input field multiline newline option)

    ie:
    sentence 1
    sentence 2

    So far, I am able to do that but when I check the .txt file it is showing up as:

    sentence 1
    sentence 2

    I want the text to be saved as:

    sentence 1 /n sentence 2

    since when I retrieve the data from the .txt file, I obtain it by lines.

    When I hit enter in the inputfield, it creates a new line on the .txt file as well causing disruptions in the line contents, so I wanted the input field string to be saved in 1 line in the .txt file instead of 2 lines.

    Alternatives are also welcome!

    Thank you
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you want to convert the line breaks into escape-sequences, just use String.Replace before you save it (and presumably again in reverse after you load it, to convert it back).

    But notice that you might have problems if the user types an actual "/n" into the field. The typical solution for this is to also escape the escape character itself; e.g. replacing each "real" slash with a double slash.