Unity原生语音识别/无插件/可离线/不需要联网 语音识别
- 直接上代码,保证自己的设备连接了麦克风,之后把下面代码直接挂在场景的空物体上,运行即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
//using UnityTools;
/// 语音识别
public class SpeechRecognition : MonoBehaviour
{
// 短语识别器
private PhraseRecognizer m_PhraseRecognizer;
// 关键字
public string[] keywords = { };
// 可信度
public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;
void Start()
{
if (m_PhraseRecognizer == null)
{
//创建一个识别器
m_PhraseRecognizer = new KeywordRecognizer(keywords, m_confidenceLevel);
//通过注册监听的方法
m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;
//开启识别器
m_PhraseRecognizer.Start();
}
}
/// 当识别到关键字时,会调用这个方法
private void M_PhraseRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
_SpeechRecognition(args.text);
print(args.text);
}
private void OnDestroy()
{
//判断场景中是否存在语音识别器,如果有,释放
if (m_PhraseRecognizer != null)
m_PhraseRecognizer.Dispose();
}
/// 识别到语音的操作
void _SpeechRecognition(string msg)
{
switch (msg)
{
case "你好你好":
Debug.Log("我在,你说");
break;
case "XXXXX":
Debug.Log("XXXXX");
break;
default:
break;
}
}
}
文章来源地址https://www.toymoban.com/news/detail-773795.html
文章来源:https://www.toymoban.com/news/detail-773795.html
到了这里,关于Unity原生语音识别/无插件/可离线/不需要联网 语音识别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!