### C# Script for Dictation - Unity Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/STT.md A C# script for Unity that utilizes the AppDictationExperience component from Meta's Voice SDK to process speech input. It handles starting and stopping dictation sessions, and logs partial and full transcriptions, as well as any errors encountered. ```csharp using UnityEngine; using Oculus.Voice.Dictation; using Meta.WitAi.Dictation.Data; public class STTScript : MonoBehaviour { private AppDictationExperience dictation; void Start() { dictation = FindFirstObjectByType(); if(dictation == null) { Debug.LogError("AppDictationExperience component not found in the scene."); return; } dictation.DictationEvents.OnPartialTranscription.AddListener(OnPartialTranscription); dictation.DictationEvents.OnFullTranscription.AddListener(OnDictationResult); dictation.DictationEvents.OnDictationSessionStarted.AddListener(OnSessionStarted); dictation.DictationEvents.OnDictationSessionStopped.AddListener(OnSessionStopped); dictation.DictationEvents.OnError.AddListener(OnDictationError); dictation.Activate(); } void OnDestroy() { if (dictation != null) { dictation.Deactivate(); dictation.DictationEvents.OnPartialTranscription.RemoveListener(OnPartialTranscription); dictation.DictationEvents.OnFullTranscription.RemoveListener(OnDictationResult); dictation.DictationEvents.OnDictationSessionStarted.RemoveListener(OnSessionStarted); dictation.DictationEvents.OnDictationSessionStopped.RemoveListener(OnSessionStopped); dictation.DictationEvents.OnError.RemoveListener(OnDictationError); } } private void OnPartialTranscription(string text) { Debug.Log("Partial result: " + text); } private void OnDictationResult(string text) { Debug.Log("Final result: " + text); } private void OnSessionStarted(DictationSession session) { Debug.Log("Dictation started"); } private void OnSessionStopped(DictationSession session) { Debug.Log("Dictation stopped"); } private void OnDictationError(string errorType, string errorMessage) { Debug.LogError($"Dictation Error: {errorType} - {errorMessage}"); } } ``` -------------------------------- ### Send Image Prompts to LLM Agent in Unity Source: https://context7.com/mcgillxr/empty-xr-project/llms.txt Enables multimodal prompts with images using LlmAgent's SendPromptWithImagesAsync. Converts Texture2D assets to ImageInput objects via the ChatImages helper class for AI vision analysis. ```csharp using UnityEngine; using Meta.XR.BuildingBlocks.AIBlocks; using System.Collections.Generic; public class PromptTest : MonoBehaviour { public Texture2D _inputImage; private LlmAgent _llmAgent; void Start() { _llmAgent = FindFirstObjectByType(); _llmAgent.onResponseReceived.AddListener(ProcessResponse); string promptText = "Describe the following image in a sentence. Use 20 words or less."; var images = new List { ChatImages.FromTexture(_inputImage) }; _llmAgent.SendPromptWithImagesAsync(promptText, images); } public void ProcessResponse(string response) { Debug.Log("Received response from LLM Agent."); Debug.Log(response); } } ``` -------------------------------- ### Send Text Prompts to LLM Agent in Unity Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/llm.md Demonstrates how to initialize an LlmAgent and send a text-based prompt asynchronously. It utilizes the Meta.XR.BuildingBlocks.AIBlocks namespace to handle response callbacks. ```csharp using UnityEngine; using Meta.XR.BuildingBlocks.AIBlocks; using System.Collections.Generic; public class PromptTest : MonoBehaviour { private LlmAgent _llmAgent; void Start() { _llmAgent = FindFirstObjectByType(); _llmAgent.onResponseReceived.AddListener(ProcessResponse); _llmAgent.SendPromptAsync("You are a tourist guide who gives interesting facts in 1 or 2 sentences. Tell me an interesting fact about McGill University."); } public void ProcessResponse(string response) { Debug.Log("Received response from LLM Agent."); Debug.Log(response); } } ``` -------------------------------- ### Asynchronous Scene Management in Unity Source: https://context7.com/mcgillxr/empty-xr-project/llms.txt Demonstrates asynchronous scene loading and activation using Unity's SceneManager API. This allows for preloading scenes in the background to ensure smooth transitions, which is crucial for XR experiences. The `allowSceneActivation` property controls when the scene becomes active. ```csharp using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class SceneChange : MonoBehaviour { void Start() { StartCoroutine(ChangeScene()); } IEnumerator ChangeScene() { yield return new WaitForSeconds(3); Scene currentScene = SceneManager.GetActiveScene(); int nextSceneIndex = (currentScene.buildIndex + 1) % SceneManager.sceneCountInBuildSettings; AsyncOperation operation = SceneManager.LoadSceneAsync(nextSceneIndex); operation.allowSceneActivation = false; while(operation.progress < 0.9f) { yield return null; } operation.allowSceneActivation = true; } } ``` -------------------------------- ### Send Text Prompts to LLM Agent in Unity Source: https://context7.com/mcgillxr/empty-xr-project/llms.txt Integrates with large language models (like OpenAI, Llama) using the LlmAgent class. Requires the 'Large Language Model' building block and API key configuration in Unity's Project Settings. ```csharp using UnityEngine; using Meta.XR.BuildingBlocks.AIBlocks; using System.Collections.Generic; public class PromptTest : MonoBehaviour { private LlmAgent _llmAgent; void Start() { _llmAgent = FindFirstObjectByType(); _llmAgent.onResponseReceived.AddListener(ProcessResponse); _llmAgent.SendPromptAsync("You are a tourist guide who gives interesting facts in 1 or 2 sentences. Tell me an interesting fact about McGill University."); } public void ProcessResponse(string response) { Debug.Log("Received response from LLM Agent."); Debug.Log(response); } } ``` -------------------------------- ### Unity C# Scene Transition Script Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/changing_scenes.md This C# script for Unity loads the next scene in the build settings after a specified delay. It uses `SceneManager.LoadSceneAsync` for a smooth transition and wraps around to the first scene if the current scene is the last one. Ensure scenes are added to the Build Settings for this script to function correctly. ```csharp using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; public class SceneChange : MonoBehaviour { void Start() { StartCoroutine(ChangeScene()); } IEnumerator ChangeScene() { yield return new WaitForSeconds(3); Scene currentScene = SceneManager.GetActiveScene(); int nextSceneIndex = (currentScene.buildIndex + 1) % SceneManager.sceneCountInBuildSettings; AsyncOperation operation = SceneManager.LoadSceneAsync(nextSceneIndex); operation.allowSceneActivation = false; while(operation.progress < 0.9f) { yield return null; } operation.allowSceneActivation = true; } } ``` -------------------------------- ### Map Controller Button Presses using OVRInput API in Unity Source: https://context7.com/mcgillxr/empty-xr-project/llms.txt Detects and logs button presses from Meta Quest controllers using the OVRInput API. This script is intended for use within a Unity MonoBehaviour to handle real-time controller input. ```csharp using UnityEngine; public class TestButtonMapping : MonoBehaviour { void Update() { // Left Controller Buttons if (OVRInput.GetDown(OVRInput.RawButton.LHandTrigger)) Debug.Log("Left Grip"); if (OVRInput.GetDown(OVRInput.RawButton.X)) Debug.Log("Left X"); if (OVRInput.GetDown(OVRInput.RawButton.Y)) Debug.Log("Left Y"); if (OVRInput.GetDown(OVRInput.RawButton.LThumbstick)) Debug.Log("Left Stick Press"); if (OVRInput.GetDown(OVRInput.RawButton.LIndexTrigger)) Debug.Log("Left Index Trigger"); if (OVRInput.GetDown(OVRInput.RawButton.Start)) Debug.Log("Menu button"); // Right Controller Buttons if (OVRInput.GetDown(OVRInput.RawButton.RHandTrigger)) Debug.Log("Right Grip"); if (OVRInput.GetDown(OVRInput.RawButton.A)) Debug.Log("Right A"); if (OVRInput.GetDown(OVRInput.RawButton.B)) Debug.Log("Right B"); if (OVRInput.GetDown(OVRInput.RawButton.RThumbstick)) Debug.Log("Right Stick Press"); if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger)) Debug.Log("Right Index Trigger"); } } ``` -------------------------------- ### Send Image Prompts to LLM Agent in Unity Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/llm.md Extends the LLM integration to support image analysis by converting a Texture2D into an ImageInput list. The SendPromptWithImagesAsync method is used to process both text and visual data. ```csharp using UnityEngine; using Meta.XR.BuildingBlocks.AIBlocks; using System.Collections.Generic; public class PromptTest : MonoBehaviour { public Texture2D _inputImage; private LlmAgent _llmAgent; void Start() { _llmAgent = FindFirstObjectByType(); _llmAgent.onResponseReceived.AddListener(ProcessResponse); string promptText = "Describe the following image in a sentence. Use 20 words or less."; var images = new List { ChatImages.FromTexture(_inputImage) }; _llmAgent.SendPromptWithImagesAsync(promptText, images); } public void ProcessResponse(string response) { Debug.Log("Received response from LLM Agent."); Debug.Log(response); } } ``` -------------------------------- ### Accessing Passthrough Camera Feed in Unity Source: https://context7.com/mcgillxr/empty-xr-project/llms.txt Provides access to the Meta Quest camera feed for mixed reality applications using the PassthroughCameraAccess API. It allows fetching the camera texture for GPU rendering or pixel data for CPU analysis. This functionality requires an Android build target. ```csharp using UnityEngine; using Meta.XR; public class PassThroughTest : MonoBehaviour { public GameObject _plane; private PassthroughCameraAccess _passthroughCameraAccess; void Start() { _passthroughCameraAccess = FindFirstObjectByType(); } void Update() { // Check if the camera is playing if (_passthroughCameraAccess != null && _passthroughCameraAccess.IsPlaying) { // Get the texture (GPU access) var cameraTexture = _passthroughCameraAccess.GetTexture(); // To get individual pixel colors (CPU access, can be intensive), uncomment the line below: // NativeArray pixels = _passthroughCameraAccess.GetColors(); // Apply the texture to the plane's material if (cameraTexture != null && _plane != null) { Renderer planeRenderer = _plane.GetComponent(); if (planeRenderer != null) { planeRenderer.material.mainTexture = cameraTexture; } } } } } ``` -------------------------------- ### Map Controller Button Presses with C# Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/controllerMapping.md This script uses the OVRInput API to detect and log presses of various buttons on Meta Quest 3 controllers. It requires the 'Controller Tracking' building block to be added to the scene. The script checks for button down events in the Update loop and logs the corresponding button name to the Unity console. ```csharp using UnityEngine; public class TestButtonMapping : MonoBehaviour { void Update() { // Left Controller Buttons if (OVRInput.GetDown(OVRInput.RawButton.LHandTrigger)) Debug.Log("Left Grip"); if (OVRInput.GetDown(OVRInput.RawButton.X)) Debug.Log("Left X"); if (OVRInput.GetDown(OVRInput.RawButton.Y)) Debug.Log("Left Y"); if (OVRInput.GetDown(OVRInput.RawButton.LThumbstick)) Debug.Log("Left Stick Press"); if (OVRInput.GetDown(OVRInput.RawButton.LIndexTrigger)) Debug.Log("Left Index Trigger"); if (OVRInput.GetDown(OVRInput.RawButton.Start)) Debug.Log("Menu button"); // Right Controller Buttons if (OVRInput.GetDown(OVRInput.RawButton.RHandTrigger)) Debug.Log("Right Grip"); if (OVRInput.GetDown(OVRInput.RawButton.A)) Debug.Log("Right A"); if (OVRInput.GetDown(OVRInput.RawButton.B)) Debug.Log("Right B"); if (OVRInput.GetDown(OVRInput.RawButton.RThumbstick)) Debug.Log("Right Stick Press"); if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger)) Debug.Log("Right Index Trigger"); } } ``` -------------------------------- ### Triggering Text to Speech via C# in Unity Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/TTS.md This script demonstrates how to programmatically trigger the TTSSpeaker component to read text aloud. It uses a Coroutine to introduce a delay before invoking the Speak method on the assigned speaker object. ```csharp using UnityEngine; using System.Collections; using Meta.WitAi.TTS.Utilities; public class SpeakerScript : MonoBehaviour { public TTSSpeaker speaker; void Start() { StartCoroutine(SpeakTest()); } IEnumerator SpeakTest() { yield return new WaitForSeconds(2f); speaker.Speak("Hello, this is a test of the text to speech system."); } } ``` -------------------------------- ### Display Passthrough Camera Feed on a 3D Plane (C#) Source: https://github.com/mcgillxr/empty-xr-project/blob/main/doc/tutorials/pca.md This C# script retrieves the camera texture from the PassthroughCameraAccess API and applies it to the material of a specified 3D plane. It requires the Unity game engine and the Meta XR SDK. The script checks if the camera is playing and updates the plane's texture in real-time. ```csharp using UnityEngine; using Meta.XR; public class PassThroughTest : MonoBehaviour { public GameObject _plane; private PassthroughCameraAccess _passthroughCameraAccess; void Start() { _passthroughCameraAccess = FindFirstObjectByType(); } void Update() { // Check if the camera is playing if (_passthroughCameraAccess != null && _passthroughCameraAccess.IsPlaying) { // Get the texture (GPU access) var cameraTexture = _passthroughCameraAccess.GetTexture(); // To get individual pixel colors (CPU access, can be intensive), uncomment the line below: // NativeArray pixels = _passthroughCameraAccess.GetColors(); // Apply the texture to the plane's material if (cameraTexture != null && _plane != null) { Renderer planeRenderer = _plane.GetComponent(); if (planeRenderer != null) { planeRenderer.material.mainTexture = cameraTexture; } } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.