(UnityEditor Tool) 语音控制unity editor暂停和播放

在Windows平台上可以通过KeywordRecognizer识别简单的语音关键字,我们可以通过麦克风控制编辑器的开启、暂停、关闭
只是一个有趣的小实验
 1 using System;
 2 using UnityEngine;
 3 using UnityEngine.Windows.Speech;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using UnityEditor;
 7 
 8 namespace Game.Editor
 9 {
10     public class EditorVoiceKeywordRecognition : EditorWindow
11     {
12         private  KeywordRecognizer _keywordRecognizer;
13         private readonly Dictionary<string, System.Action> _keywords;
14 
15         [MenuItem("Tools/Custom/Voice Control")]
16         public static void ShowWindow()
17         {
18             EditorWindow.GetWindow(typeof(EditorVoiceKeywordRecognition), false, "Voice Control");
19         }
20 
21         private EditorVoiceKeywordRecognition()
22         {
23             _keywords = new Dictionary<string, System.Action>
24             {
25                 {
26                     "pause", () =>
27                     {
28                         EditorApplication.isPaused = !EditorApplication.isPaused;
29                     }
30                 },
31                 {
32                     "stop", () =>
33                     {
34                         EditorApplication.isPlaying = false;
35                     }
36                 },
37 
38                 {
39                     "play", () =>
40                     {
41                         EditorApplication.isPlaying = true;
42                     }
43                 }
44             };
45         }
46 
47         private void Awake()
48         {
49             _keywordRecognizer = new KeywordRecognizer(_keywords.Keys.ToArray());
50             _keywordRecognizer.OnPhraseRecognized += OnKeywordsRecognized;
51             _keywordRecognizer.Start();
52             Debug.Log("Keyword Recognizer Started");
53         }
54 
55         private void OnDestroy()
56         {
57             if (_keywordRecognizer != null)
58             {
59                 _keywordRecognizer.Stop();
60                 _keywordRecognizer.Dispose();
61                 Debug.Log("Keyword Recognizer Stopped");
62             }
63         }
64 
65         private void OnGUI()
66         {
67             GUILayout.Label("Voice Control", EditorStyles.boldLabel);
68             if (_keywordRecognizer != null && _keywordRecognizer.IsRunning)
69             {
70                 GUILayout.Label("Voice Recognition: Running");
71             }
72             else
73             {
74                 GUILayout.Label("Voice Recognition: Stopped");
75             }
76 
77             if (GUILayout.Button("Restart Recognizer"))
78             {
79                 _keywordRecognizer.Stop();
80                 _keywordRecognizer.Start();
81             }
82         }
83 
84         private void OnKeywordsRecognized(PhraseRecognizedEventArgs args)
85         {
86             Debug.Log($"Recognized Keyword: {args.text}");
87             if (_keywords.TryGetValue(args.text, out Action keyword))
88             {
89                 keyword?.Invoke();
90             }
91         }
92     }
93 }

 

posted @ 2025-04-16 15:23  sun_dust_shadow  阅读(19)  评论(0)    收藏  举报