If(using VSCODE) { // Open this README in Preview mode || press Ctrl + Shift + V in Windows to view embedded images. }
When working on a Unity WebGL project, I ran into a frustrating issue:
Unity's built-in TMP_InputField components do not play well with mobile browsers — especially when the on-screen keyboard appears.
- The TMP input field gets obscured by the soft keyboard, especially in landscape mode
- It’s hard or impossible for users to see what they’re typing
- Behavior is inconsistent across devices, browsers, and OSes
- Unity provides limited control over soft keyboard interactions in WebGL builds
To fix this, I created a custom HTML input overlay that appears when a TMP field is selected.
This overlay:
- Renders a native HTML
<input>on top of the Unity canvas - Automatically focuses and adjusts on mobile devices
- Syncs the input back to Unity through
SendMessage
This approach results in a smoother, more intuitive typing experience for WebGL users — especially on touch devices(Landscape mode).
This setup was built and tested in:
Unity 6.0.51f1 (LTS)
Unity allows you to use a custom HTML template for WebGL builds.
To gain full control over the HTML, CSS, and JavaScript behavior during runtime, I duplicated Unity’s default template and modified it.
-
Navigate to the default template location on your system: /PlaybackEngines/WebGLSupport/BuildTools/WebGLTemplates/Default
-
Copy the entire
Defaultfolder into your project under: Assets/WebGLTemplates/CustomTemplate
-
Rename the folder if you’d like (e.g.,
CustomTemplate,KeyBInputTemplate, etc.) -
In Unity, go to:
Project Settings → Player → Resolution and Presentation → WebGL Template
and select your custom template from the dropdown.
These two scripts work together to connect Unity’s TMP_InputField components to the HTML input system.
-
Acts as the central controller
-
Sends calls to JavaScript when a TMP field is selected
-
Receives the final typed value back from JavaScript
-
Keeps track of all active input fields by unique IDs
- Attach to each TMP input field which you want to communicate over html.
- Handles showing the input via
InputBridge - Updates the field text when HTML input is submitted
- Registers its field with a unique
inputId
Once the WebGL template is set up, the main logic for showing and handling the native HTML <input> lives in two places:
This file handles the UI and behavior of the native HTML input overlay.
- When Unity requests input, the
ShowHtmlInput(...)JavaScript function is triggered - The Unity canvas is hidden, and a styled HTML
<input>appears - The user types directly into the HTML input (fully mobile/browser native)
- Once finished (Enter or blur), the input is hidden and value sent back to Unity using:
unityInstance.SendMessage("InputBridge", "ReturnFromHtmlInput", inputId + "|" + value);
make sure that the .jslib file stays in same hierarchy Assets>Plugins>WebGL> .jslib to prevent any build errors or manually link it. 5. The .jslib file defines a function that Unity can call from C#. 6. It receives data from C# (via [DllImport("__Internal")]) 7. Converts it from pointers to strings 8. Then calls the browser-side ShowHtmlInput(...) function.






