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.

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!