powerfun-unity/Assets/Scripts/UI/Prefab/Activity/ActivityController.cs

127 lines
3.6 KiB
C#
Raw Normal View History

2022-02-16 18:22:27 +08:00
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using Newtonsoft.Json;
using System;
using System.Collections;
2022-02-11 17:26:36 +08:00
using System.Collections.Generic;
using UnityEngine;
2022-02-16 18:22:27 +08:00
using UnityEngine.SceneManagement;
2022-02-11 17:26:36 +08:00
public class ActivityController : PFUIPanel
{
Transform browser;
2022-02-16 18:22:27 +08:00
UniWebView webView;
2022-02-11 17:26:36 +08:00
protected override void Awake()
{
browser = transform.Find("Browser");
2022-02-16 18:22:27 +08:00
webView = browser.GetComponent<UniWebView>();
//webView.LoadHTMLString("<div style='width:100%;height:100%;background:#000;'></div>", "http://192.168.0.101:3081");
//webView.Show();
2022-02-11 17:26:36 +08:00
}
public override void Show()
{
base.Show();
2022-02-16 18:22:27 +08:00
//Debug.Log(20);
//Invoke("StartPageFunc",0.5f);
2022-02-11 17:26:36 +08:00
}
private void StartPageFunc()
{
Debug.Log("开始");
// Load a URL.
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
webView.ReferenceRectTransform = browser.GetComponent<RectTransform>();
webView.SetUserAgent($"UniWebView {Application.platform} {Application.version}");
webView.BackgroundColor = Utils.HexToColorHtml("#23232d");
2022-02-16 18:22:27 +08:00
webView.Load(url);
2022-02-11 17:26:36 +08:00
// Show it.
webView.Show();
webView.OnMessageReceived -= FromJs;
webView.OnMessageReceived += FromJs;
}
private void FromJs(UniWebView webView, UniWebViewMessage message)
{
switch (message.Path)
{
case "Close": CloseFunc(); break;
2022-02-16 18:22:27 +08:00
case "StartRide": StartRide(message.Args); break;
case "Share": Share(message.Args); break;
case "OpenUrl": OpenUrl(message.Args); break;
2022-02-11 17:26:36 +08:00
default:break;
}
//if (message.Path.Equals("Close"))
//{
// Debug.Log(message.Args["msg"]);
// //transform.Find("Text").GetComponent<Text>().text = message.Args["msg"];
//}
}
2022-02-16 18:22:27 +08:00
private void OpenUrl(Dictionary<string, string> args)
{
Application.OpenURL(System.Net.WebUtility.UrlDecode(args["url"]));
}
2022-02-16 18:22:27 +08:00
private void Share(Dictionary<string, string> 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;
browser.GetComponent<UniWebView>().EvaluateJavaScript("window.postMessage(\"webview;复制成功\")");
2022-02-16 18:22:27 +08:00
}
}
private void StartRide(Dictionary<string, string> args)
{
var id = int.Parse(args["id"]);
App.RouteIdParam = id;
if (args.ContainsKey("routeResult"))
{
Debug.Log(args.ContainsKey("routeResult"));
App.routeResult = JsonConvert.DeserializeObject<RouteResult>(args["routeResult"]);
}
SceneManager.LoadScene("Ride");
}
2022-02-11 17:26:36 +08:00
private void CloseFunc()
{
CancelInvoke("StartPageFunc");
Close();
}
// Update is called once per frame
void Update()
{
}
2022-02-16 18:22:27 +08:00
string url { get; set; }
public void Initial(string url)
{
this.url = url;
Debug.Log(this.url);
StartPageFunc();
return;
if (this.url != url)
{
this.url = url;
StartPageFunc();
}
else
{
//执行浏览器内刷新或者其他操作
}
}
2022-02-11 17:26:36 +08:00
}