JavaScript interface in Unity Web builds
Learn about some useful functions you can use in the JavaScript interface.
Read time 3 minutesLast updated 7 days ago
The JavaScript interface in Unity Web builds provides useful functions and variables you can use to configure your web applications. You can call these functions in your JavaScript library code (.jslib files).
Display error, warning, or custom banners
Use the function to display a message banner, or override the function to customize it for your own purposes.
unityShowBanner()Parameter | Description |
|---|---|
| The message you want to display. |
| The type of message you want to display. For example:
|
The following code from the default WebGL template sets the appearance of the error and warning banners. In your custom template, you can override this function to create your own banner types, or customize the way non-critical warnings and error messages display.
function unityShowBanner(msg, type) { var warningBanner = document.querySelector("#unity-warning"); function updateBannerVisibility() { warningBanner.style.display = warningBanner.children.length ? 'block' : 'none'; } var div = document.createElement('div'); div.innerHTML = msg; warningBanner.appendChild(div); if (type == 'error') div.style = 'background: red; padding: 10px;'; else { if (type == 'warning') div.style = 'background: yellow; padding: 10px;'; setTimeout(function() { warningBanner.removeChild(div); updateBannerVisibility(); }, 5000); } updateBannerVisibility(); }
You can call the function in your .jslib file. The following banner permanently displays The message you want to show as an error message:
unityShowBanner('The message you want to show', 'error');
Change WebGL context attributes
Use to customize the creation attributes that the WebGL context is initialized with. You can only change the following attributes:
webglContextAttributespowerPreferencepremultipliedAlphapreserveDrawingBuffer
These options only apply when you use the WebGL rendering API. If you use the WebGPU rendering API, the field is ignored.
webglContextAttributesTo change these attributes, change the configuration object in a custom WebGL template. For example:
script.onload = () => { config['webglContextAttributes'] = { powerPreference: 'high-performance', premultipliedAlpha: false, preserveDrawingBuffer: true, }; createUnityInstance(canvas, config, (progress) => {
The attribute has the following options:
powerPreferenceOption | Description |
|---|---|
| default | Let the user agent decide which GPU configuration is most suitable. This is the default value. |
| high-performance | Prioritizes rendering performance over power consumption. |
| low-power | Prioritizes power saving over rendering performance. |
For more information about each attribute, refer to the Mozilla documentation on HTMLCanvasElement: getContext().
For information about Unity WebGL graphics, refer to WebGL graphics.
Direct your project to your streaming assets
If you need to host your streaming assets on a different CDN, you can adjust the property on the configuration object to point to the full URL where your streaming assets are.
streamingAssetsUrlcreateUnityInstance()var buildUrl = "Build";var config = { dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}", frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}", codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",#if MEMORY_FILENAME memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",#endif#if SYMBOLS_FILENAME symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",#endif streamingAssetsUrl: "https://mygameserver.com/StreamingAssets/", // Add this line to override the default streaming assets location companyName: "{{{ COMPANY_NAME }}}", productName: "{{{ PRODUCT_NAME }}}", productVersion: "{{{ PRODUCT_VERSION }}}",};
Control cache of web pages
Use the function to control which URLs are cached in the . The function accepts the URL as a parameter and returns either , , or . You can also override the function for your own purposes.
cacheControl()UnityCachemust-revalidateimmutableno-storeFor a code example and more information, refer to Cache behavior in Web.