Coding linebreaks in text put on clipboard in WebGL

In my ‘game’, when the user clicks a button, a string is constructed and sent to the clipboard (using the process recommended on a previous thread in this section )

The string is destined to be pasted into a spreadsheet and so is built to include tab and linebreak markers.
For example:

string TextToCopy = "some words " + ‘\t’ + “some more words” + ‘\n’;

In what goes on the clipboard, the tab markers are recognised fine but I cannot get any linebreak markers to work.

I’ve tried several things including replacing ‘\r’ with ‘\n’ and also using Environment.newline (either by itself or declaring a string variable with it that then goes in the construction)

All methods above work in the Desktop version but not in WebGL:

I was recommended to try ‘\n\r’ or ‘\r\n’ but when I try and insert these I get the error “Too many characters in character literal”

Any suggestions would be greatly appreciated. Thanks :slight_smile:

I found the answer:

In the in index.html, where I had

var tempInput = document.createElement(“input”)

I needed to replace “input” with “textarea”:

var tempInput = document.createElement(“textarea”);

The reason is that HTML inputs can’t contain linebreaks, while textareas can.

1 Like

Here’s a version of a jslib plugin you can use, if you don’t want to change the default index.html:
Saved as WebGLPasteToBuffer.jslib
in a Plugins folder in your Assets:

mergeInto(LibraryManager.library, {
CopyToClipboard: function (arg){
var tempInput = document.createElement(“textarea”);
tempInput.value = UTF8ToString(arg);
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand(“copy”);
document.body.removeChild(tempInput);
}
});

then you can have a ClipboardUtility class:

using System.Runtime.InteropServices;
using UnityEngine;

// this is a great example of a string extension class
public static class ClipboardCopy
{
    // Using a JavaScript plugin
    [DllImport("__Internal")]
    public static extern void CopyToClipboard(string str); // notice: this!

    public static void CopyMeToClipboard(this string str)
    {
        #if UNITY_WEBGL && UNITY_EDITOR == false
            CopyToClipboard(str);
        #else
            GUIUtility.systemCopyBuffer = str;
        #endif
    }

}