Training
Learning path
HoloLens 2 fundamentals: develop mixed reality applications - Training
Learn about the best practices for developing Azure-enabled mixed reality applications with the Mixed Reality Toolkit.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The Microsoft Mixed Reality WebView plugin for Unity enables the integration of WebView2 functionality into your HoloLens 2 app. This WebView plugin for Unity simplifies the integration of WebView2 functionality into your HoloLens 2 app by wrapping the WebView2 control, automatically handling rendering, and automatically directing input to the WebView2 control.
This plugin also manages interop between Unity and WebView2, enabling communication between JavaScript and Unity via messages and events.
This plugin exposes a subset of the functionality that's available via CoreWebView2.
WebView2 on HoloLens 2 and the WebView plugin for Unity are both in Preview and are subject to change before general availability. The WebView2 Preview is available in the Insider Preview for Microsoft HoloLens. To access this preview, you must be enrolled in the Windows Insider Program; see Start receiving Insider builds in Insider preview for Microsoft HoloLens.
WebView2 and the WebView plugin are only supported on HoloLens 2 devices running the Windows 11 update. For more information, see Update HoloLens 2.
For information on getting started with the WebView plugin for Unity, see Get started with WebView2 in HoloLens 2 Unity apps.
The main interface for the WebView plugin for Unity.
public interface IWebView
{
event WebView_OnNavigated Navigated;
event WebView_OnCloseRequested WindowCloseRequested;
GameObject GameObject { get; }
Texture2D Texture { get; }
int Width { get; set; }
int Height { get; set; }
Uri Page { get; }
Task OnceCreated { get; }
void Resize(int width, int height);
void Load(Uri url);
void Dispose();
}
public delegate void WebView_OnNavigated(string path);
public delegate void WebView_OnCanGoForwardUpdated(bool value);
Event triggered when CoreWebView2.SourceChanged Event is raised by WebView.
webView.Navigated += OnNavigated;
private void OnNavigated(string uri)
{
UrlField.text = uri;
}
Event triggered when CoreWebView2.WindowCloseRequested Event is raised by WebView.
webView.WindowCloseRequested += OnWindowCloseRequested;
private void OnWindowCloseRequested()
{
Destroy(GameObject);
}
The top-level Unity GameObject entity that represents the WebView plugin in the scene. Readonly.
The 2D Unity Texture2D
object that the WebView content is rendered to. Readonly
The width of the WebView texture and the WebView control. Note that the rendered dimensions of the IWebView
instance in the Unity scene are controlled by the GameObject
.
The height of the WebView texture and the WebView control. Note that the rendered dimensions of the IWebView
instance in the Unity scene are controlled by the GameObject
.
The URI currently loaded or being navigated to by the WebView control. Readonly
Task executes when the WebView control has been fully instantiated and ready to use. Readonly
webView.OnceCreated.ContinueWith((task) => {
// Finish setting up plugin.
webview.Navigated += OnNavigated;
webview.WindowCloseRequested += OnWindowCloseRequested;
Load(initialURL);
}, TaskScheduler.FromCurrentSynchronizationContext());
Changes the size of the WebView2 control and the Texture
. For details, see the underlying CoreWebView2Controller.Bounds Property.
Note that the rendered dimensions of the IWebView
instance in the Unity scene are controlled by the GameObject
.
Resize(600, 400);
Navigates to the specified URI. For details, see the underlying CoreWebView2.Navigate Method.
Load(new Uri("https://www.microsoft.com"));
Clears resources such as memory, handles, and callbacks that are related to an IWebView
instance.
Invoke this method when you are done with a particular IWebView
instance to ensure internal memory is properly freed. After Dispose
is called, the IWebView
instance should be considered invalid.
void OnDestroy()
{
webView.Dispose();
}
Supports mouse and pointer input for the WebView plugin for Unity.
public interface IWithMouseEvents
{
void MouseEvent(WebViewMouseEventData mouseEvent);
}
Propagates a WebViewMouseEventData
event to the WebView control. Depending on internal logic, the event results in calling either the CoreWebView2Controller.SendMouseInput Method or the CoreWebView2Controller.SendPointerInput Method.
public void OnPointerDown(PointerEventData eventData)
{
IWithMouseEvents mouseEventsWebView = webView as IWithMouseEvents;
// Call hypothetical function which converts the event's x, y into the WebView2's coordinate space.
var hitCoord = ConvertToWebViewSpace(eventData.position.x, eventData.position.y);
WebViewMouseEventData mouseEvent = new WebViewMouseEventData
{
X = hitCoord.x,
Y = hitCoord.y,
Type = PointerEvent.PointerDown,
Button = PointerButton.Left,
TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
};
mouseEventsWebView.MouseEvent(mouseEvent);
}
Interface for interop communication between Unity code and hosted WebView code.
To learn more about interop in WebView2, see Interop of native-side and web-side code.
public interface IWithPostMessage : IWebView
{
event WebView_OnPostMessage MessageReceived;
void PostMessage(string message, bool isJSON = false);
}
public delegate void WebView_OnPostMessage(string message);
Triggered when a new JavaScript message is received from the WebView control. For details, see the underlying CoreWebView2.WebMessageReceived Event.
(webView as IWithPostMessage).MessageReceived += OnMessageReceived;
void OnMessageReceived(string message)
{
Debug.Log(message);
}
Sends a JavaScript message to the hosted content in the WebView control. Depending on the isJSON
parameter, this will either result in calling the CoreWebView2.PostWebMessageAsString Method or the CoreWebView2.PostWebMessageAsJson Method.
var msg = new MyMessage("updateText", "Updated from Unity!");
(webView as IWithPostMessage).PostMessage(JsonUtility.ToJson(msg), true);
Handles functionality related to browser history, such as navigating to a previous page.
public interface IWithBrowserHistory : IWebView
{
event WebView_OnCanGoForwardUpdated CanGoForwardUpdated;
event WebView_OnCanGoBackUpdated CanGoBackUpdated;
void GoBack();
void GoForward();
}
public delegate void WebView_OnCanGoBackUpdated(bool value);
public delegate void WebView_OnCloseRequested();
Triggered when a navigation occurs. The event delegate will provide a true
value if CoreWebView2.CanGoForward Property is true
.
(webView as IWithBrowserHistory).CanGoBackUpdated += OnCanGoBack;
void OnCanGoBack(bool value)
{
BackButton.enabled = value;
}
Triggered when a navigation occurs. The event delegate will provide a true
value if CoreWebView2.CanGoBack Property is true
.
(webView as IWithBrowserHistory).CanGoForwardUpdated += OnCanGoForward;
void OnCanGoForward(bool value)
{
ForwardButton.enabled = value;
}
Navigates to the previous page. For details, see the underlying CoreWebView2.GoBack Method.
(webView as IWithBrowserHistory).GoBack();
Navigates to the next page. For details, see the underlying CoreWebView2.GoForward Method.
(webView as IWithBrowserHistory).GoForward();
Training
Learning path
HoloLens 2 fundamentals: develop mixed reality applications - Training
Learn about the best practices for developing Azure-enabled mixed reality applications with the Mixed Reality Toolkit.