Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. Tenebrous

    Tenebrous

    Volunteer Moderator

    Joined:
    Jul 25, 2011
    Posts:
    102
    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

    sandboxr

    Joined:
    Aug 31, 2012
    Posts:
    2
    You sir, are my hero!