该脚本挂在摄像机上

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.Speech; //关键字识别的API

public class SpeechManagerrr : MonoBehaviour {
    KeywordRecognizer recognizer;  //能识别关键字S
    //存放关键字的字典
    private Dictionary<string, System.Action> keyWord = new Dictionary<string, System.Action>();
void Start () {
        keyWord.Add("Hi", () => { FindObjectOfType<Cbuerrr>().OnTapped(); });
        //初始化关键字识别器
        recognizer = new KeywordRecognizer(keyWord.Keys.ToArray());
        recognizer.OnPhraseRecognized += Recognizer_OnPhraseRecognized;
        recognizer.Start();
    }
    private void Recognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        System.Action action;
        if (keyWord.TryGetValue(args.text, out action))
        {
            action();
        }
    }
}

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐