Version: 1.7
LanguageEnglish
  • C#

Webview

class in UnityEngine.OpenHarmony

/

Implemented in:UnityEngine.OpenHarmonyJSModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Provides OpenHarmony Webview functionality encapsulation, supporting creation, management and operation of Webview instances.

using System.Collections;
using UnityEngine;
using UnityEngine.OpenHarmony;

public class ExampleWebview : MonoBehaviour { void Start() { StartCoroutine(WebviewExample()); }

IEnumerator WebviewExample() { //WaitForSeconds are for display only. The script still works without them. Webview webview1 = new Webview(); Webview webview2 = new Webview(); webview1.OnHttpError += PrintMsg; webview2.OnHttpError += PrintMsg;

webview1.SetMargins(Screen.width / 16, Screen.height / 16, Screen.width / 16, Screen.height / 2);

webview2.Position = new Vector2Int(Screen.width / 16, Screen.height / 16 + Screen.height / 2); webview2.Size = new Vector2Int(Screen.width * 7 / 8, Screen.height * 3 / 8);

webview1.Url = "https://docs.unity.cn/cn/tuanjiemanual/Manual/"; webview2.Url = "https://unity.cn/";

yield return new WaitForSeconds(2);

webview1.EvaluateJS("console.log('Hello from Tuanjie')"); webview2.Reload();

yield return new WaitForSeconds(2);

webview1.Visible = false; webview2.LoadHTMLString("<html><body bgcolor=\"white\">Hello<pre>world</pre></body></html>", "");

yield return new WaitForSeconds(2);

webview1.Delete(); webview2.Delete();

} void PrintMsg(string msg) { Debug.LogError(msg); } }

Properties

IndexGets the unique identifier of the Webview.
IsValidGets whether the Webview is valid (Initialized and not deleted).
PositionGets or sets the position of the Webview.
SizeGets or sets the size of the Webview.
UrlGets or sets the current URL loaded in the Webview.
VisibleGets or sets the visibility of the Webview.

Constructors

WebviewCreate a Webview instance.

Public Methods

DeleteDeletes the current Webview instance.
DeleteJavaScriptRegisterDelete the JavaScript object registered with the specified name via registerJavaScriptProxy. After deletion, call the Webview.Reload().
EvaluateJSExecutes JavaScript code.
GoBackNavigates back to the previous page.
GoForwardNavigates forward to the next page.
LoadDataLoads data content.
LoadHTMLStringLoads HTML string content.
RegisterJavaScriptProxyRegister JavaScript object so that it's able to be called in html. After registration, call the Webview.Reload() for the changes to take effect.
ReloadReloads the current page.
SetMarginsSets the margins of the Webview.
SetWebviewAttributesSet new appearance attributes for webview.
StopLoadingStops loading the current page.

Events

OnConsoleTriggered when JavaScript console print a message.
OnErrorTriggered when an error occurs in the Webview.
OnHttpErrorTriggered when an HTTP error occurs in the Webview.