基于 UniWebView 3
官网教程 点击打开链接
using System.Collections; using System.Collections.Generic; using UnityEngine; public class WebViewController : MonoBehaviour { private UniWebView uniWebView; public RectTransform viewImage; public UniWebView GetUniWebView { get { if (uniWebView == null) { uniWebView = GameObject.FindObjectOfType<UniWebView>(); if (uniWebView == null) { GameObject webViewGameObject = GameObject.Find("UniWebView"); if (webViewGameObject == null) webViewGameObject = new GameObject("UniWebView"); uniWebView = webViewGameObject.AddComponent<UniWebView>(); uniWebView.ReferenceRectTransform = viewImage; } uniWebView.OnMessageReceived += OnMessageReceived; uniWebView.OnPageStarted += OnPageStarted; uniWebView.OnPageFinished += OnPageFinished; uniWebView.OnKeyCodeReceived += OnKeyCodeReceived; uniWebView.OnPageErrorReceived += OnPageErrorReceived; uniWebView.OnShouldClose += OnShouldClose; uniWebView.SetBackButtonEnabled(false);// 回退钮 物理按键 } return uniWebView; } } private void OnPageErrorReceived(UniWebView webView, int errorCode, string errorMessage) { Debug.Log("OnPageErrorReceived :" + string.Format("errorCode:{0},errorMessage{1}", errorCode, errorMessage)); } private void OnKeyCodeReceived(UniWebView webView, int keyCode) { Debug.Log("OnKeyCodeReceived keycode:" + keyCode); } private void OnPageFinished(UniWebView webView, int statusCode, string url) { Debug.Log("OnPageFinished statusCode:" + string.Format("statusCode:{0},url{1}", statusCode, url)); } private void OnPageStarted(UniWebView webView, string url) { Debug.Log("OnPageStarted " + url); } private void OnMessageReceived(UniWebView webView, UniWebViewMessage message) { Debug.Log("OnMessageReceived :" + message.RawMessage); // if (message.Path.Equals("game-over")) { // var score = message.Args["score"]; // Debug.Log("Your final score is: " + score); // // // Restart the game after 3 second // Invoke("OnReLoaded", 3.0f); // } if (message.Path.Equals("game")) { var score = message.Args["score"]; var name = message.Args["name"]; Debug.Log("Your final score is: " + score + "name :" + name); // Restart the game after 3 second //Invoke("OnReLoaded", 3.0f); if (GetUniWebView.isActiveAndEnabled) { string content = string.Format("openParamOne({0});", int.Parse(score) * 2 + int.Parse(name) * 1); GetUniWebView.EvaluateJavaScript(content, (payload) => { if (payload.resultCode.Equals("0")) { Debug.Log("Game Started!=========>"); } else { Debug.Log("Something goes wrong: " + payload.data); } }); } } } private bool OnShouldClose(UniWebView webView) { webView.CleanCache(); webView = null; return true; } private string url = "http://www.0000.com/TWeb.aspx"; private void OnGUI() { url = GUILayout.TextField(url, GUILayout.Width(250), GUILayout.Height(80)); if (GUILayout.Button("Load", GUILayout.Height(80))) { OnLoaded(); } if (GUILayout.Button("ReLoad", GUILayout.Height(80))) { OnReLoaded(); } if (GUILayout.Button("Close", GUILayout.Height(80))) { OnClose(); } if (GUILayout.Button("Call JS", GUILayout.Height(80))) { OnCallJavaScript(); } if (GUILayout.Button("GoBack", GUILayout.Height(80))) { if (GetUniWebView.CanGoBack) { GetUniWebView.GoBack(); } } if (GUILayout.Button("GoForward", GUILayout.Height(80))) { if (GetUniWebView.CanGoForward) { GetUniWebView.GoForward(); } } } private void OnLoaded() { GetUniWebView.Load(url); GetUniWebView.Show(); } private void OnReLoaded() { if (GetUniWebView.isActiveAndEnabled) { GetUniWebView.Reload(); } } private void OnClose() { GetUniWebView.Hide(); Destroy(GetUniWebView.gameObject); } private void OnCallJavaScript() { if (GetUniWebView.isActiveAndEnabled) { GetUniWebView.EvaluateJavaScript("openParam();", (payload) => { if (payload.resultCode.Equals("0")) { Debug.Log("Game Started!"); } else { Debug.Log("Something goes wrong: " + payload.data); } }); } // if (GetUniWebView.isActiveAndEnabled) // { // string content = string.Format ("openParam({0});", int.Parse(score) * 2 + int.Parse(name) * 1); // GetUniWebView.EvaluateJavaScript(content, (payload)=>{ // if (payload.resultCode.Equals("0")) { // Debug.Log("Game Started!"); // } else { // Debug.Log("Something goes wrong: " + payload.data); // } // }); // } } }插件下载链接