插件:UniWebVew之一

1、Code
···C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WebViewController : MonoBehaviour {
[SerializeField] Text txtTitle = null;
[SerializeField] Button btnTitle = null;
[SerializeField] Button btnBack = null;
[SerializeField] Button btnForward = null;
[SerializeField] Button btnDone = null;
[SerializeField] RectTransform rtPage = null;

UniWebView webView;

List<string> urlList = new List<string>();
int currentIndex = -1;

void Awake()
{
    btnTitle.onClick.AddListener(OnClickRefresh);
    btnBack.onClick.AddListener(OnClickBack);
    btnForward.onClick.AddListener(OnClickForward);
    btnDone.onClick.AddListener(OnClickDone);

}

private void WebView_OnPageStarted(UniWebView view, string url)
{
    Debug.Log("WebView_OnPageStarted:" + view.Url);
    Debug.Log(view.CanGoBack + "  " + view.CanGoForward);
    btnBack.interactable = view.CanGoBack;
    btnForward.interactable = view.CanGoForward;
}

private void WebView_OnPageFinished(UniWebView view, int statusCode, string url)
{
    Debug.Log("WebView_OnPageStarted: " + view.Url);
    Debug.Log(view.CanGoBack + "  " + view.CanGoForward);
    btnBack.interactable = view.CanGoBack;
    btnForward.interactable = view.CanGoForward;
    txtTitle.text = view.Url;


}

void OnClickRefresh()
{
    webView.Reload();
}

void OnClickBack()
{
    btnBack.interactable = webView.CanGoBack;
    if (webView.CanGoBack)
    {
        webView.GoBack();
    }
}

void OnClickForward()
{
    btnForward.interactable = webView.CanGoForward;
    if (webView.CanGoForward)
    {
        webView.GoForward();
    }
}

void OnClickDone()
{
    CloseView();
}

public void OpenUrl(string url)
{
    if (gameObject.activeSelf == false)
    {
        gameObject.SetActive(true);
    }

    if (webView!=null)
    {
        webView.CleanCache();
        Destroy(webView);
        webView = null;
    }

    // Create a game object to hold UniWebView and add component.
    var webViewGameObject = new GameObject("UniWebView");
    webView = webViewGameObject.AddComponent<UniWebView>();
    webView.ReferenceRectTransform = rtPage;
    webView.Frame = new Rect(0, 0, Screen.width, Screen.height);

    // webview events
    webView.OnShouldClose += (view) =>
    {
        Debug.Log("OnShouldClose:" + view.Url);
        Debug.Log(view.CanGoBack + "  " + view.CanGoForward);
        CloseView();
        return true;
    };
    webView.OnPageStarted += WebView_OnPageStarted;
    webView.OnPageFinished += WebView_OnPageFinished;
    btnBack.interactable = webView.CanGoBack;
    btnForward.interactable = webView.CanGoForward;

    webView.Load(url);
    webView.Show();
}

public void CloseView()
{
    if (webView != null)
    {
        webView.CleanCache();
        Destroy(webView);
        webView = null;
    }
    gameObject.SetActive(false);
}

void OnDestroy()
{
    CloseView();
}

}

“`

2、截图
这里写图片描述

3、安装apk
下载 https://download.csdn.net/download/anyuanlzh/10450492

猜你喜欢

转载自blog.csdn.net/anyuanlzh/article/details/80526378