Unity原生语音识别/无插件/可离线/不需要联网 语音识别

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;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Chj1319261607/article/details/131984072