Skip to main content
Version: 12 Dec 2024

Runtime Configuration

This section provides details on how developers can create and register voice commands dynamically 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).

Pre-Build Configuration

  • Id
    • Must be a Unique integer.
  • Value
    • This is the voice command that will be spoken. There are no limitations on the length of this phrase currently. It should be specific enough to accomplish what is needed, but it is recommended that it is not so long that it is cumbersome to say.
    • The value may contain an or operator, |, to indicate multiple phrases that will trigger the same event. For example, in the image below, both Hello Friend | Hello Buddy phrases will trigger the 3rd command.
Voice Intents configuration data
tip

You can choose to directly add voice commands through code instead of in the inspector.

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);
}
}