资料参考地址:
https://blog.csdn.net/qq_37310110/article/details/79761844
https://blog.csdn.net/shen71702163/article/details/79283645
https://docs.uniwebview.com/guide/position-and-size.html#setting-frame
首先这里提供几个可以使用的UniWebView插件
链接:https://pan.baidu.com/s/1oOiUftaioXEn06-FIRTZkA
提取码:9yao
导入之后就是这样,还有个参考Demo
参考网上教程:(https://blog.csdn.net/qq_37310110/article/details/79761844#commentsedit)
在UniWebViewHelper里面加个方法
public static UniWebView CreateUniWebView(GameObject go, string url, float top, float left, float bottom, float right)
{
if (go == null || !go.activeSelf)
{
return null;
}
var view = go.GetComponent();
if (view == null)
{
view = go.AddComponent();
}
view.insets = new UniWebViewEdgeInsets(UniWebViewHelper.ConvertPixelToPoint(top, false), UniWebViewHelper.ConvertPixelToPoint(left, true), UniWebViewHelper.ConvertPixelToPoint(bottom, false), UniWebViewHelper.ConvertPixelToPoint(right, true));
view.SetShowSpinnerWhenLoading(true);
view.immersiveMode = false;
view.url = url;
return view;
}
private static int ConvertPixelToPoint(float num, bool v)
{
#if UNITY_IOS && !UNITY_EDITOR
float scale = 0;
if(v)
{
scale = 1f * screenWidth / Screen.width;
}
else
{
scale = 1f * screenHeight / Screen.height;
}
return (int)(num*scale);
#endif
return (int)num;
}
目的在于随时用随时Create。而且网页显示界面的初始化也可以写在里面包括一些界面尺寸,显示模式,目标网址
新建OpenUrl类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OpenURL : MonoBehaviour
{
public InputField _url;
public Button _enterBtn;
public Button _backBtn;
UniWebView _view;
private void Awake()
{
_enterBtn.onClick.AddListener(OpenUrl);
}
public void OpenUrl()
{
if (_view != null)
{
_view.CleanCache();
}
if (_url.text == null)
{
return;
}
_view = UniWebViewHelper.CreateUniWebView(gameObject, "https://" + _url.text, 100, 0, 50, 0);
_view.OnLoadComplete += View_OnLoadComplete;
_view.Load();
}
private void View_OnLoadComplete(UniWebView webView, bool success, string errorMessage)
{
if (success)
{
webView.Show();
_backBtn.gameObject.SetActive(true);
}
else
{
Debug.LogError("Something wrong in webview loading: " + errorMessage);
}
}
public void CloseUrl()
{
_view.Hide();
_view.OnLoadComplete -= View_OnLoadComplete;
Destroy(_view);
}
}
创建两个按钮和一个输入框,在canvas下挂OpenURL和UniWebView这两个脚本,记得给OpenURL挂按钮,那个UniWebView参数直接是在代码改变的,所以不用设置。
然后,这样就成功了,不过注意如图下是因为在OpenURL脚本里的OpenUrl方法本来加了http://,可以选择去掉或者输入不要http://。