未找到uniwebview_unity内嵌浏览器——UniWebView插件
這次突然被要求實現內嵌瀏覽器。在網上到處找資料參考,下面幾個鏈接的內容感覺很實用
https://blog.csdn.net/qq_37310110/article/details/79761844
https://blog.csdn.net/shen71702163/article/details/79283645
分享一下這次心得吧,以下是個人感受,技術不太好,說錯了敬請糾正。
1.首先找插件,小編就找到了個UniWebView2.9,附上下載鏈接如下,https://pan.baidu.com/s/1HPvFzKU7WNHSvxHrKtX8zg? ?提取碼:4g2i
2.導入之后就是這樣,還有個參考Demo
在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;
}
3.后面發現報紅線了
發現這個方法沒有,網上那個教程也沒說這個方法,只能自立更生思考怎么寫了,發現這個是界面顯示方法,參考官方文檔不知道怎么寫好,只能換種思路,繼續查“UniWebView案例”。
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;
}
5.創建一個新腳本OpenURL,(代碼有點長,大家可以直接去上一個鏈接那里復制就行)
using System;
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);
_backBtn.onClick.AddListener(CloseUrl);
_backBtn.gameObject.SetActive(false);
}
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);
}
}
6.創建兩個按鈕和一個輸入框,在canvas下掛OpenURL和UniWebView這兩個腳本,記得給OpenURL掛按鈕,那個UniWebView參數直接是在代碼改變的,所以不用設置。
7.這樣就成功了,不過注意如圖下是因為在OpenURL腳本里的OpenUrl方法本來加了http://,可以選擇去掉或者輸入不要http://。
8.效果如下(之前的top=100是為了空白可以顯示上面的返回按鈕,bottom=50底部留白)
總結
以上是生活随笔為你收集整理的未找到uniwebview_unity内嵌浏览器——UniWebView插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [软技能] 在前后端分离项目里,请说说
- 下一篇: [html] button标签的typ