using TMPro; using Unity.XR.CoreUtils; using UnityEngine; using Whisper; using Whisper.Utils; public class VoiceTranscriptionBox : MonoBehaviour { public Material activeMaterial; public Material inactiveMaterial; private MeshRenderer meshRenderer; public WhisperManager whisper; public MicrophoneRecord microphoneRecord; public TextMeshProUGUI outputText; private WhisperStream stream; private string lastTextOutput; public string LastTextOutput { get { return lastTextOutput; } } // Start is called before the first frame update async void Start() { meshRenderer = GetComponent(); // This causes about 1 sec long freeze, has to be done once at the start of the game microphoneRecord.StartRecord(); stream = await whisper.CreateStream(microphoneRecord); stream.OnResultUpdated += OnWhisperResult; } // Update is called once per frame void Update() { } void OnTriggerEnter(Collider other) { KbmController controller = other.GetComponent(); XROrigin playerOrigin = other.GetComponent(); if (controller != null || playerOrigin != null) { meshRenderer.material = activeMaterial; stream.StartStream(); } } private void OnTriggerExit(Collider other) { KbmController controller = other.GetComponent(); XROrigin playerOrigin = other.GetComponent(); if (controller != null | playerOrigin != null) { stream.StopStream(); meshRenderer.material = inactiveMaterial; } } private void OnWhisperResult(string result) { lastTextOutput = result; outputText.text = result; } private void OnDestroy() { microphoneRecord.StopRecord(); Destroy(gameObject); } }