html5 调用unity,Unity调用UniWebView打开H5界面脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//********************************************************************
// 文件名: ShowH5Controller
// 描述: 顯示H5界面
// 作者:
// 創建時間: #CREATIONDATE#
//********************************************************************
public class ShowH5Controller : MonoBehaviour
{
/// /// H5的數據
///
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();
}
return _uniWebView;
}
set
{
_uniWebView = value;
}
}
private UniWebView _uniWebView;
/// /// loading物體
///
public GameObject loadingGo;
/// /// h5的返回按鍵是否使用
///
[HideInInspector]
public bool backButtonEnable = false;
public static ShowH5Controller Instance;
/// /// 異常的退出回調
///
public Action errorCallBack;
/// /// 是否顯示H5界面
///
private bool isShowH5;
/// /// 數據
///
private H5Data data;
private void Awake()
{
Instance = this;
}
/// /// 顯示H5界面
///
///
/// url后面跟的參數
public void ShowH5(string h5_url, Dictionaryurl_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);
}
/// /// 顯示H5界面
///
///
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);
}
/// /// 加載頁面
///
///
///
///
private void OnLoadComplete(UniWebView webView, bool success, string errorMessage)
{
if(!success)
{
if (errorCallBack != null) errorCallBack();
Debug.Log("加載頁面失敗!!!!!!!" + errorMessage);
}
else
{
loadingGo.SetActive(false);
uniWebView.Show();
}
}
/// /// 關閉H5界面
///
public void CloseH5()
{
ClearUniWebView();
data = null;
isShowH5 = false;
//Debug.Log("關閉h5的界面url!!");
}
/// /// 清空UniWebView
///
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/
總結
以上是生活随笔為你收集整理的html5 调用unity,Unity调用UniWebView打开H5界面脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 胖子脸:库珀·布莱克100年
- 下一篇: 脚本入门(2)-perl脚本