NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Debug/SROptions.Notice.cs

61 lines
2.0 KiB
C#

using System;
using UnityEngine;
using System.ComponentModel;
using Object = UnityEngine.Object;
public partial class SROptions
{
private UniWebView webView;
[Category("公告"), DisplayName("公告内容")] public string NoticeContent { get; set; }
[Category("公告"), DisplayName("显示测试公告")]
public void ShowDebugNotice()
{
try
{
var webViewGameObject = new GameObject("UniWebViewTest");
Object.DontDestroyOnLoad(webViewGameObject);
webView = webViewGameObject.AddComponent<UniWebView>();
webView.Frame = new Rect(0, 0, Screen.width / 2, Screen.height / 2);
webView.LoadHTMLString(NoticeContent, null, false);
webView.EmbeddedToolbar.Show();
webView.Show();
webView.OnMessageReceived += HandleMessageReceived;
webView.OnShouldClose += (view) =>
{
webView.OnMessageReceived -= HandleMessageReceived;
webView = null;
Object.DestroyImmediate(webViewGameObject);
return true;
};
}
catch (Exception e)
{
DebugUtil.LogError("创建内置浏览器异常:{0}", e);
}
}
// 不是正式方法 需要转译字符
private void HandleMessageReceived(UniWebView web, UniWebViewMessage message)
{
if (message.Path != "open") return;
// 从 RawMessage 中手动提取 URL 参数
var rawMessage = message.RawMessage;
var startIndex = rawMessage.IndexOf("url=", StringComparison.Ordinal) + 4;
if (startIndex > 3)
{
var openUrl = rawMessage.Substring(startIndex);
openUrl = System.Uri.UnescapeDataString(openUrl);
Debug.Log($"跳转链接是: {openUrl}");
Application.OpenURL(openUrl);
}
else
{
Debug.LogError("未找到有效的 URL 参数!");
}
}
}