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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Returning focus to webGL

Discussion in 'WebGL' started by arumiat, Jul 4, 2015.

  1. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    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?
     
  2. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,685
    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.
     
    arumiat likes this.
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    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.
     
    DudeGuy and arumiat like this.
  4. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    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.
     
  5. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Yes, as I wrote, this is a known issue, which will be fixed in 5.2. The workaround I mentioned should work, though?
     
  6. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    I will give it a go and let you know, cheers
     
  7. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    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
     
  8. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Well, i never said call it every frame. If you want to workaround getting focus on mouse clicks, you could for instance just do:

    if (Input.GetMouseButtonDown(0))
    Application.ExternalEval("window.focus();");
     
    arumiat likes this.
  9. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    His response:

    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.


    I'll post back the results. Cheers, T
     
    yuliyF likes this.
  10. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    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..
     
  11. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    So this definitely hasn't worked for us. Is there something we're doing wrong Jonas?
     
  12. DudeGuy

    DudeGuy

    Joined:
    Jul 17, 2014
    Posts:
    19
    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:

    Code (Javascript):
    1. var canvas = document.getElementById("canvas");
    2. canvas.addEventListener("mousedown", onMouseDown, false);
    3.  
    4. function onMouseDown(event)
    5. {
    6.     window.focus();
    7. }
    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!
     
    arumiat likes this.
  13. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    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
     
  14. Tenebrous

    Tenebrous

    Volunteer Moderator Moderator

    Joined:
    Jul 25, 2011
    Posts:
    102
  15. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    Looks to work real nice, thanks buddy!
     
  16. wbl1

    wbl1

    Joined:
    Apr 22, 2009
    Posts:
    159
    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?
     
  17. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    5.3.5f1 seems to work normally for me...
     
  18. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    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.
     
  19. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Does this method work. Surely if the window doesn't have the focus it won't get the mouse down event in the first place?