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/