@@ -13,42 +13,37 @@ public class SpeechInputHandler : MonoBehaviour, ISpeechHandler
1313 [ System . Serializable ]
1414 public struct KeywordAndResponse
1515 {
16- [ Tooltip ( "The keyword to recognize ." ) ]
16+ [ Tooltip ( "The keyword to handle ." ) ]
1717 public string Keyword ;
18- [ Tooltip ( "The UnityEvent to be invoked when the keyword is recognized ." ) ]
18+ [ Tooltip ( "The handler to be invoked." ) ]
1919 public UnityEvent Response ;
2020 }
2121
22- [ Tooltip ( "An array of string keywords and UnityEvents, to be set in the Inspector ." ) ]
22+ [ Tooltip ( "The keywords to be recognized and optional keyboard shortcuts ." ) ]
2323 public KeywordAndResponse [ ] keywords ;
2424
25+ [ NonSerialized ]
2526 private readonly Dictionary < string , UnityEvent > responses = new Dictionary < string , UnityEvent > ( ) ;
2627
2728 // Use this for initialization
2829 protected virtual void Start ( )
2930 {
31+ // Convert the struct array into a dictionary, with the keywords and the methods as the values.
32+ // This helps easily link the keyword recognized to the UnityEvent to be invoked.
3033 int keywordCount = keywords . Length ;
31- if ( keywordCount > 0 )
34+ for ( int index = 0 ; index < keywordCount ; index ++ )
3235 {
33- try
36+ KeywordAndResponse keywordAndResponse = keywords [ index ] ;
37+ string keyword = keywordAndResponse . Keyword . ToLower ( ) ;
38+ if ( responses . ContainsKey ( keyword ) )
3439 {
35- // Convert the struct array into a dictionary, with the keywords and the keys and the methods as the values.
36- // This helps easily link the keyword recognized to the UnityEvent to be invoked.
37- for ( int index = 0 ; index < keywordCount ; index ++ )
38- {
39- KeywordAndResponse keywordAndResponse = keywords [ index ] ;
40- responses [ keywordAndResponse . Keyword . ToLower ( ) ] = keywordAndResponse . Response ;
41- }
40+ Debug . LogError ( "Duplicate keyword '" + keyword + "' specified in '" + gameObject . name + "'." ) ;
4241 }
43- catch ( ArgumentException )
42+ else
4443 {
45- Debug . LogError ( "Duplicate keywords specified in the Inspector on " + gameObject . name + "." ) ;
44+ responses . Add ( keyword , keywordAndResponse . Response ) ;
4645 }
4746 }
48- else
49- {
50- Debug . LogError ( "Must have at least one keyword specified in the Inspector on " + gameObject . name + "." ) ;
51- }
5247 }
5348
5449 void ISpeechHandler . OnSpeechKeywordRecognized ( SpeechKeywordRecognizedEventData eventData )
0 commit comments