60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using TMPro;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using System.Text.RegularExpressions;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class DialogueCaller : MonoBehaviour
|
||
|
{
|
||
|
public DeepSeekDialogueManager dialogueManager;
|
||
|
public TextMeshProUGUI dialogueText;
|
||
|
public InputField InputField;
|
||
|
// Start is called before the first frame update
|
||
|
void Start()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
public void _Start()
|
||
|
{
|
||
|
string userInput = InputField.text;
|
||
|
InputField.text = "";
|
||
|
|
||
|
dialogueText.text = "让我思考一下~";
|
||
|
|
||
|
|
||
|
string myString = "请你的回答中如果出现了专有名词,例如 潜艇,用[潜艇]代替,请不要使用专有名词,以下是我的提问:";
|
||
|
|
||
|
dialogueManager.SendDialogueRequest(myString+userInput, OnDialogueResponse);
|
||
|
}
|
||
|
|
||
|
// 回调函数,用于处理 DeepSeek API 的响应
|
||
|
private void OnDialogueResponse(string response, bool isSuccess)
|
||
|
{
|
||
|
if (isSuccess)
|
||
|
{
|
||
|
print(response);
|
||
|
Debug.Log("NPC 回复: " + response);
|
||
|
|
||
|
dialogueText.text = ParseClickableText(response);
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogError("对话请求失败或无回复");
|
||
|
dialogueText.text = "对话请求失败或无回复";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 解析 [] 内的内容,转换成 <link> 形式
|
||
|
private string ParseClickableText(string rawText)
|
||
|
{
|
||
|
return Regex.Replace(rawText, @"\[(.*?)\]", "<link=$1><color=yellow><u>$1</u></color></link>");
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|