using System.IO.Ports; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.Networking; using UnityEngine.SceneManagement; public class WebViewController : MonoBehaviour { private UniWebView webView; private SerialDevice _sp; public static string Token { get; set; } public static string FaceLoginUrl { get; set; } private void Start() { UniWebView.SetAllowAutoPlay(true); UniWebView.SetAllowInlinePlay(true); var webViewGameObject = new GameObject("UniWebView"); webView = webViewGameObject.AddComponent(); webView.SetCalloutEnabled(false); webView.SetUserAgent(webView.GetUserAgent() + "app"); webView.Frame = new Rect(0, 0, Screen.width, Screen.height); webView.OnOrientationChanged += (view, orientation) => { webView.Frame = new Rect(0, 0, Screen.width, Screen.height); }; webView.OnMessageReceived += (view, message) => { if (message.Path == "getToken") { if (!string.IsNullOrEmpty(Token)) { this.webView.EvaluateJavaScript($"faceLoginCallback('{Token}')"); Token = null; } } else if (message.Path == "facelogin") { FaceLoginUrl = "http://" + UnityWebRequest.UnEscapeURL(Regex.Match(message.RawMessage, "host=(.*)").Groups[1].Value) + "/UserCenter/Account/FaceLogin"; this.FaceLogin(); } }; webView.OnShouldClose += (view) => { webView = null; return true; }; // webView.CleanCache(); webView.Load(UniWebViewHelper.StreamingAssetURLForPath("wwwroot/index.html")); webView.Show(); } private void OnRectTransformDimensionsChange() { webView.UpdateFrame(); } private void OnDestroy() { CloseWebView(); } private void FaceLogin() { SceneManager.LoadScene(1); } private void CloseWebView() { Destroy(webView); webView = null; } public void Exit() { Application.Quit(); } }