200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > html5 调用unity Unity调用UniWebView打开H5界面脚本

html5 调用unity Unity调用UniWebView打开H5界面脚本

时间:2023-07-19 06:40:47

相关推荐

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.讲解网站链接:/u010019717/article/details/52890644

2.官网链接:/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。