0

Displaying a Javascript dialog box in browser from Unity C# causes photon networking to disconnect and crash.

My Unity WebGL game running in a browser needs the user to PAST in some information.

The browser doesnt allow Unity to access the past buffer directly from Unity, so i wrote some JavaScript code to display a PROMPT dialog box so the user can paste in the information. This JavaScript code is called from Unity C# code.

The problem is my game is also using photon networking that constantly needs to ping the server. When my C# code calls the JavaScript code and the dialog box is displayed, the unity engine freeze during the duration of time the JavaScript PROMPT dialog box is being displayed, this freeze prevents photon from pinging the server, so if the user takes more than a few seconds to do the paste and dismiss the dialog box, photon times out and disconnects since it cant ping the server and the game crashes.

Does anybody know why Unity freeze while javascript showns the dialog box? is there anyway around it?

here is my JavaScript code

function DoJavaScriptPaste()
{
    var retVal = prompt("Paste Here", "Paste Here");
    if (retVal==null) {
       retVal="";
    }
    var buffer = _malloc(lengthBytesUTF8(retVal) + 1);
    writeStringToMemory(retVal, buffer);
    return buffer;
};

Unity calls the Javascript code like this from C#

[DllImport("__Internal")]
private static extern string DoJavaScriptPaste();

public void DoPaste()
{
    string pastedInfo=DoJavaScriptPaste();
    text.text = pastedInfo;
 }

I even tried calling DoPaste from a C# Coroutine, but Unity Engine still freeze...

gman
  • 100,619
  • 31
  • 269
  • 393
Programmer2
  • 69
  • 11
  • There is no way around this. You'll have to use custom dialogs. – Tim VN May 02 '19 at 15:15
  • Custom dialogs? you mean custom Javascript dialogs? How to do it? – Programmer2 May 02 '19 at 15:38
  • Well, the standard dialogs block the execution. I'm not sure if you're able to use bootbox for example in Unity as I'm not familiar with Unity as much as I am with Javascript. http://bootboxjs.com/ – Tim VN May 02 '19 at 16:27

1 Answers1

0

The problem with Javascript prompts is that it blocks the entire navigator.

If I had to guess, Unity uses either a javascript timeout or a requestanimationframe or something in order to keep Unity ticking over.

When the prompt is active, timeouts or requestanimationframe callbacks are entirely blocked.

As mentioned by Tim VN, the only solution would be to create a custom input elements. You could display these above the Unity canvas. Or Maybe you could use something like a modal dialog ?

If it was your intention to pause Unity while entering the data, be sure to add this line before you open the modal :

Time.timeScale = 0;
kevernicus
  • 344
  • 1
  • 5
  • Thanks for the comment, the engine pausing during the dialog is the source of the problem, so i dont want to pause it – Programmer2 May 02 '19 at 15:45
  • Setting the timeScale to 0 stops time advancing in your scene, but your networking will still continue to run as usual. You will notice that your 2D interfaces also continue working. – kevernicus May 02 '19 at 15:49