当前位置:   article > 正文

Unity内部打开Web网页——《UniWebView》,unity与网页相互调用并且传参数_unity 调用webview的方法

unity 调用webview的方法

1.首先找插件,UniWebView2.9,附上下载链接如下,https://pan.baidu.com/s/1HPvFzKU7WNHSvxHrKtX8zg   提取码:4g2i。

2.导入之后就是这样。

3.参考网上教程:( https://blog.csdn.net/qq_37310110/article/details/79761844)。

4,在UniWebViewHelper里面添加两个方法。

  1. public static UniWebView CreateUniWebView(GameObject go, string url, float top, float left, float bottom, float right)
  2. {
  3. if (go == null || !go.activeSelf)
  4. {
  5. return null;
  6. }
  7. var view = go.GetComponent<UniWebView>();
  8. if (view == null)
  9. {
  10. view = go.AddComponent<UniWebView>();
  11. }
  12. view.insets = new UniWebViewEdgeInsets(UniWebViewHelper.ConvertPixelToPoint(top, false), UniWebViewHelper.ConvertPixelToPoint(left, true), UniWebViewHelper.ConvertPixelToPoint(bottom, false), UniWebViewHelper.ConvertPixelToPoint(right, true));
  13. view.SetShowSpinnerWhenLoading(true);
  14. view.immersiveMode = false;
  15. view.url = url;
  16. return view;
  17. }
  18. private static int ConvertPixelToPoint(float num, bool v)
  19. {
  20. #if UNITY_IOS && !UNITY_EDITOR
  21.         float scale = 0;
  22.         if(v)
  23.         {
  24.             scale = 1f * screenWidth / Screen.width;
  25.         }
  26.         else
  27.         {
  28.             scale = 1f * screenHeight / Screen.height;
  29.         }
  30.         return (int)(num*scale);
  31. #endif
  32. return (int)num;
  33. }

5.创建一个新脚本OpenURL,挂在到Canvas上。

  1. #region 模块信息
  2. // **********************************************************************
  3. // Copyright (C) 2018 Blazors
  4. // Please contact me if you have any questions
  5. // File Name: OpenURL
  6. // Author: romantic123fly
  7. // WeChat||QQ: at853394528 || 853394528
  8. // **********************************************************************
  9. #endregion
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. public class OpenURL : MonoBehaviour {
  16. public InputField _url;
  17. public Button _enterBtn;
  18. public Button _backBtn;
  19. public Text _text;
  20. UniWebView _view;
  21. private void Awake()
  22. {
  23. _url.text = "https://www.baidu.com";
  24. _enterBtn.onClick.AddListener(OpenUrl);
  25. _backBtn.onClick.AddListener(CloseUrl);
  26. }
  27. public void OpenUrl()
  28. {
  29. if (_view!=null)
  30. {
  31. _view.CleanCache();
  32. }
  33. if (_url.text == null)
  34. {
  35. return;
  36. }
  37. _view = UniWebViewHelper.CreateUniWebView(gameObject, _url.text, 100,0,0,0);
  38. _view.Load();
  39. _view.OnLoadComplete += View_OnLoadComplete;
  40. _view.OnReceivedMessage += _webView_OnMessageReceived;
  41. _view.OnEvalJavaScriptFinished += _webview_OnEvalJavaScriptFinished;
  42. /*
  43. private void RemoveAllListeners() {
  44. this.OnLoadBegin = null;
  45. this.OnLoadComplete = null;
  46. this.OnReceivedMessage = null;
  47. this.OnReceivedKeyCode = null;
  48. this.OnEvalJavaScriptFinished = null;
  49. this.OnWebViewShouldClose = null;
  50. this.InsetsForScreenOreitation = null;
  51. }
  52. */
  53. }
  54. private void View_OnLoadComplete(UniWebView webView, bool success, string errorMessage)
  55. {
  56. if (success)
  57. {
  58. // 显示 加载完成的界面
  59. webView.Show();
  60. _backBtn.gameObject.SetActive(true);
  61. //unity调用网页的showAlert方法,并传入参数2222
  62. webView.EvaluatingJavaScript("showAlert('2222')");
  63. }
  64. else
  65. {
  66. Debug.LogError("Something wrong in webview loading: " + errorMessage);
  67. }
  68. }
  69. public void CloseUrl()
  70. {
  71. _view.Hide();
  72. _view.OnLoadComplete -= View_OnLoadComplete;
  73. _view.OnReceivedMessage -= _webView_OnMessageReceived;
  74. _view.OnEvalJavaScriptFinished -= _webview_OnEvalJavaScriptFinished;
  75. Destroy(_view);
  76. }
  77. //接收到网页事件消息
  78. private void _webView_OnMessageReceived(UniWebView webView, UniWebViewMessage message)
  79. {
  80. switch (message.path)
  81. {
  82. case "close":
  83. /*var name = message.args["name"];//网页传递过来的参数args[] json
  84. Debug.Log(name);*/
  85. Destroy(webView.gameObject);
  86. webView.CleanCache();
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. private void _webview_OnEvalJavaScriptFinished(UniWebView webView, string result)
  93. {
  94. if (result.Equals("0"))
  95. {
  96. Debug.Log("调用网页的showAlert成功");
  97. }
  98. else
  99. {
  100. Debug.Log("调用网页的showAlert失败");
  101. }
  102. }
  103. }

 

网页端代码。

 

参考连接; https://www.jianshu.com/p/c4f49922b8e9

https://blog.csdn.net/qq_37310110/article/details/80469144

https://blog.csdn.net/qq_37310110/article/details/79761844

https://blog.csdn.net/u010019717/article/details/52890644

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/121718
推荐阅读
相关标签
  

闽ICP备14008679号