2025-09-24 19:24:19 +08:00

73 lines
1.9 KiB
C#

using System;
using UnityEngine;
public class ChatAI : MonoBehaviour
{
/// <summary>
/// 聊天配置
/// </summary>
public ChatSetting m_ChatSettings;
public static ChatAI Instance { get; private set; }
Action<string> recordingCallBack;
void Awake()
{
Instance = this;
//DontDestroyOnLoad(this);
}
#region AI智能体
public void SendData(string _postWord, Action<string> action = null)
{
if (string.IsNullOrEmpty(_postWord)) return;
m_ChatSettings.m_ChatModel.PostMsg(_postWord, reply => {
action?.Invoke(reply);
});
}
public void StopSend()
{
m_ChatSettings.m_ChatModel.StopMsg();
}
#endregion
#region
/// <summary>
/// 开始录制
/// </summary>
public void StartRecord()
{
//m_ChatSettings.m_RealtimeSpeechToText?.StopSpeechToText(null);//停止流式语音
m_ChatSettings.m_SpeechToText?.StartSpeechToText(null);//开启非流式语音
}
/// <summary>
/// 结束录制
/// </summary>
public void StopRecord(Action<string> callBack)
{
m_ChatSettings.m_SpeechToText?.StopSpeechToText(callBack);//停止非流式语音
//m_ChatSettings.m_RealtimeSpeechToText?.StartSpeechToText(recordingCallBack);//开启流式语音
}
/// <summary>
/// 开始实时录制
/// </summary>
public void StartRecording(Action<string> callBack)
{
recordingCallBack = callBack;
m_ChatSettings.m_RealtimeSpeechToText.StartSpeechToText(recordingCallBack);
}
public void StopRecording()
{
recordingCallBack = null;
m_ChatSettings.m_RealtimeSpeechToText.StopSpeechToText(null);
}
#endregion
#region
public void PlayVoice(string speakText,Action<AudioClip> callBack = null)
{
m_ChatSettings.m_TextToSpeech.StartSpeak(speakText, callBack);
}
public void StopVoice()
{
m_ChatSettings.m_TextToSpeech.StopSpeak();
}
#endregion
}