Unity调用UniWebView打开H5界面脚本

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//********************************************************************
// 文件名: ShowH5Controller
// 描述: 显示H5界面
// 作者: 
// 创建时间: #CREATIONDATE#
//********************************************************************
public class ShowH5Controller : MonoBehaviour
{
    /// <summary>
    /// H5的数据
    /// </summary>
    public class H5Data
    {
        public string h5_url;
        public UniWebView.ReceivedMessageDelegate OnReceivedMessage;
        public UniWebView.WebViewShouldCloseDelegate OnWebViewShouldClose;
    }

    public UniWebView uniWebView
    {
        get
        {
            if (_uniWebView == null)
            {
                GameObject go = new GameObject("UniWebView");
                _uniWebView = go.AddComponent<UniWebView>();
            }

            return _uniWebView;
        }
        set
        {
            _uniWebView = value;
        }

    }

    private UniWebView _uniWebView;
    /// <summary>
    /// loading物体
    /// </summary>
    public GameObject loadingGo;
    /// <summary>
    /// h5的返回按键是否使用
    /// </summary>
    [HideInInspector]
    public bool backButtonEnable = false;
    public static ShowH5Controller Instance;
    /// <summary>
    /// 异常的退出回调
    /// </summary>
    public Action errorCallBack;
    /// <summary>
    /// 是否显示H5界面
    /// </summary>
    private bool isShowH5;
    /// <summary>
    /// 数据
    /// </summary>
    private H5Data data;

    private void Awake()
    {
        Instance = this;
    }

    /// <summary>
    /// 显示H5界面
    /// </summary>
    /// <param name="h5_url"></param>
    /// <param name="url_param">url后面跟的参数</param>
    public void ShowH5(string h5_url, Dictionary<string,string> url_param, UniWebView.ReceivedMessageDelegate OnReceivedMessage, UniWebView.WebViewShouldCloseDelegate OnWebViewShouldClose)
    {
        if (string.IsNullOrEmpty(h5_url))
        {
            //Debug.Log("显示h5的界面url为空");
            LogTool.AddLogString("显示h5的界面url为空");
            if (errorCallBack != null) errorCallBack();
            return;
        }

        string param = "?";

        foreach (var item in url_param)
        {
            if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value)) continue;

            param += item.Key + "=" + item.Value + "&";
        }
       

        h5_url += param.TrimEnd('&');
        h5_url = h5_url.Replace(" ", "");
        ShowH5(h5_url, OnReceivedMessage, OnWebViewShouldClose);
    }

    /// <summary>
    /// 显示H5界面
    /// </summary>
    /// <param name="h5_url"></param>
    public void ShowH5(string h5_url, UniWebView.ReceivedMessageDelegate OnReceivedMessage, UniWebView.WebViewShouldCloseDelegate OnWebViewShouldClose)
    {
        if (string.IsNullOrEmpty(h5_url))
        {
            //Debug.Log("显示h5的界面url为空");
            LogTool.AddLogString("显示h5的界面url为空");
            if (errorCallBack != null) errorCallBack();
            return;
        }

        isShowH5 = true;
        data = new H5Data() { h5_url = h5_url, OnReceivedMessage = OnReceivedMessage, OnWebViewShouldClose = OnWebViewShouldClose };
        uniWebView.OnLoadComplete += OnLoadComplete;
        loadingGo.SetActive(true);
        uniWebView.Load(h5_url);
        uniWebView.backButtonEnable = backButtonEnable;
        uniWebView.SetShowSpinnerWhenLoading(false);
        if (OnReceivedMessage != null)
            uniWebView.OnReceivedMessage += OnReceivedMessage;
        if (OnWebViewShouldClose != null)
            uniWebView.OnWebViewShouldClose += OnWebViewShouldClose;
        LogTool.AddLogString("h5的界面url:" + h5_url);
    }

    /// <summary>
    /// 加载页面
    /// </summary>
    /// <param name="webView"></param>
    /// <param name="success"></param>
    /// <param name="errorMessage"></param>
    private void OnLoadComplete(UniWebView webView, bool success, string errorMessage)
    {
        if(!success)
        {
            if (errorCallBack != null) errorCallBack();
            Debug.Log("加载页面失败!!!!!!!" + errorMessage);
        }
        else
        {
            loadingGo.SetActive(false);
            uniWebView.Show();
        }
    }

    /// <summary>
    /// 关闭H5界面
    /// </summary>
    public void CloseH5()
    {
        ClearUniWebView();
        data = null;
        isShowH5 = false;
        //Debug.Log("关闭h5的界面url!!");
    }

    /// <summary>
    /// 清空UniWebView
    /// </summary>
    private void ClearUniWebView()
    {
        backButtonEnable = false;
        //uniWebView.CleanCache();
        uniWebView.Hide();
        Destroy(uniWebView);
        Destroy(uniWebView.gameObject);
        uniWebView = null;
    }

    private void OnApplicationPause(bool pause)
    {
        if(pause)
        {
            if (isShowH5 && data != null)
            {
                ClearUniWebView();
            }
        }
        else
        {
            if(isShowH5 && data != null)
            {
                ShowH5(data.h5_url, data.OnReceivedMessage, data.OnWebViewShouldClose);
            }
        }
    }
}

1.讲解网站链接:https://blog.csdn.net/u010019717/article/details/52890644

2.官网链接:http://uniwebview.onevcat.com/

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值