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(jsonStr); webUrlInitData = config; canvasWebViewPrefab.InitialUrl = webUrlInitData.Weburl; canvash51Prefab.InitialUrl = webUrlInitData.H5url1; 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 class WebUrlInitData { [JsonProperty("weburl")] public string Weburl { get; set; } [JsonProperty("h5url1")] public string H5url1 { get; set; } [JsonProperty("h5url2")] public string H5url2 { get; set; } }