WebGL copy/paste

Hello,

Its critical for my WebGL app to allow users to be able to PASTE information into the unity game, to prevent them having to type a whole bunch of complicated information over and over. But seems unity dont have copy/paste for WebGL.

The only way i can see to do it is to call javascript from C#, javascript would call the prompt(…) function displaying a dialog box allowing the user to paste something into it, then pass that back to c# using sendmessage.

This actually works like a charm, but has the following REALLY bad side effects…

  1. Displaying javascript prompt dialog throws the user out of WebGL full screen mode in the browser.
  2. During the time the prompt dialog is displayed, the whole unity engine is frozen completely.
  3. If the user takes more than a few seconds to paste and dismiss the dialog, multi-player photon disconnects from the server (CRASHING THE GAME) because it cant ping the server while the prompt is being displayed since unity is frozen during that time.

This is a really big problem. Is there any Unity solution yet for copy/paste in WebGL?

Since prompt(…) is a MODAL dialog (stopping everything until the dialog is dismissed), maybe a MODELESS version of prompt will do it, just like MODELESS dialog boxes (like a floating tool box window) in windows programming?

Thanks

There’s a user that’s posted his own solution in another copy/paste thread which judging from the sample alone works quite nicely - no prompt required, supports both copy and paste.

Credited to kou-yeung.

I’m currently using my own solution at work, passing the browser’s native OnPaste event through for pasting and doing something similar for copying but I don’t really have it in a nice all-in-one package for sharing. However I think the above solution should work fine - if not I can take a look at packaging my own solution up.

As for your existing ‘prompt’ solution, there simply isn’t a native non-modal solution to that without some sort of third party library.

Hello,

Thanks so much for your comment. I will certainly look at that solution you posted. One other solution i started working on was putting a small HTML code inside a DIV. This code would have a simple input text box where the user can do a paste, and also an OK button, like this

<div id="dopaste" class="fixedPosition">
  Paste Here: <input type="text" id="paste">

  <button id="button" onclick="SendToUnity()">Ok</button>
</div>

The “fixedposition” class above is css code to force the DIV to appear at the top of the game canvas in the browser.

When the time comes to do a paste, the C# code would call a small javascript function to unhide the DIV at the top of the game canvas in the browser. The user can paste in their text, then when the OK button is pressed call another JavaScript function to get the pasted text from the input field and SendMessage to c#, then rehide the DIV.

Im trying to get all that working, but i dont know where to put the code in the index.html file the WebGL build generates because it seems the whole game code is inside the gameInstance variable, and i dont know javascript and the DOM well enough to know how to put it all together. It works on its own, but i dont know how to get it into the unity build.

Hello, after integrating the package at the link you gave, though it works to a point, i seems to have some problems.

If the Unity game canvas in the browser is zoom in to full screen,then paste dont work anymore, and whatever i type appears in reverse. For example if i type “hello”, it it appears “olleh” in the text box.

You said you have a version that works, is it possible for you to post that? I would really appreciate it!

1 Like

in chrome and opera you can do this:

but it’s not yet supportet in other browsers afaik.

I don’t know if this works well but an attempt at a solution is to use the browser’s copy and paste events. In paste pass that into Unity and append to the current InputField. On copy ask the inputfield to copy then pass unity’s clipboard back to the browser

Here’s some code and a working example

https://github.com/greggman/unity-webgl-copy-and-paste