/** * Copyright (c) 2021 Vuplex Inc. All rights reserved. * * Licensed under the Vuplex Commercial Software Library License, you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * https://vuplex.com/commercial-library-license * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using UnityEngine; #if NET_4_6 || NET_STANDARD_2_0 using System.Threading.Tasks; #endif namespace Vuplex.WebView { /// /// `IWebView` is the primary interface for loading and interacting with web content. /// /// /// `WebViewPrefab` takes care of creating one for you and hooking it up to the materials /// in its prefab. If you want to create an `IWebView` outside of the prefab (to connect /// to your own custom GameObject) you can use `Web.CreateWebView()`. /// public interface IWebView { /// /// Indicates that the page has requested to close (i.e. via `window.close()`). /// event EventHandler CloseRequested; /// /// Indicates that a message was logged to the JavaScript console. /// /// /// The 3D WebView packages for Android with Gecko, iOS, and UWP have the following limitations: /// - Messages from iframes aren't captured /// - Messages logged early when the page starts loading may be missed /// event EventHandler ConsoleMessageLogged; /// /// Indicates that an input field was focused or unfocused. This can be used, /// for example, to determine when to show or hide an on-screen keyboard. /// event EventHandler FocusedInputFieldChanged; /// /// Indicates that the page load percentage changed. /// event EventHandler LoadProgressChanged; /// /// Indicates that JavaScript running in the page used the `window.vuplex.postMessage` /// JavaScript API to emit a message to the Unity application. /// /// /// // JavaScript example /// function sendMessageToCSharp() { /// // This object passed to `postMessage()` is automatically serialized as JSON /// // and is emitted via the C# MessageEmitted event. This API mimics the window.postMessage API. /// window.vuplex.postMessage({ type: 'greeting', message: 'Hello from JavaScript!' }); /// } /// /// if (window.vuplex) { /// // The window.vuplex object has already been initialized after page load, /// // so we can go ahead and send the message. /// sendMessageToCSharp(); /// } else { /// // The window.vuplex object hasn't been initialized yet because the page is still /// // loading, so add an event listener to send the message once it's initialized. /// window.addEventListener('vuplexready', sendMessageToCSharp); /// } /// event EventHandler> MessageEmitted; /// /// Indicates that the page failed to load. This can happen, for instance, /// if DNS is unable to resolve the hostname. /// event EventHandler PageLoadFailed; /// /// Indicates that the page's title changed. /// event EventHandler> TitleChanged; /// /// Indicates that the URL of the webview changed, either /// due to user interaction or JavaScript. /// event EventHandler UrlChanged; /// /// Indicates that the rect of the playing video changed. /// /// /// Note that `WebViewPrefab` automatically handles this event for you. /// event EventHandler> VideoRectChanged; /// /// Indicates whether the instance has been disposed via `Dispose()`. /// bool IsDisposed { get; } /// /// Indicates whether the instance has been initialized via `Init()`. /// bool IsInitialized { get; } /// /// JavaScript scripts that are automatically executed in every new /// page that is loaded. This list is empty by default, but the application /// can add scripts. /// List PageLoadScripts { get; } /// /// Indicates the instance's plugin type. /// WebPluginType PluginType { get; } /// /// The webview's resolution in pixels per Unity unit. /// /// float Resolution { get; } /// /// The webview's current size in Unity units. /// Vector2 Size { get; } /// /// The webview's current size in pixels. /// /// Vector2 SizeInPixels { get; } /// /// The texture for the webview's web content. /// /// /// This texture is an "external texture" created with /// `Texture2D.CreateExternalTexture()`. An undocumented characteristic /// of external textures in Unity is that not all `Texture2D` methods work for them. /// For example, `Texture2D.GetRawTextureData()` and `ImageConversion.EncodeToPNG()` /// fail for external textures. To compensate, the `IWebView` interface includes // its own`GetRawTextureData()` and `CaptureScreenshot()` methods to replace them. /// Texture2D Texture { get; } /// /// The current URL. /// string Url { get; } /// /// The texture for the webview's video content. /// Note that iOS uses this separate texture for video. /// Texture2D VideoTexture { get; } /// /// Initializes a newly created webview with the given textures created with /// `Web.CreateMaterial()` and the dimensions in Unity units. /// /// /// Important notes: /// - If you're using `WebViewPrefab`, you don't need to call this method, because it calls it for you. /// - A separate video texture is only used on Android and iOS. /// - A webview's default resolution is 1300px per Unity unit but can be changed with /// `IWebView.SetResolution()`. /// void Init(Texture2D viewportTexture, float width, float height, Texture2D videoTexture); /// /// Like the other `Init()` method, but with video support disabled on Android and iOS. /// void Init(Texture2D viewportTexture, float width, float height); /// /// Makes the webview relinquish focus. /// void Blur(); #if NET_4_6 || NET_STANDARD_2_0 /// /// Checks whether the webview can go back with a call to `GoBack()`. /// Task CanGoBack(); /// /// Checks whether the webview can go forward with a call to `GoForward()`. /// Task CanGoForward(); #endif /// /// Like the other version of `CanGoBack()`, except it uses a callback /// instead of a `Task` in order to be compatible with legacy .NET. /// void CanGoBack(Action callback); /// /// Like the other version of `CanGoForward()`, except it uses a callback /// instead of a `Task` in order to be compatible with legacy .NET. /// void CanGoForward(Action callback); #if NET_4_6 || NET_STANDARD_2_0 /// /// Returns a PNG image of the content visible in the webview. /// /// /// Note that on iOS, screenshots do not include video content, which appears black. /// Task CaptureScreenshot(); #endif /// /// Like the other version of `CaptureScreenshot()`, except it uses a callback /// instead of a `Task` in order to be compatible with legacy .NET. /// void CaptureScreenshot(Action callback); /// /// Clicks at the given point in the webpage, dispatching both a mouse /// down and a mouse up event. /// /// /// The x and y components of the point are values /// between 0 and 1 that are normalized to the width and height, respectively. For example, /// `point.x = x in Unity units / width in Unity units`. /// Like in the browser, the origin is in the upper-left corner, /// the positive direction of the y-axis is down, and the positive /// direction of the x-axis is right. /// void Click(Vector2 point); /// /// Like `Click()` but with an additional option to prevent stealing focus. /// void Click(Vector2 point, bool preventStealingFocus); /// /// Copies the selected text to the clipboard. /// void Copy(); /// /// Copies the selected text to the clipboard and removes it. /// void Cut(); /// /// Disables the webview from rendering to its texture. /// void DisableViewUpdates(); /// /// Destroys the webview, releasing all of its resources. /// /// /// Note that if you're using `WebViewPrefab`, you should call /// `WebViewPrefab.Destroy()` instead. /// void Dispose(); /// /// Re-enables rendering after a call to `DisableViewUpdates()`. /// void EnableViewUpdates(); #if NET_4_6 || NET_STANDARD_2_0 /// /// Executes the given script in the context of the webpage's main frame /// and returns the result. /// /// /// When targeting legacy .NET, this method returns `void` instead of a `Task`. /// Task ExecuteJavaScript(string javaScript); #else /// /// Executes the given script in the context of the webpage's main frame. /// /// /// When targeting legacy .NET, this method returns `void` instead of a `Task`. /// void ExecuteJavaScript(string javaScript); #endif /// /// Executes the given script in the context of the webpage's main frame /// and calls the given callback with the result. /// /// /// This method is functionally equivalent to the version of `ExecuteJavaScript()` /// that returns a `Task`, except it uses a callback instead of a `Task` in order /// to be compatible with legacy .NET. /// void ExecuteJavaScript(string javaScript, Action callback); /// /// Makes the webview take focus. /// void Focus(); #if NET_4_6 || NET_STANDARD_2_0 /// /// A replacement for [`Texture2D.GetRawTextureData()`](https://docs.unity3d.com/ScriptReference/Texture2D.GetRawTextureData.html) /// for IWebView.Texture. /// /// /// Unity's `Texture2D.GetRawTextureData()` method currently does not work for textures created with /// `Texture2D.CreateExternalTexture()`. So, this method serves as a replacement by providing /// the equivalent functionality. You can load the bytes returned by this method into another /// texture using [`Texture2D.LoadRawTextureData()`](https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html). /// Note that on iOS, the texture data excludes video content, which appears black. /// /// /// var textureData = await webView.GetRawTextureData(); /// var texture = new Texture2D( /// (int)webView.SizeInPixels.x, /// (int)webView.SizeInPixels.y, /// TextureFormat.RGBA32, /// false, /// false /// ); /// texture.LoadRawTextureData(textureData); /// texture.Apply(); /// Task GetRawTextureData(); #endif /// /// Like the other version of `GetRawTextureData()`, except it uses a callback /// instead of a `Task` in order to be compatible with legacy .NET. /// void GetRawTextureData(Action callback); /// /// Navigates back to the previous page in the webview's history. /// void GoBack(); /// /// Navigates forward to the next page in the webview's history. /// void GoForward(); /// /// Dispatches a keystroke to the webview. /// /// /// A key can either be a single character representing /// a unicode character (e.g. "A", "b", "?") or a [JavaScript Key value](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) /// (e.g. "ArrowUp", "Enter"). /// void HandleKeyboardInput(string key); /// /// Loads the webpage contained in the given HTML string. /// /// /// /// /// Test Page /// /// /// ///

LoadHtml Example

/// /// /// " /// ); /// ``` /// ]]> void LoadHtml(string html); /// /// Loads the given URL. Supported URL schemes: /// - `http://`, `https://` - loads a remote page over HTTP /// - `streaming-assets://` - loads a local page from StreamingAssets /// (equivalent to `"file://" + Application.streamingAssetsPath + path`) /// - `file://` - some platforms support loading arbitrary file URLs /// void LoadUrl(string url); /// /// Like `LoadUrl(string url)`, but also sends the given HTTP request headers /// when loading the URL. /// void LoadUrl(string url, Dictionary additionalHttpHeaders); /// /// Pastes text from the clipboard. /// void Paste(); /// /// Posts a message that JavaScript within the webview can listen for /// using `window.vuplex.addEventListener('message', function(message) {})`. /// /// /// String that is passed as the data property of the message object. /// void PostMessage(string data); /// /// Reloads the current page. /// void Reload(); /// /// Resizes the webview to the dimensions given in Unity units. /// /// /// Important notes: /// - If you're using `WebViewPrefab`, you should call /// `WebViewPrefab.Resize()` instead. /// - A webview's default resolution is 1300px per Unity unit but can be changed with /// `IWebView.SetResolution()`. /// void Resize(float width, float height); /// /// Scrolls the webview's top-level body by the given delta. /// If you want to scroll a specific section of the page, /// see `Scroll(Vector2 scrollDelta, Vector2 point)` instead. /// /// /// The scroll delta in Unity units. Because the browser's origin /// is in the upper-left corner, the y-axis' positive direction /// is down and the x-axis' positive direction is right. /// void Scroll(Vector2 scrollDelta); /// /// Scrolls by the given delta at the given pointer position. /// /// /// The scroll delta in Unity units. Because the browser's origin /// is in the upper-left corner, the y-axis' positive direction /// is down and the x-axis' positive direction is right. /// /// /// The pointer position at the time of the scroll. The x and y components of are values /// between 0 and 1 that are normalized to the width and height, respectively. For example, /// `point.x = x in Unity units / width in Unity units`. /// void Scroll(Vector2 scrollDelta, Vector2 point); /// /// Selects all text, depending on the page's focused element. /// void SelectAll(); /// /// Sets the webview's resolution in pixels per Unity unit. /// You can change the resolution to make web content appear larger or smaller. /// /// /// The default resolution is 1300 pixels per Unity unit. /// Setting a lower resolution decreases the pixel density, but has the effect /// of making web content appear larger. Setting a higher resolution increases /// the pixel density, but has the effect of making content appear smaller. /// For more information on scaling web content, see /// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content). /// void SetResolution(float pixelsPerUnityUnit); /// /// Zooms into the currently loaded web content. /// /// /// Note that the zoom level gets reset when a new page is loaded. /// void ZoomIn(); /// /// Zooms back out after a previous call to `ZoomIn()`. /// /// /// Note that the zoom level gets reset when a new page is loaded. /// void ZoomOut(); } }