赞
踩
1.首先找插件,UniWebView2.9,附上下载链接如下,https://pan.baidu.com/s/1HPvFzKU7WNHSvxHrKtX8zg 提取码:4g2i。
2.导入之后就是这样。
3.参考网上教程:( https://blog.csdn.net/qq_37310110/article/details/79761844)。
4,在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<UniWebView>();
-
- if (view == null)
-
- {
-
- view = go.AddComponent<UniWebView>();
-
- }
-
- 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;
-
- }
5.创建一个新脚本OpenURL,挂在到Canvas上。
- #region 模块信息
- // **********************************************************************
- // Copyright (C) 2018 Blazors
- // Please contact me if you have any questions
- // File Name: OpenURL
- // Author: romantic123fly
- // WeChat||QQ: at853394528 || 853394528
- // **********************************************************************
- #endregion
- 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;
- public Text _text;
-
- UniWebView _view;
- private void Awake()
- {
- _url.text = "https://www.baidu.com";
- _enterBtn.onClick.AddListener(OpenUrl);
- _backBtn.onClick.AddListener(CloseUrl);
- }
- public void OpenUrl()
- {
-
- if (_view!=null)
- {
- _view.CleanCache();
- }
- if (_url.text == null)
- {
- return;
- }
- _view = UniWebViewHelper.CreateUniWebView(gameObject, _url.text, 100,0,0,0);
- _view.Load();
-
- _view.OnLoadComplete += View_OnLoadComplete;
- _view.OnReceivedMessage += _webView_OnMessageReceived;
- _view.OnEvalJavaScriptFinished += _webview_OnEvalJavaScriptFinished;
- /*
- private void RemoveAllListeners() {
- this.OnLoadBegin = null;
- this.OnLoadComplete = null;
- this.OnReceivedMessage = null;
- this.OnReceivedKeyCode = null;
- this.OnEvalJavaScriptFinished = null;
- this.OnWebViewShouldClose = null;
- this.InsetsForScreenOreitation = null;
- }
- */
- }
- private void View_OnLoadComplete(UniWebView webView, bool success, string errorMessage)
- {
- if (success)
- {
- // 显示 加载完成的界面
- webView.Show();
- _backBtn.gameObject.SetActive(true);
- //unity调用网页的showAlert方法,并传入参数2222
- webView.EvaluatingJavaScript("showAlert('2222')");
- }
- else
- {
- Debug.LogError("Something wrong in webview loading: " + errorMessage);
- }
- }
- public void CloseUrl()
- {
- _view.Hide();
- _view.OnLoadComplete -= View_OnLoadComplete;
- _view.OnReceivedMessage -= _webView_OnMessageReceived;
- _view.OnEvalJavaScriptFinished -= _webview_OnEvalJavaScriptFinished;
- Destroy(_view);
- }
-
- //接收到网页事件消息
- private void _webView_OnMessageReceived(UniWebView webView, UniWebViewMessage message)
- {
- switch (message.path)
- {
- case "close":
- /*var name = message.args["name"];//网页传递过来的参数args[] json
- Debug.Log(name);*/
- Destroy(webView.gameObject);
- webView.CleanCache();
- break;
- default:
- break;
- }
- }
- private void _webview_OnEvalJavaScriptFinished(UniWebView webView, string result)
- {
- if (result.Equals("0"))
- {
- Debug.Log("调用网页的showAlert成功");
- }
- else
- {
- Debug.Log("调用网页的showAlert失败");
- }
- }
- }
网页端代码。
参考连接; https://www.jianshu.com/p/c4f49922b8e9
https://blog.csdn.net/qq_37310110/article/details/80469144
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。