前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity Hololens2开发|(五)MRTK3子系统 KeywordRecognitionSubsystem(关键字识别)

Unity Hololens2开发|(五)MRTK3子系统 KeywordRecognitionSubsystem(关键字识别)

作者头像
游戏开发小Y
发布2024-01-18 19:09:09
1780
发布2024-01-18 19:09:09
举报
文章被收录于专栏:Unity开发系列专栏

1.前言

核心定义包附带有 KeywordRecognitionSubsystem,它是 MRTKSubsystemIKeywordRecognitionSubsystem 的基本实现,作为负责 MRTK3 中的关键字/短语识别的子系统的基础。 MRTK 附带的具体实现(例如 WindowsKeywordRecognitionSubsystem),以及你可能构建的其他潜在短语识别子系统,都应该基于此类。 继承自 KeywordRecognitionSubsystem 的子系统可以使用 SpeechInteractor 基于可交互对象的设置触发选择事件 StatefulInteractable’s 。 继承的子类还允许将任意UnityAction’s注册到所选关键字 (keyword) ,以便在说出此类字词时调用操作。

2.设置

2.1 启用语音子系统
  • 转到“project Setting”>“MRTK3”>“KeywordRecognitionSubsystem”,启用语音子系统,如下图:
1
1
2.2 确保 MRTK Speech GameObject 处于活动状态
  • 确保“MRTK XR Rig”->“MRTK Speech”处于活动状态并且附加的脚本已启用。如下图:
2
2
2.3 确保在“播放器设置”中设置适当的功能
  • 要在 UWP 平台上使用 WindowsKeywordRecognitionSubsystem,请转到“project Setting”>“Player”>“Capabilities”,并确保开启“Microphone”功能。如下图:
3
3

3.使用关键字

3.1 KeywordRecognitionSubsystem 与 StatefulInteractable一起使用
  • 使用 KeywordRecognitionSubsystem 的最简单方法是将其与 StatefulInteractable 一起使用。 如果执行了设置部分下的步骤 2.2,则 SpeechInteractor 将在满足针对此类可交互对象指定的条件时触发 StatefulInteractable 上的选择事件(例如,听到指定的短语并且可交互对象被凝视悬停)
4
4
3.2 手动使用 KeywordRecognitionSubsystem
  • KeywordRecognitionSubsystem另一种方法是手动注册关键字 (keyword) ,并在UnityAction子系统听到关键字 (keyword) 时调用。
代码语言:javascript
复制
var keywordRecognitionSubsystem = XRSubsystemHelpers.GetFirstRunningSubsystem<KeywordRecognitionSubsystem>();

if (keywordRecognitionSubsystem != null)
{
    keywordRecognitionSubsystem.CreateOrGetEventForKeyword("your keyword").AddListener(() => Debug.Log("Keyword recognized"));
}
  • 通过声明 Dictionary<string, UnityEvent> 来设定语音命令的关键字和对应行为,并由这个注册 Dictionary 来初始化 KeywordRecognizer ,编写行为动作函数。当用户说出关键词时,预设的动作就会被调用,从而实现语音命令的功能。
代码语言:javascript
复制
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Events;
using UnityEngine.Windows.Speech;
using UnityEngine;
using MixedReality.Toolkit.Subsystems;
using MixedReality.Toolkit;

public class KeywordManager : MonoBehaviour
{
    //将此结构体暴露在Inspector面板中实现语音关键词和动作的添加
    [System.Serializable]
    public struct KeywordAndResponse
    {
        [Tooltip("关键字")]
        public string Keyword;
        [Tooltip("识别调用的UnityEvent")]
        public UnityEvent Response;
    }

    public KeywordAndResponse[] KeywordsAndResponses;
    private Dictionary<string, UnityEvent> responses;

    void Start()
    {
        if (KeywordsAndResponses.Length > 0)
        {
            //将struct数组转换为字典,将struct中的Keyword转换为字典的关键字,UnityEven转换为字典的方法
            responses = KeywordsAndResponses.ToDictionary(keywordAndResponse => keywordAndResponse.Keyword,
                                                          keywordAndResponse => keywordAndResponse.Response);

            var keywordRecognitionSubsystem = XRSubsystemHelpers.GetFirstRunningSubsystem<KeywordRecognitionSubsystem>();
            int Length = KeywordsAndResponses.Length;
            for (int i = 0; i < Length; i++)
            {
                if (keywordRecognitionSubsystem != null)
                {
                    //在子系统中注册一个关键字及其相关的操作
                    string Keyword = KeywordsAndResponses[i].Keyword;
                    keywordRecognitionSubsystem.CreateOrGetEventForKeyword(Keyword).AddListener
                        (() => { KeywordRecognizer_OnPhraseRecognized(Keyword); });
                }
            }
        }
        else
        {
            Debug.LogError("必须至少指定一个关键字 " + gameObject.name + ".");
        }
    }

    //识别关键字之后的回调
    private void KeywordRecognizer_OnPhraseRecognized(string Keyword)
    {
        // 如果识别到关键词就调用
        if (responses.TryGetValue(Keyword, out UnityEvent keywordResponse))
        {
            keywordResponse.Invoke();
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.前言
  • 2.设置
    • 2.1 启用语音子系统
      • 2.2 确保 MRTK Speech GameObject 处于活动状态
        • 2.3 确保在“播放器设置”中设置适当的功能
        • 3.使用关键字
          • 3.1 KeywordRecognitionSubsystem 与 StatefulInteractable一起使用
            • 3.2 手动使用 KeywordRecognitionSubsystem
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档