batteryDiagnosis/Assets/Scripts/Manager/WebViewController.cs

128 lines
4.2 KiB
C#

using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using Vuplex.WebView;
using Vuplex.WebView.Internal;
public class WebViewController : MonoBehaviour
{
public static WebViewController Instance;
public CanvasWebViewPrefab canvasWebViewPrefab;
public CanvasWebViewPrefab canvash51Prefab;
public CanvasWebViewPrefab canvash52Prefab;
private WebUrlInitData webUrlInitData;
public GameObject WGDetection;
public GameObject HWDetection;
public GameObject ZWDetection;
private void Awake()
{
Instance = this;
var filePath = Path.Combine(Application.streamingAssetsPath, "StateTrendConfig/WebUrl.json");
//string filePath = Path.Combine(Application.streamingAssetsPath, "WebUrl.txt");
if (File.Exists(filePath))
{
var jsonStr = File.ReadAllText(filePath);
var config = JsonConvert.DeserializeObject<WebUrlInitData>(jsonStr);
webUrlInitData = config;
canvasWebViewPrefab.InitialUrl = webUrlInitData.Weburl;
//canvash51Prefab.InitialUrl = webUrlInitData.H5url1;
//canvash51Prefab.Resolution = 1;
////canvash51Prefab.WebView.Resize(414, 896);
////canvash51Prefab.WebView.Reload();
//canvash52Prefab.InitialUrl = webUrlInitData.H5url2;
Debug.Log("Web URL: " + webUrlInitData.Weburl);
}
else
{
Debug.LogError("WebUrl.json not found£¡");
}
}
private void Start()
{
canvasWebViewPrefab.Initialized += OnWebViewInitialized;
}
private async void OnWebViewInitialized(object sender, System.EventArgs e)
{
Debug.Log("ÍøÒ³¼ÓÔØÍê³É.");
canvasWebViewPrefab.WebView.ConsoleMessageLogged += HandleConsoleMessage;
//Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.ChangeCarSwitch);
await Task.Delay(2000);
WGDetection.SetActive(true);
HWDetection.SetActive(true);
ZWDetection.SetActive(true);
}
void HandleConsoleMessage(object sender, ConsoleMessageEventArgs eventArgs)
{
var message = "[Web Console] " + eventArgs.Message;
if (eventArgs.Source != null)
{
message += $" ({eventArgs.Source}:{eventArgs.Line})";
}
switch (eventArgs.Level)
{
case ConsoleMessageLevel.Error:
WebViewLogger.LogError(message, false);
break;
case ConsoleMessageLevel.Warning:
WebViewLogger.LogWarning(message, false);
break;
default:
//Debug.Log($"message: {message})");
//if (message.Contains(""))
//{
// Debug.Log($"½ÓÊܵÄÍøÒ³¶ËÏûÏ¢: {message}");
//}
break;
}
}
public void CallGetButtonClickWithCode(string parameter)
{
if (canvasWebViewPrefab != null)
{
Debug.Log("·¢ËÍÏûÏ¢:" + parameter);
string jsCode = $"getCode('{parameter}');";
canvasWebViewPrefab.WebView.ExecuteJavaScript(jsCode);
}
}
private void Update()
{
if(Input.GetKey(KeyCode.LeftShift)&& Input.GetKeyDown(KeyCode.Alpha1))
{
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.ChangeCarSwitch);
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Alpha2))
{
CallGetButtonClickWithCode("¶þάÂë");
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Alpha3))
{
CallGetButtonClickWithCode("³µÅÆÊ¶±ð");
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Alpha4))
{
CallGetButtonClickWithCode("µç³Ø¼ì²â");
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Alpha5))
{
CallGetButtonClickWithCode("ÉùÎÆ¼ì²â");
}
}
}
public class WebUrlInitData
{
[JsonProperty("weburl")]
public string Weburl { get; set; }
[JsonProperty("h5url1")]
public string H5url1 { get; set; }
[JsonProperty("h5url2")]
public string H5url2 { get; set; }
}