NewMyBook/Assets/script/LinkOpener.cs
2025-03-28 16:07:42 +08:00

103 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using TMPro;
using UnityEngine.EventSystems;
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
[RequireComponent(typeof(TMP_Text))]
public class LinkOpener : MonoBehaviour, IPointerClickHandler
{
public DeepSeekDialogueManager dialogueManager;
public DialogueCaller dialogueCaller;
public TextMeshProUGUI dialogueText;
public InfoShow infoShow;
public DeepSeekReasonerStreamManager deepSeekReasonerStreamManager;
public DialogueCallerStream dialogueCallerStream;
// 监听点击事件
public void OnPointerClick(PointerEventData eventData)
{
int linkIndex = TMP_TextUtilities.FindIntersectingLink(dialogueText, eventData.position, null);
if (linkIndex != -1)
{
GameManager.Instance.infoTGo.text = "小知正在努力思考,请稍等哦...";
TMP_LinkInfo linkInfo = dialogueText.textInfo.linkInfo[linkIndex];
string linkID = linkInfo.GetLinkID(); // 获取点击的文字
if (linkID.Contains("返回"))
{
Debug.Log("返回链接被点击");
return;
}
Debug.Log($"点击了:{linkID}");
GameManager.Instance.pointStrName =linkID;
HandleLinkClick(linkID);
}
}
// 处理点击事件
private void HandleLinkClick(string linkID)
{
string myString = "请你提出一些有关" + linkID + "的问题" +
"要求是2-3个最好是科普性的小学生感兴趣的例如: 什么是航母?" +
"回答的时候最好是以JSON的格式回答比如{\"什么是航母?\": \"航空母舰是一种大型水面舰船,以舰载飞机为主要武器\", " +
"\"什么是潜艇?\": \"一种水下作战武器\"}" +
"。请一定记得回答的内容要用{}括起来,千万别忘了给整个内容加上{},每个问题的字数控制在13个字以内,每个答案的内容在250字左右";
deepSeekReasonerStreamManager.infoShow = infoShow;
deepSeekReasonerStreamManager.dialogueText = infoShow.textMeshProUGUI;
dialogueCallerStream.SendUserMessage(myString);
//dialogueManager.SendDialogueRequest(myString, linkidResponse);
}
private void linkidResponse(string response, bool isSuccess)
{
if (isSuccess)
{
print(response);
try
{
Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
infoShow.ShowInfo(dictionary);
}
catch (System.Exception)
{
infoShow.ShowInfo(
new Dictionary<string, string>
{
{"什么是潜艇?","潜艇是一种能够在水下航行的特种舰艇主要用于军事侦察、战斗巡逻、秘密运输等任务。潜艇利用内部的压载舱调整自身浮力实现浮出水面或下潜水中的能力。现代潜艇通常配备柴油发动机或核动力系统能够长时间潜伏在水下执行任务。军事潜艇通常携带鱼雷、导弹或水雷等武器能够对敌方舰船实施打击。此外潜艇还可用于海洋科学研究、深海探测和搜救任务。在历史上潜艇在第一次和第二次世界大战中都发挥了重要作用尤其是德国的“U型潜艇”对海战产生了深远影响。如今各国海军普遍装备不同类型的潜艇如战略核潜艇、攻击型潜艇和常规动力潜艇以执行各种军事和非军事任务。"},
{"什么是航母?","航空母舰,简称航母,是一种专门用于搭载和起降战斗机的大型水面舰艇。航母通常配备巨大的甲板作为飞机的跑道,并设有升降机、机库等辅助设施,使舰载机能够高效起降和维护。现代航母一般由核动力或常规动力提供动力,拥有强大的续航能力。航母不仅是一种海上军事基地,还是国家海军力量的象征,能够在远离本土的海域实施空中作战任务。其舰载机编队能够执行制空、对海打击、反潜作战、电子战等多种任务。航母战斗群通常由驱逐舰、护卫舰、补给舰和潜艇等护航舰艇组成,形成强大的作战体系。世界主要海军强国,如美国、中国、英国、法国等,都装备有不同级别的航母,并持续发展新型舰载机和航母战斗体系。"}
}
);
}
}
else
{
Debug.LogError("对话请求失败或无回复");
}
}
// 解析 [] 内的内容,转换成 <link> 形式
private string ParseClickableText(string rawText)
{
return Regex.Replace(rawText, @"\[(.*?)\]", "<link=$1><color=yellow><u>$1</u></color></link>");
}
}