using UnityEngine; // Test public class WebController : MonoBehaviour { UniWebView webView; void Start() { DontDestroyOnLoad(this); // Create a game object to hold UniWebView and add component. var webViewGameObject = new GameObject("UniWebView"); webView = webViewGameObject.AddComponent(); DontDestroyOnLoad(webViewGameObject); webView.Frame = new Rect(0, 0, 2000, 4000); webView.Load("https://docs.uniwebview.com/game.html"); webView.Show(); webView.OnPageFinished += (view, statusCode, url) => { DebugUtil.LogError("关闭网页"); webView.EvaluateJavaScript("startGame();", (payload) => { if (payload.resultCode.Equals("0")) { Debug.Log("Game Started!"); } else { Debug.Log("Something goes wrong: " + payload.data); } }); }; webView.OnShouldClose += (view) => { DebugUtil.LogError("清理网页"); webView = null; return true; }; webView.OnMessageReceived += (view, message) => { if (message.Path.Equals("game-over")) { var score = message.Args["score"]; DebugUtil.LogError("Your final score is: " + score); webView.EvaluateJavaScript($"getStars({score})", (payload) => { DebugUtil.LogError("Stars: " + payload.data); }); Invoke(nameof(Restart), 3.0f); } if (message.Path.Equals("close")) { Destroy(webView); webView = null; } }; } void Restart() { if (webView != null) { webView.Reload(); } } void Update() { } }