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; #if UNITY_STANDALONE_WIN using ZenFulcrum.EmbeddedBrowser; #endif public class ActivityController : PFUIPanel { Transform browser; #if UNITY_STANDALONE_WIN Browser webView; #else UniWebView webView; #endif protected override void Awake() { this.url = null; browser = transform.Find("Browser"); #if UNITY_STANDALONE_WIN webView = browser.GetComponent(); webView.onLoad -= RegisterFunc; webView.onLoad += RegisterFunc; #if UNITY_EDITOR webView.ShowDevTools(true); #endif #else webView = browser.GetComponent(); #endif //webView.Show(); } #if UNITY_STANDALONE_WIN private void RegisterFunc(JSONNode obj) { RunJavaScript($"navigator.pfLang = '{App.GetLocalLanguage()}';"); webView.RegisterFunction("Close", args => { Debug.Log("执行返回"); CloseFunc(); }); webView.RegisterFunction("StartRide", args => { if (args.Count == 2) { StartRide((int)args[0], args[1]); } }); webView.RegisterFunction("OpenUrl", args => { if (args.Count == 1) { OpenUrl(args[0]); } }); webView.RegisterFunction("Share", args => { if (args.Count == 2) { Share((int)args[0], (int)args[1]); } }); } #endif public override void Show() { base.Show(); //Debug.Log(20); //Invoke("StartPageFunc",0.5f); } #if UNITY_STANDALONE_WIN private void StartPageFunc(Action action = null) { if (action == null) { //webView.UserAgent = "tes "+ App.GetLocalLanguage(); webView.LoadURL(url, true); } else { action.Invoke(); } } #else 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(int.Parse(message.Args["id"]),message.Args.ContainsKey("routeResult")?message.Args["routeResult"]:null); break; case "Share": Share(int.Parse(message.Args["type"]),int.Parse(message.Args["id"])); break; case "OpenUrl": OpenUrl(message.Args["url"]); break; default:break; } } #endif private void OpenUrl(string url) { Application.OpenURL(System.Net.WebUtility.UrlDecode(url)); } private void Share(int type, int id) { string url = $"http://pfweb.juze.pro/Activity/LightUpChina/Share?userId={App.CurrentUser.Id}"; if (type < 2) { if (App.weChatController.IsWeChatAppInstalled()) { App.weChatController.ShareWebpageToWX(type, url, App.GetLocalString("Light up China Share"), "by " + App.CurrentUser.Nickname, null); } } else if (type == 2) { UnityEngine.GUIUtility.systemCopyBuffer = url; RunJavaScript($"window.postMessage(\"webview;{App.GetLocalString("Copy Success")}\")"); } else { Facebook.Unity.FB.ShareLink(contentURL: new Uri(url), contentTitle: App.GetLocalString("Light up China Share"), contentDescription: "By " + App.CurrentUser.Nickname, photoURL: new Uri(App.CurrentUser.WxHeadImg)); } } private void StartRide(int id,string routeResult) { App.RouteIdParam = id; if (routeResult!=null) { App.routeResult = JsonConvert.DeserializeObject(routeResult); } SceneManager.LoadScene("Ride"); } private void CloseFunc() { CancelInvoke("StartPageFunc"); Close(); } private void RunJavaScript(string js) { #if UNITY_STANDALONE_WIN webView.EvalJS(js); #else browser.GetComponent().EvaluateJavaScript(js); #endif } // Update is called once per frame void Update() { } string url { get; set; } public void Initial(string url) { Debug.Log(url); if (this.url != url) { this.url = url; StartPageFunc(); } else { //执行浏览器内刷新或者其他操作 StartPageFunc(()=> { RunJavaScript("window.postMessage(\"webview;Refresh\")"); }); } } }