赞
踩
UniWebView 是一个 Unity 3D 插件,用于将 Web 视图组件添加到 iOS 和 Android 上的 Unity 3D 手机游戏。它在 C# 中提供了一组抽象、跨平台和高级 API。使用 UniWebView,您可以在不了解原生开发的情况下将一些行为(例如浏览器)放入您的游戏中。
当您需要显示公告或通知、为玩家排名添加排行榜或向您的用户显示任何交互式网页时,它会极大地促进您的开发过程。
官网地址
UniWebView 4.7
提取码:2jhf
UniWebView 3.0+
提取码:gd0w
1.使用方法相对简单,直接上代码
public class UniWebPresenter :UIPresenterBase
{
[SerializeField] private RectTransform m_UniWebRectTransform; //UniWebViewNativeListener物体的父物体
private UniWebView m_UniWebView;
private string m_Url;
private bool m_Log;
public override void Init(params object[] parameters)
{
m_Url = parameters[0] as string;
if(string.IsNullOrEmpty(m_Url))
{
Destroy(gameObject);
}
m_Log = GameSetting.Instance.Log;
CreateUniWebView();
OnLoaded();
}
public void CreateUniWebView()
{
m_UniWebView = m_UniWebRectTransform.gameObject.AddComponent<UniWebView>();
m_UniWebView.ReferenceRectTransform = m_UniWebRectTransform;
m_UniWebView.OnMessageReceived += OnMessageReceived;
m_UniWebView.OnPageStarted += OnPageStarted;
m_UniWebView.OnPageFinished += OnPageFinished;
m_UniWebView.OnKeyCodeReceived += OnKeyCodeReceived;
m_UniWebView.OnPageErrorReceived += OnPageErrorReceived;
m_UniWebView.OnShouldClose += OnShouldClose;
m_UniWebView.SetBackButtonEnabled(true);// 回退钮 物理按键
m_UniWebView.SetAllowFileAccessFromFileURLs(true);//访问本地的权限
m_UniWebView.SetShowSpinnerWhileLoading(true);//过程中是否显示加载指示器。
//uniWebView.SetSpinnerText("Loading");//设置显示在加载指示器中的文本
m_UniWebView.SetHorizontalScrollBarEnabled(false);// 设置是否在Web内容超出Web视图范围时显示水平滚动条。
m_UniWebView.SetVerticalScrollBarEnabled(false);// 设置当Web内容超出Web视图范围时,是否应显示垂直滚动条。
m_UniWebView.BackgroundColor = Color.white;//背景
}
/// <summary>
/// 加载页面
/// </summary>
/// <param name="url"></param>
public void OnLoaded()
{
m_UniWebView.Load(m_Url);
m_UniWebView.Show();
}
/// <summary>
/// 重新加载页面
/// </summary>
private void OnReLoaded()
{
if (m_UniWebView.isActiveAndEnabled)
{
m_UniWebView.Reload();
}
}
/// <summary>
/// 关闭页面
/// </summary>
private void OnClose()
{
m_UniWebView.Hide();
Destroy(gameObject);
}
/// <summary>
/// 开始加载web视图触发
/// </summary>
/// <param name="webView"></param>
/// <param name="url"></param>
private void OnPageStarted(UniWebView webView, string url)
{
if(m_Log)
Debug.Log("[UbiWebPresenter] OnPageStarted " + url);
}
/// <summary>
/// 加载web视图成功触发
/// </summary>
/// <param name="webView"></param>
/// <param name="statusCode"></param>
/// <param name="url"></param>
private void OnPageFinished(UniWebView webView, int statusCode, string url)
{
if (m_Log)
Debug.Log("[UbiWebPresenter] OnPageFinished statusCode:" + string.Format("statusCode:{0},url{1}", statusCode, url));
}
/// <summary>
/// 页面加载过程中引发错误
/// </summary>
/// <param name="webView"></param>
/// <param name="errorCode"></param>
/// <param name="errorMessage"></param>
private void OnPageErrorReceived(UniWebView webView, int errorCode, string errorMessage)
{
if (errorCode == -2)
{
OnClose();
}
if (m_Log)
Debug.Log("[UbiWebPresenter] OnPageErrorReceived :" + string.Format("errorCode:{0},errorMessage{1}", errorCode, errorMessage));
}
/// <summary>
/// 设备按键触发
/// </summary>
/// <param name="webView"></param>
/// <param name="keyCode"></param>
private void OnKeyCodeReceived(UniWebView webView, int keyCode)
{
if (keyCode == 4)
{
OnClose();
}
if (m_Log)
Debug.Log("[UbiWebPresenter] OnKeyCodeReceived keycode:" + keyCode);
}
/// <summary>
/// JS调用Unity时触发
/// </summary>
/// <param name="webView"></param>
/// <param name="message"></param>
private void OnMessageReceived(UniWebView webView, UniWebViewMessage message)
{
if (m_Log)
Debug.Log("[UbiWebPresenter] OnMessageReceived :" + message.RawMessage);
}
/// <summary>
/// 视图自动关闭时触发
/// </summary>
/// <param name="webView"></param>
/// <returns></returns>
private bool OnShouldClose(UniWebView webView)
{
webView.CleanCache();//清除缓存
webView = null;
return true;
}
}
- url必须加协议前缀 http://、https://之类,否则网页打开一片空白
- 注意:UniWebView只支持Android、iOS和Mac平台,Windows是打不开的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。