How to reference a webview in a script?

In order to call the instance methods for a WebViewPrefab, CanvasWebViewPrefab, or IWebView, your script must first get a reference to the WebViewPrefab or CanvasWebViewPrefab instance in your scene. If you created the prefab by dragging it into the scene via the editor, then there are a couple approaches for this:

Reference via a public field

The first approach is to add a public field for the WebViewPrefab to your script and then assign its value in the editor's inspector tab. Here's an example:

using UnityEngine;
using Vuplex.WebView;

class Example1 : MonoBehaviour {

    // TODO: Set this field via the editor's inspector tab.
    public WebViewPrefab webViewPrefab;

    async void Start() {
        // Wait for the prefab to initialize before accessing WebViewPrefab.WebView:
        // https://developer.vuplex.com/webview/WebViewPrefab#WaitUntilInitialized
        await webViewPrefab.WaitUntilInitialized();

        // Now you can use the APIs on the WebViewPrefab or its WebView:
        webViewPrefab.WebView.LoadProgressChanged += (eventArgs, sender) => {
            Debug.Log($"LoadProgressChanged: {eventArgs.Type}, {eventArgs.Progress}");
        };
    }
}

Reference via Find()

Another option is to programmatically find a reference to your WebViewPrefab using a method like GameObject.Find(). Here's an example:

using UnityEngine;
using Vuplex.WebView;

class Example2 : MonoBehaviour {

    async void Start() {
        // TODO: Change "WebViewPrefab" to the name you assigned to the WebViewPrefab object in the editor.
        var webViewPrefab = GameObject.Find("WebViewPrefab").GetComponent<WebViewPrefab>();

        // Wait for the prefab to initialize before accessing WebViewPrefab.WebView:
        // https://developer.vuplex.com/webview/WebViewPrefab#WaitUntilInitialized
        await webViewPrefab.WaitUntilInitialized();

        // Now you can use the APIs on the WebViewPrefab or its WebView:
        webViewPrefab.WebView.LoadProgressChanged += (eventArgs, sender) => {
            Debug.Log($"LoadProgressChanged: {eventArgs.Type}, {eventArgs.Progress}");
        };
    }
}

Note that the Keyboard prefab internally uses a WebViewPrefab, so if you instead use FindObjectOfType<WebViewPrefab>(), it may actually return the WebViewPrefab for a Keyboard in your scene.