- Home /
CR LF replacement in Unity Input Fields when copy and pasting
I setup a bit of code to detect line breaks using \n
char (line feed), from the TMPro_InputField
:
foreach(char c in text)
{
if (c == '\n') { splitLines.Add(cur); cur = ""; }
else {cur += c;}
}
When only characters are pressed directly into the input field, things worked great, but when I pasted in text from notepad, the results are confusing. After a ton of debugging I realize that Windows use CRLF (carriage return + line feed combo), so I simply use Replace()
to sort things out.
Problems arise when the user copy and paste. If I implement a copy button, then I can replace "\n"
with "\r\n"
manually:
public void copyFromText()
{
TextEditor textEditor = new TextEditor();
textEditor.text = textField.GetComponent<TMP_InputField>().text;
textEditor.text = textEditor.text.Replace("\n", "\r\n"); //this line here
textEditor.SelectAll();
textEditor.Copy();
}
Same goes for pasting. But when user press Ctrl+C
or Ctrl+V
, things get complicated. I could detect keys pressed, check if input field is highlighted, and replace stuff by hand, etc... but that's too much of a nuisance.
Is there a way to resolve this quicker? Or better, just config Unity to use CR LF with input fields somehow?
Your answer
Follow this Question
Related Questions
[Bug?] uGUI InputField - Last line disappeared after cut&paste 1 Answer
Input Field Characer Validation not changing 0 Answers
Is there a notepad-like (preferably a notepad++-like) in-game text editor for use in Unity5? 0 Answers
Instantiated prefab script not capturing on click 1 Answer
Android version, 0 Answers