0

I creating WEB GL game. Game menu is shown by the following code:

Input.GetKeyUp(KeyCode.Escape)

It works fine in unity and in the windows build. In the browser, escape pressing is intercepted by browser and the menu is not displayed.

Is it possible to make this code working in WEB GL build? What is the standart "meny button" for WEB GL games?

gman
  • 100,619
  • 31
  • 269
  • 393
user1941407
  • 2,722
  • 4
  • 27
  • 39

3 Answers3

2

It should be working because

From Unity Manual webgl input : By default, Unity WebGL will process all keyboard input send to the page, regardless of whether the WebGL canvas has focus or not.

It could be that you disabled WebGLInput.captureAllKeyboardInput (Which is enabled by default) In that case just enable that and it should work.

Shogunivar
  • 1,237
  • 11
  • 18
1

The Escape Button is used by default for exiting fullscreen mode, maybe that's causing the issue. I would suggest to use the Input Manager ( https://docs.unity3d.com/Manual/class-InputManager.html ) and define a button using Escape as the positive button.

pasotee
  • 338
  • 2
  • 12
1

As @pasotee mentioned, the Escape key could be consumed already. By the Browser, as well as by the Unity Player, as for example the F1 key is used by Chrome to open the Help Tab. Escape is definitely used by the Unity Player to exit its Fullscreen mode.

So the short answer is. No, you can't.
Unity is listening to all key by default, true, but never gets the events when the upper lying application, like the browser, doesn't send it to the Canvas or the executing environment - aka the Unity3d Player doesn't send it down to your code. Or in this case, they should really fix the manual to "[..] all buttons, except Escape and browser related Hotkeys":
https://docs.unity3d.com/Manual/webgl-input.html

The long answer is, you might intercept the 'Escape' key event within JavaScript and pass a function call into the Unity Player like mentioned here:

jQuery

$(document).on('keyup',function(evt) {
    if (evt.keyCode == 27) {
       alert('Esc key pressed.');
    }
});

https://jsfiddle.net/MQHVa/1/

Vanilla JS

var msg = document.getElementById('state-msg');

document.body.addEventListener('keypress', function(e) {
    if(e.key == "Escape"){
        msg.textContent += 'Escape pressed:'
    }
});

https://jsfiddle.net/etcjwaf5/

And in the end you'll need this information, to call Unity from Javascript:

SendMessage('MyGameObject', 'MyFunction');
SendMessage('MyGameObject', 'MyFunction', 5);
SendMessage('MyGameObject', 'MyFunction', 'MyString');

https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

And the very last thing is to have a GameObject in your loaded Scene that provides the function for doing, whatever you desired to be done.
Bare in mind, that if you don't cancel distribution of the Escape key event it will be send to the UnityPlayer which triggers it to exit fullscreen mode! And you might not be able to prevent Unity from consuming this event anyway, so remember WebGLInput.captureAllKeyboardInput member.

mateiasu
  • 76
  • 5