using Assets.Scripts; using Assets.Scripts.Apis.Models; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class ActivityController : PFUIPanel { Transform browser; UniWebView webView; protected override void Awake() { browser = transform.Find("Browser"); webView = browser.GetComponent(); //webView.Show(); } public override void Show() { base.Show(); //Debug.Log(20); //Invoke("StartPageFunc",0.5f); } private void StartPageFunc(Action action = null) { Debug.Log("开始"); // Load a URL. webView.Frame = new Rect(0, 0, Screen.width, Screen.height); webView.ReferenceRectTransform = browser.GetComponent(); webView.SetUserAgent($"UniWebView {Application.platform} {Application.version}"); webView.BackgroundColor = Utils.HexToColorHtml("#23232d"); if (action == null) { webView.Load(url); } else { action.Invoke(); } // Show it. webView.Show(); webView.OnMessageReceived -= FromJs; webView.OnMessageReceived += FromJs; } private void FromJs(UniWebView webView, UniWebViewMessage message) { switch (message.Path) { case "Close": CloseFunc(); break; case "StartRide": StartRide(message.Args); break; case "Share": Share(message.Args); break; case "OpenUrl": OpenUrl(message.Args); break; default:break; } } private void OpenUrl(Dictionary args) { Application.OpenURL(System.Net.WebUtility.UrlDecode(args["url"])); } private void Share(Dictionary args) { int type = int.Parse(args["type"]); int id = int.Parse(args["id"]); string url = $"http://192.168.0.101:3081/Activity/LightUpChina/Share?userId={App.CurrentUser.Id}"; if (type < 2) { if (App.weChatController.IsWeChatAppInstalled()) { App.weChatController.ShareWebpageToWX(type, url, "点亮中国活动", "by " + App.CurrentUser.Nickname, null); } } else { UnityEngine.GUIUtility.systemCopyBuffer = url; RunJavaScript("window.postMessage(\"webview;复制成功\")"); } } private void StartRide(Dictionary args) { var id = int.Parse(args["id"]); App.RouteIdParam = id; if (args.ContainsKey("routeResult")) { Debug.Log(args.ContainsKey("routeResult")); App.routeResult = JsonConvert.DeserializeObject(args["routeResult"]); } SceneManager.LoadScene("Ride"); } private void CloseFunc() { CancelInvoke("StartPageFunc"); Close(); } private void RunJavaScript(string js) { browser.GetComponent().EvaluateJavaScript(js); } // Update is called once per frame void Update() { } string url { get; set; } public void Initial(string url) { if (this.url != url) { this.url = url; StartPageFunc(); } else { //执行浏览器内刷新或者其他操作 StartPageFunc(()=> { RunJavaScript("window.postMessage(\"webview;Refresh\")"); }); } } }