Search Unity

  1. Dismiss Notice
  2. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  3. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  4. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

WebGL input not working when in an iframe (e.g. Newgrounds) - FIXED

Discussion in 'WebGL' started by Tenebrous, Jul 23, 2015.

  1. Tenebrous


    Volunteer Moderator Moderator

    Jul 25, 2011
    I struggled a bit today getting keyboard input working properly in Unity WebGL on Newgrounds, but finally have it working.

    There's a couple of issues which might trip you up here.

    Firstly, Unity's Input.inputString doesn't seem to work at all at the moment, so if you're using that you'll have to scan the keys using Input.GetKeyXXX.

    Secondly, since NG hosts the game in an iframe, there's an issue with not having the focus set so that the game can receive key input.

    You can fix this by changing index.html and adding the following to the <canvas> element:

    Code (javascript):
    1. onclick="window.focus()"
    And also by adding this <script> block inside <body>:

    Code (javascript):
    1. <script>
    2.     window.onload = function() { window.focus(); }
    3. </script>
    Hope this helps!
  2. sandboxr


    Aug 31, 2012
    You sir, are my hero!