Launch Game with Cortana


With cortana skill, we can launch app or game, so i decided to use START RACE command to open my game - Cortana Rally Driver.

First i created interop class in unity and declared static variable to store cortana speech recognised text.

using System;
using UnityEngine;
public class CortanaInterop : MonoBehaviour {

    public static string CortanaText;


   


    // Use this for initialization
    void Start () {

       
    }

 // Update is called once per frame
 void Update () {

 }
}


Export unity project to universal 10 platform. Note to select project type as XAML. If you want exported project in C#, change scripting backend to .NET. Also ensure internet client and microphone is selected in player settings as it is important for cortana to work.


Once the project is built, open the exported project in Visual Studio and install Voice Definition File.
First, create a new XML document in your solution (Right click -> Add new Item -> XML File) and then adding basic VCD content to the new XML file, I created a VoiceCommandDefinition.xml file as follows:

<?xml version="1.0" encoding="utf-8"?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
 <CommandSet xml:lang="en-us">
      <AppName>Start Race</AppName>
      <Example>Start Cortana Rally Driver</Example>
      <Command Name="LaunchText">
        <Example>Give instructions to Driver</Example>
        <ListenFor RequireAppName="BeforePhrase">{gamelaunchtext}</ListenFor>
        <Feedback> Starting Race... </Feedback>
        <Navigate/>
      </Command>
    <PhraseTopic Label="gamelaunchtext" Scenario="Natural Language"></PhraseTopic>
  </CommandSet>

</VoiceCommands >

To install VCD, open App.xaml.CS file that is in project, locate the OnLaunched method and insert the following lines:

protected override async void OnLaunched(LaunchActivatedEventArgs args)
        {
           
          
            try
            {
                Windows.Storage.StorageFile vcdStorageFile = await
                Package.Current.InstalledLocation.GetFileAsync
                    (@"VoiceCommandDefinition.xml");

                await Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager
                .InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
               

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine
                ("Installing Voice Commands Failed: " + ex.ToString());
            }
          
            splashScreen = args.SplashScreen;
            InitializeUnity(args.Arguments);

        }

Next I created another new C# script called SpeechHelper in the Windows project to contain all the code required for the speech integration. Once you have created the script, replace its contents with the following:




public class SpeechHelper
    {
        private static Windows.Media.SpeechRecognition.SpeechRecognizer speechRecognizer;
        private static IAsyncOperation<Windows.Media.SpeechRecognition.
            SpeechRecognitionResult> recognitionOperation;

        public static void HandleSpeechCommand(IActivatedEventArgs args)
        {

        var commandArgs = args as
          Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs;
            Windows.Media.SpeechRecognition.SpeechRecognitionResult
          speechRecognitionResult = commandArgs.Result;

            string textSpoken = speechRecognitionResult.Text;
            CortanaInterop.CortanaText = textSpoken;
        }

}


To register call handler on startup, return to App.xaml.cs file and locate the OnActivated method. In this method Unity already checks to see if a Unity app was launched via a custom URL (or protocol). To this we need to add a case for our VoiceCommand start method, as follows:

 protected override void OnActivated(IActivatedEventArgs args)
        {
            string appArgs = "";

            switch (args.Kind)
            {
                case ActivationKind.Protocol:
                    ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
                    splashScreen = eventArgs.SplashScreen;
                    appArgs += string.Format("Uri={0}", eventArgs.Uri.AbsoluteUri);
                    break;

                //Start VoiceCommand detection & use the SpeechHelper handler
                case ActivationKind.VoiceCommand:
                   SpeechHelper.HandleSpeechCommand(args);
                    break;
            }
            InitializeUnity(appArgs);
        }




Now, if i run this project in real device with Cortana, VCD get installed to device. So now START RACE command can be used to launch game using Cortana.

Comments

Popular Posts