### Enable WKWebView Initialization (iOS) Source: https://github.com/gree/unity-webview/blob/master/README.md Example of initializing WebViewObject with 'enableWKWebView: true' for iOS. ```csharp webViewObject = (new GameObject("WebViewObject")).AddComponent(); webViewObject.Init( ..., enableWKWebView: true); ``` -------------------------------- ### Manual NuGet Restore Source: https://github.com/gree/unity-webview/blob/master/plugins/Windows/README.md If you have the NuGet CLI installed, open Command Prompt or Developer PowerShell, navigate to plugins/Windows, and run: ```cmd nuget install Microsoft.Web.WebView2 -Version 1.0.3800.47 -OutputDirectory packages ``` -------------------------------- ### Unity Web Player Initialization Script Source: https://github.com/gree/unity-webview/blob/master/dist/package-nofragment/Assets/WebPlayerTemplates/unity-webview/index.html This script initializes the Unity Web Player object and handles progress updates and installation prompts. ```javascript var unityObjectUrl = "%UNITY_UNITYOBJECT_URL%"; if (document.location.protocol == 'https:') unityObjectUrl = unityObjectUrl.replace("http://", "https://ssl-"); document.write('
``` -------------------------------- ### WebViewPlugin.cpp - Keyboard Input Handling Source: https://github.com/gree/unity-webview/blob/master/plugins/Windows/PlanDocument/unity_webview_windows_click_window_shrink_fix_plan_cht.md Original code in WebViewPlugin.cpp that unconditionally calls SetFocus(target) during keyboard input handling, which can lead to the main application window losing focus. ```cpp SetFocus(target); ``` -------------------------------- ### Copy WebGL Template Data Source: https://github.com/gree/unity-webview/blob/master/README.md Command to copy the default WebGL template data to the unity-webview directory for use in Unity projects. ```bash cp -a /Applications/Unity/Hub/Editor/2018.4.13f1/PlaybackEngines/WebGLSupport/BuildTools/WebGLTemplates/Default/TemplateData Assets/WebGLTemplates/unity-webview ``` -------------------------------- ### Microphone Access Runtime Source: https://github.com/gree/unity-webview/blob/master/README.md C# code to call on runtime for microphone access. ```csharp webViewObject.SetMicrophoneAccess(true); ``` -------------------------------- ### Microphone Permissions Manifest Source: https://github.com/gree/unity-webview/blob/master/README.md XML snippet to add to AndroidManifest.xml for microphone access. ```xml ``` -------------------------------- ### Unity WebGL Player HTML and JavaScript Source: https://github.com/gree/unity-webview/blob/master/dist/package-nofragment/Assets/WebGLTemplates/unity-webview-2020/index.html This snippet includes the HTML structure and JavaScript code required to load and run a Unity WebGL build. It configures the Unity instance, handles loading progress, and manages fullscreen functionality. ```html Unity WebGL Player | {{{ PRODUCT_NAME }}}
``` -------------------------------- ### Camera Permissions Manifest Source: https://github.com/gree/unity-webview/blob/master/README.md XML snippet to add to AndroidManifest.xml for camera access. ```xml ``` -------------------------------- ### Unity WebGL Player Configuration Source: https://github.com/gree/unity-webview/blob/master/dist/package/Assets/WebGLTemplates/unity-webview-2020/index.html This JavaScript code configures the Unity WebGL player, setting up build URLs, loader paths, and various configuration options like data, framework, and code URLs. It also handles device detection for mobile, canvas styling, background images, and fullscreen functionality. ```javascript var buildUrl = "Build"; var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}"; 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: "StreamingAssets", companyName: "{{{ COMPANY_NAME }}}", productName: "{{{ PRODUCT_NAME }}}", productVersion: "{{{ PRODUCT_VERSION }}}", }; var container = document.querySelector("#unity-container"); var canvas = document.querySelector("#unity-canvas"); var loadingBar = document.querySelector("#unity-loading-bar"); var progressBarFull = document.querySelector("#unity-progress-bar-full"); var fullscreenButton = document.querySelector("#unity-fullscreen-button"); var width0 = "{{{ WIDTH }}}px"; var height0 = "{{{ HEIGHT }}}px"; if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) { container.className = "unity-mobile"; config.devicePixelRatio = 1; } else { canvas.style.width = "{{{ WIDTH }}}px"; canvas.style.height = "{{{ HEIGHT }}}px"; } #if BACKGROUND_FILENAME canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover"; #endif loadingBar.style.display = "block"; document.addEventListener( 'fullscreenchange', function() { var p = document.getElementById('unity-container'); var c = document.getElementById('unity-canvas'); if (document.fullscreenElement) { width0 = c.style.width; height0 = c.style.height; setTimeout( function() { c.style.width = getComputedStyle(p).width; c.style.height = getComputedStyle(p).height; }, 250); } else { c.style.width = width0; c.style.height = height0; } }); var script = document.createElement("script"); script.src = loaderUrl; script.onload = () => { createUnityInstance(canvas, config, (progress) => { progressBarFull.style.width = 100 * progress + "%"; }).then((unityInstance) => { window.unityInstance = unityInstance; loadingBar.style.display = "none"; fullscreenButton.onclick = () => { var p = document.getElementById('unity-container'); var c = document.getElementById('unity-canvas'); c.requestFullscreen = () => { p.requestFullscreen(); }; unityInstance.SetFullscreen(1); }; }).catch((message) => { alert(message); }); }; document.body.appendChild(script); ``` -------------------------------- ### WebViewPlugin.cpp - WM_WEBVIEW_LOAD_URL Handler Source: https://github.com/gree/unity-webview/blob/master/plugins/Windows/PlanDocument/windows_webview2_navigation_capture_fix_plan_cht.md Handles URL loading requests. It stops the current navigation, clears the capture flag, and defers the actual navigation to the next message loop iteration to prevent race conditions. It differentiates between initial load requests (wParam == 0) and deferred navigation (wParam == 1). ```cpp case WM_WEBVIEW_LOAD_URL: { LPCWSTR url = (LPCWSTR)lParam; if (wParam == 0) { // Stop current navigation, clear capture flag, and post a deferred navigation message if (inst && inst->webview) { inst->webview->Stop(); inst->captureInProgress = false; PostMessage(hwnd, WM_WEBVIEW_LOAD_URL, 1, (LPARAM)url); // url is now owned by the deferred message, do not delete here } else { delete[] url; } } else // wParam == 1, deferred navigation { if (inst && inst->webview) { inst->webview->Navigate(url); } delete[] url; } return 0; } ``` -------------------------------- ### WebViewPlugin.cpp - WM_WEBVIEW_LOAD_HTML Handler Source: https://github.com/gree/unity-webview/blob/master/plugins/Windows/PlanDocument/windows_webview2_navigation_capture_fix_plan_cht.md Handles HTML content loading requests. Before navigating to the new HTML content using NavigateToString, it stops any ongoing navigation and clears the capture flag, similar to the HTTP loading process. ```cpp case WM_WEBVIEW_LOAD_HTML: { // ... other code ... if (inst && inst->webview) { inst->webview->Stop(); // Stop any ongoing navigation inst->captureInProgress = false; // Clear capture flag inst->webview->NavigateToString(html); } // ... other code ... return 0; } ``` -------------------------------- ### macOS App Transport Security Configuration (Info.plist) Source: https://github.com/gree/unity-webview/blob/master/README.md This snippet shows how to modify the Info.plist file to allow arbitrary loads, which is necessary for HTTP URLs in the Unity Mac Editor. ```diff --- Info.plist~ 2016-04-11 18:29:25.000000000 +0900 +++ Info.plist 2016-04-15 16:17:28.000000000 +0900 @@ -57,5 +57,10 @@ EditorApplicationPrincipalClass UnityBuildNumber b902ad490cea + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + ``` -------------------------------- ### macOS App Transport Security Configuration (Terminal Command) Source: https://github.com/gree/unity-webview/blob/master/README.md An alternative terminal command to modify the Info.plist for App Transport Security settings. ```bash /usr/libexec/PlistBuddy -c "Add NSAppTransportSecurity:NSAllowsArbitraryLoads bool true" /Applications/Unity/Unity.app/Contents/Info.plist ``` -------------------------------- ### WebViewPlugin.cpp - NavigationCompleted Callback Source: https://github.com/gree/unity-webview/blob/master/plugins/Windows/PlanDocument/windows_webview2_navigation_capture_fix_plan_cht.md Clears the 'captureInProgress' flag in the NavigationCompleted callback to ensure the next frame's capture can be posted, resolving issues where the Unity view might display an old frame. ```cpp void WebViewPlugin::NavigationCompleted(ICoreWebView2NavigationCompletedEventArgs* args) { // ... other code ... inst->captureInProgress = false; // ... other code ... } ```