Are any of you having an issue returning focus to webGL once you click on another part of your browser? Once I click away to scroll the browser or something, currently I’m having to click the fullscreen button and then exit fullscreen to get the focus back on the game. Sometimes I can get focus back by continually clicking around on the screen but it’s a hit and miss experience…
Still on 5.0.2f1
T
EDIT: Somehow got double posted, mods can you delete this one?
I’m pretty sure it has something to do with how you run your Cursor Lock, Cursor Hide Script. I had a similar problem and it was solved by putting the Cursor Lock, Cursor Hide in Update. Don’t keep it in the Awake Function Only otherwise it will only run once and never detect if the mouse is back in the screen or not if you decide to ESC and browse away then come back to the game.
Are you using iframes? We found that there is an issue where an iframe will not get focus when clicking on the WebGL content (which will be fixed in 5.2). You can try calling Application.ExternalEval(“window.focus();”); in your code to enforce focus on your iframe.
Yes, we are using iframes. The focus is initially there, it’s just that when you click away elsewhere on the page & then come back sometimes the focus will not return.
I asked my developer about implementing your method Jonas but his response was this
If I use proposed function then WebGL window will gain focus every frame and it will not be possible to switch it correctly to any other control, e.g. text edit, scroll bar, etc.
So not sure it’s a good solution for me… Can you think of anything else? Essentially I just want it to work like the webplayer in terms of its functionality
The question is whether or not Unity WebGL window will receive any input messages like GetMouseButtonDown(0) if it doesn’t have focus. Also if it works even if Unity window is inactive then it means Unity will switch focus to its window even if user clicks anywhere else on the page. It is necessary to check where the mouse cursor is when the button is clicked. I can experiment with that but can’t promise that it will work correctly.
So we just tried to implement this but: Unfortunately, when application window becomes inactive it doesn’t recognise any mouse button input until I click to the empty space of the iframe.
I’m suprised noone else seems to be having this issue? Using the fullscreen button actually returns focus to the game, I’m trying to think of having some sort of ‘refocus’ button that might do this, but without making fullscreen…
Hi arumiat. I recently ran into a similar situation that you’re experiencing. I was able to get around the problem by using the window.focus() thing Jonas mentioned, but in a slightly different way.
I wrote some simple javascript to detect mousedown events on the ‘canvas’ element. For example:
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", onMouseDown, false);
function onMouseDown(event)
{
window.focus();
}
Now, whenever a click occurs anywhere on the canvas portion of your page, it regains focus immediately. This fixes some iframe focus issues I’ve been experiencing. I hope it works for you or others as well!
I can’t remember if there is an id of ‘canvas’ on the html element by default, but feel free to get that element any way you know how if it doesn’t.
One other thing, I do recommend updating to at least 5.1.1f1. It fixed so many issues over the 5.0.x versions for me.
As usual, thanks for helping with this stuff Jonas!
Thanks for sharing DG, hopefully it will fix this issue for us. I also recently started using 5.1.2, fingers crossed I have the same positive experience!
T
I am having similar iframe/focus issues in 5.3.5f1 … is this issue still around or could it be that I have some settings in the build wrong or something?
Still seeing this issue in 5.3.7 but it’s a bit more intermittent and only in Firefox. The one sure fire way to trigger it is when we call a javascript function in the page to save an image. That spawns a dialog box, once that is closed getting focus back to the WebGL window on a Mac requires maximising and minimising the window or using the Page info option… oddly although that opens another dialogue box, returning from it cures the issue.