AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SpeechInputHandler.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using UnityEngine;
7 using UnityEngine.Events;
8 
9 namespace HoloToolkit.Unity.InputModule
10 {
11  public class SpeechInputHandler : MonoBehaviour, ISpeechHandler
12  {
13  [Serializable]
14  public struct KeywordAndResponse
15  {
16  [Tooltip("The keyword to handle.")]
17  public string Keyword;
18 
19  [Tooltip("The handler to be invoked.")]
20  public UnityEvent Response;
21  }
22 
26  [Tooltip("The keywords to be recognized and optional keyboard shortcuts.")]
28 
32  [Tooltip("Determines if this handler is a global listener, not connected to a specific GameObject.")]
33  public bool IsGlobalListener;
34 
38  [Tooltip("Keywords are persistent across all scenes. This Speech Input Handler instance will not be destroyed when loading a new scene.")]
39  public bool PersistentKeywords;
40 
41  [NonSerialized]
42  private readonly Dictionary<string, UnityEvent> responses = new Dictionary<string, UnityEvent>();
43 
44  protected virtual void OnEnable()
45  {
46  if (IsGlobalListener)
47  {
48  InputManager.Instance.AddGlobalListener(gameObject);
49  }
50  }
51 
52  protected virtual void Start()
53  {
54  if (PersistentKeywords)
55  {
56  gameObject.DontDestroyOnLoad();
57  }
58 
59  // Convert the struct array into a dictionary, with the keywords and the methods as the values.
60  // This helps easily link the keyword recognized to the UnityEvent to be invoked.
61  int keywordCount = Keywords.Length;
62  for (int index = 0; index < keywordCount; index++)
63  {
64  KeywordAndResponse keywordAndResponse = Keywords[index];
65  string keyword = keywordAndResponse.Keyword.ToLower().TrimEnd();
66 
67  if (responses.ContainsKey(keyword))
68  {
69  Debug.LogError("Duplicate keyword '" + keyword + "' specified in '" + gameObject.name + "'.");
70  }
71  else
72  {
73  responses.Add(keyword, keywordAndResponse.Response);
74  }
75  }
76  }
77 
78  protected virtual void OnDisable()
79  {
80  if (InputManager.Instance != null && IsGlobalListener)
81  {
82  InputManager.Instance.RemoveGlobalListener(gameObject);
83  }
84  }
85 
86  protected virtual void OnDestroy()
87  {
88  if (InputManager.Instance != null && IsGlobalListener)
89  {
90  InputManager.Instance.RemoveGlobalListener(gameObject);
91  }
92  }
93 
95  {
96  UnityEvent keywordResponse;
97 
98  // Check to make sure the recognized keyword exists in the methods dictionary, then invoke the corresponding method.
99  if (enabled && responses.TryGetValue(eventData.RecognizedText.ToLower(), out keywordResponse))
100  {
101  keywordResponse.Invoke();
102  }
103  }
104  }
105 }
KeywordAndResponse [] Keywords
The keywords to be recognized and optional keyboard shortcuts.
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
void OnSpeechKeywordRecognized(SpeechEventData eventData)
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
bool IsGlobalListener
Determines if this handler is a global listener, not connected to a specific GameObject.
string RecognizedText
The text that was recognized.
Describes an input event that involves keyword recognition.
Interface to implement to react to speech recognition.
bool PersistentKeywords
Keywords are persistent across all scenes. This Speech Input Source instance will not be destroyed wh...