Runtime Voice Intents Example
This section provides details on how developers can create voice commands at runtime. This feature can be helpful when loading content dynamically.
caution
This feature requires the VOICE_INPUT permission to be requested at runtime and enabled in your project's Manifest Settings (Edit > Project Settings > Magic Leap > Manifest Settings).
Example
Creates a custom MLVoiceIntentsConfiguration on Start. Then subscribes to the voice event and logs a message when a command was detected.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using MagicLeap.Android;
public class Example : MonoBehaviour
{
    // The commands that will be added to the voice configuration
    public string[] VoiceCommands = new string[] { "Select", "Change Color", "Undo", "Slot Test for {Color}"};
    // The commands that will be added to the voice configuration
    public MLVoiceIntentsConfiguration.SlotData slotData  = new MLVoiceIntentsConfiguration.SlotData
    {
        name = "Color",
        values = new List<string> { "Red", "Blue", "Green" }
    };
    
    // Voice Configuration that will be created at runtime.
    private MLVoiceIntentsConfiguration _voiceConfiguration;
    // Start is called before the first frame update
    void Start()
    {
        // Request necessary permissions
        Permissions.RequestPermission(Permissions.VoiceInput, OnPermissionGranted, OnPermissionDenied);
    }
    
    // Initialize Voice Intents When Permissions are Granted
    private void OnPermissionGranted(string permission)
    {
        // Make sure the intents only register if the permission is VoiceInput
        if(!permission.Equals(Permissions.VoiceInput))
            return;
        
        // Create a new instance of the Voice Intents Configuration
        _voiceConfiguration = ScriptableObject.CreateInstance<MLVoiceIntentsConfiguration>();
        // Initialize all lists and properties of the configuration
        InitializeVoiceConfiguration();
        // Add slot for voice commands
        _voiceConfiguration.SlotsForVoiceCommands.Add(slotData);
        // Add voice commands to the configuration
        AddVoiceCommands();
        // Configure the voice intents based on the new configuration
        MLResult result = MLVoice.SetupVoiceIntents(_voiceConfiguration);
        if (result.IsOk)
        {
            MLVoice.OnVoiceEvent += OnVoiceEvent;
        }
        else
        {
            Debug.LogError("Failed to Setup Voice Intents with result: " + result);
        }
    }
    
    private void OnPermissionDenied(string permission)
    {
        Debug.LogError($"{permission} denied. Eye tracking data will not be available.");
    }
    // Initialize all lists and properties of the voice configuration
    private void InitializeVoiceConfiguration()
    {
        _voiceConfiguration.VoiceCommandsToAdd = new List<MLVoiceIntentsConfiguration.CustomVoiceIntents>();
        _voiceConfiguration.AllVoiceIntents = new List<MLVoiceIntentsConfiguration.JSONData>();
        _voiceConfiguration.SlotsForVoiceCommands = new List<MLVoiceIntentsConfiguration.SlotData>();
        _voiceConfiguration.SystemCommands = 0; // Initialize with no system commands enabled
        _voiceConfiguration.AutoAllowAllSystemIntents = false; // Default to not auto-allowing all system intents
    }
    // Add voice commands from the array to the configuration
    private void AddVoiceCommands()
    {
        for (int i = 0; i < VoiceCommands.Length; i++)
        {
            MLVoiceIntentsConfiguration.CustomVoiceIntents newIntent = new MLVoiceIntentsConfiguration.CustomVoiceIntents
            {
                Value = VoiceCommands[i],
                Id = (uint)i
            };
            _voiceConfiguration.VoiceCommandsToAdd.Add(newIntent);
        }
    }
    // Write a message to the log when a command is detected
    private void OnVoiceEvent(in bool wasSuccessful, in MLVoice.IntentEvent voiceEvent)
    {
        Debug.Log("Detected Voice Command : " + voiceEvent.EventName);
    }
}