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. Dismiss Notice

Coding linebreaks in text put on clipboard in WebGL

Discussion in 'WebGL' started by garethdenyer, Sep 13, 2020.

  1. garethdenyer

    garethdenyer

    Joined:
    Aug 16, 2020
    Posts:
    6
    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 :)
     
  2. garethdenyer

    garethdenyer

    Joined:
    Aug 16, 2020
    Posts:
    6
    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.
     
    momorobin likes this.
  3. andrlz

    andrlz

    Joined:
    Mar 15, 2021
    Posts:
    6
    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:

    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2. using UnityEngine;
    3.  
    4. // this is a great example of a string extension class
    5. public static class ClipboardCopy
    6. {
    7.     // Using a JavaScript plugin
    8.     [DllImport("__Internal")]
    9.     public static extern void CopyToClipboard(string str); // notice: this!
    10.  
    11.     public static void CopyMeToClipboard(this string str)
    12.     {
    13.         #if UNITY_WEBGL && UNITY_EDITOR == false
    14.             CopyToClipboard(str);
    15.         #else
    16.             GUIUtility.systemCopyBuffer = str;
    17.         #endif
    18.     }
    19.  
    20. }