【AI私房菜】Unity 接入 DeepSeek AI 并实现语音交互
作者:微信文章本教程将带你完整实现 Unity 集成 DeepSeek AI,并添加语音输入(麦克风识别)和语音输出(TTS 朗读)功能,打造一个完整的 AI 语音助手应用。
## **📝 教程目录**
1. **准备工作**2. **Unity 接入 DeepSeek API(HTTP 请求)**3. **添加语音输入(麦克风识别)**4. **添加语音输出(TTS 朗读)**5. **构建 UI 交互界面**6. **进阶优化(多语言、离线识别、错误处理)**7. **测试与发布**
## **1️⃣ 准备工作**
### **1.1 获取 DeepSeek API Key**
1. 访问 (https://deepseek.com) 并注册账号
2. 进入 **API 管理** 页面,获取你的 `API Key`
### **1.2 创建 Unity 项目**
- **Unity 版本**: 2020 或更高
- **项目模板**: 3D 或 2D 均可
- **推荐包安装**(Window > Package Manager):
- `TextMeshPro`(更好的 UI 文本显示)
- `Newtonsoft.Json`(可选,更强大的 JSON 解析)
### **1.3 项目结构**
```Assets/├─ Scripts/│ ├─ DeepSeekManager.cs // API 交互│ ├─ VoiceRecognition.cs // 语音输入│ ├─ TextToSpeechManager.cs // 语音输出│ └─ UIController.cs // UI 控制├─ Scenes/│ └─ Main.unity // 主场景└─ Resources/ └─ ... // 音频、字体等资源```
## **2️⃣ Unity 接入 DeepSeek API**
### **2.1 创建 `DeepSeekManager.cs`**
```csharpusing UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Text;public class DeepSeekManager : MonoBehaviour{ private const string API_URL = "https://api.deepseek.com/v1/chat/completions"; private string apiKey = "YOUR_API_KEY"; // 替换成你的 API Key public static DeepSeekManager Instance { get; private set; } private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } public void SendMessageToDeepSeek(string message, System.Action<string> callback) { StartCoroutine(PostRequest(message, callback)); } private IEnumerator PostRequest(string message, System.Action<string> callback) { var requestData = new { model = "deepseek-chat", messages = new[] { new { role = "user", content = message } }, temperature = 0.7 }; string jsonData = JsonUtility.ToJson(requestData); byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData); using (UnityWebRequest request = new UnityWebRequest(API_URL, "POST")) { request.uploadHandler = new UploadHandlerRaw(bodyRaw); request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); request.SetRequestHeader("Authorization", $"Bearer {apiKey}"); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { string responseJson = request.downloadHandler.text; var response = JsonUtility.FromJson<DeepSeekResponse>(responseJson); callback?.Invoke(response.choices.message.content); } else { Debug.LogError($"Error: {request.error}"); callback?.Invoke($"Error: {request.error}"); } } } private class DeepSeekResponse { public Choice[] choices; } private class Choice { public Message message; } private class Message { public string content; }}```
### **2.2 测试 API 调用**
```csharp// 在某个脚本里测试:DeepSeekManager.Instance.SendMessageToDeepSeek("你好,DeepSeek!", (response) => { Debug.Log("AI 回复:" + response);});```
✅ **运行测试**:确保能在 Console 看到 DeepSeek 的回复。
## **3️⃣ 添加语音输入(麦克风识别)**
### **3.1 创建 `VoiceRecognition.cs`**
```csharpusing UnityEngine;using UnityEngine.Windows.Speech;public class VoiceRecognition : MonoBehaviour{ private DictationRecognizer dictationRecognizer; public bool IsListening { get; private set; } private void Start() { dictationRecognizer = new DictationRecognizer(); dictationRecognizer.DictationResult += OnDictationResult; dictationRecognizer.DictationError += OnDictationError; } public void StartListening() { if (!IsListening) { dictationRecognizer.Start(); IsListening = true; Debug.Log("开始语音识别..."); } } public void StopListening() { if (IsListening) { dictationRecognizer.Stop(); IsListening = false; Debug.Log("停止语音识别"); } } private void OnDictationResult(string text, ConfidenceLevel confidence) { Debug.Log("识别结果: " + text); DeepSeekManager.Instance.SendMessageToDeepSeek(text, (response) => { Debug.Log("AI 回复: " + response); TextToSpeechManager.Instance.Speak(response); // 语音播报回复 }); } private void OnDictationError(string error, int result) { Debug.LogError("语音识别错误: " + error); IsListening = false; } private void OnDestroy() { if (dictationRecognizer != null) { dictationRecognizer.Stop(); dictationRecognizer.Dispose(); } }}```### **3.2 测试语音识别**```csharp// 测试代码:VoiceRecognition voiceRecognition = GetComponent<VoiceRecognition>();voiceRecognition.StartListening(); // 开始录音voiceRecognition.StopListening();// 停止录音```
✅ **运行测试**:对着麦克风说话,检查 Unity Console 是否显示识别结果。
## **4️⃣ 添加语音输出(TTS 朗读)**
### **4.1 创建 `TextToSpeechManager.cs`**
```csharpusing UnityEngine;using UnityEngine.Windows.Speech;public class TextToSpeechManager : MonoBehaviour{ public static TextToSpeechManager Instance { get; private set; } private SpeechSynthesizer synthesizer; private AudioSource audioSource; private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); audioSource = gameObject.AddComponent<AudioSource>(); synthesizer = new SpeechSynthesizer(); } else { Destroy(gameObject); } } public void Speak(string text) { if (string.IsNullOrEmpty(text)) return; synthesizer.Speak(text, OnSpeechSynthesized); } private void OnSpeechSynthesized(AudioClip clip) { if (clip != null) { audioSource.clip = clip; audioSource.Play(); } } private void OnDestroy() { if (synthesizer != null) synthesizer.Dispose(); }}```
### **4.2 测试 TTS**
```csharpTextToSpeechManager.Instance.Speak("你好,我是 DeepSeek AI!");```
✅ **运行测试**:应该能听到 AI 的语音回复。
## **5️⃣ 构建 UI 交互界面**
### **5.1 创建 UI 元素**
1. **Canvas**(UI 主画布) - **ChatPanel**(ScrollView,显示对话) - **InputField**(文本输入框) - **SendButton**(发送按钮) - **MicButton**(麦克风按钮) - **ListeningIndicator**("正在聆听..." 提示)
### **5.2 创建 `UIController.cs`**
```csharpusing UnityEngine;using UnityEngine.UI;using TMPro;public class UIController : MonoBehaviour{ public TMP_InputField inputField; public Button sendButton; public Button micButton; public TextMeshProUGUI chatDisplay; public GameObject listeningIndicator; private void Start() { sendButton.onClick.AddListener(OnSendButtonClick); micButton.onClick.AddListener(OnMicButtonClick); } private void OnSendButtonClick() { string message = inputField.text; if (!string.IsNullOrEmpty(message)) { AddMessage("You: " + message); inputField.text = ""; DeepSeekManager.Instance.SendMessageToDeepSeek(message, (response) => { AddMessage("AI: " + response); TextToSpeechManager.Instance.Speak(response); }); } } private void OnMicButtonClick() { if (!VoiceRecognition.Instance.IsListening) { listeningIndicator.SetActive(true); VoiceRecognition.Instance.StartListening(); } else { listeningIndicator.SetActive(false); VoiceRecognition.Instance.StopListening(); } } public void AddMessage(string message) { chatDisplay.text += "\n" + message; // 自动滚动到底部 Canvas.ForceUpdateCanvases(); chatDisplay.GetComponentInParent<ScrollRect>().verticalNormalizedPosition = 0; }}```
✅ **运行测试**:现在可以 **打字发送** 或 **语音输入**,AI 会 **文字+语音回复**!
## **6️⃣ 进阶优化**
### **6.1 多语言支持**
```csharp// 在 TextToSpeechManager 添加:public void SetLanguage(string languageCode){ synthesizer.SetOutputToDefaultAudioDevice(languageCode);}// 在 DeepSeekManager 检测语言:if (message.Contains("你好")) TextToSpeechManager.Instance.SetLanguage("zh-CN");else TextToSpeechManager.Instance.SetLanguage("en-US");```### **6.2 离线语音识别(备用方案)**```csharpusing UnityEngine.Windows.Speech;public class OfflineKeywordDetector : MonoBehaviour{ private KeywordRecognizer keywordRecognizer; private string[] keywords = { "Hey AI", "Start Listening" }; void Start() { keywordRecognizer = new KeywordRecognizer(keywords); keywordRecognizer.OnPhraseRecognized += OnKeywordRecognized; keywordRecognizer.Start(); } private void OnKeywordRecognized(PhraseRecognizedEventArgs args) { Debug.Log("唤醒词识别: " + args.text); VoiceRecognition.Instance.StartListening(); } void OnDestroy() { keywordRecognizer?.Dispose(); }}```
### **6.3 错误处理**
```csharp// 在 DeepSeekManager 的 PostRequest 添加:if (request.result == UnityWebRequest.Result.ConnectionError){ UIController.Instance.AddMessage("网络错误,请检查连接!");}else if (request.responseCode == 401){ UIController.Instance.AddMessage("API Key 无效!");}```
## **7️⃣ 测试与发布**
### **7.1 测试功能**
- **文本聊天** ✅- **语音输入** ✅- **语音输出** ✅- **多语言切换** ✅
### **7.2 发布设置**
- **PC 端**:直接 Build(Windows/macOS)
- **Android/iOS**:需额外配置麦克风权限:
```xml<!-- AndroidManifest.xml --><uses-permission android:name="android.permission.RECORD_AUDIO" />```
- **WebGL**:需使用 `WebMicrophone` 替代 `UnityEngine.Windows.Speech`
## **🎉 完成!**
现在你的 Unity 应用已经集成了 DeepSeek AI + 语音交互,可以扩展成:
- **智能语音助手**
- **AI 客服系统**
- **语音交互游戏 NPC**
如果有问题,欢迎在评论区交流! 🚀
页:
[1]