Hey! Hope you’re having an awesome Spring so far. I have been so busy with a lot of topics to explore especially with the release of the RS4 Preview Build and many more which are going to go on my backlog, for more tutorials!
However, in this article, I focus on how to use TextToSpeech in your Hololens apps. This was an article request and sorry that it is coming in later than I expected (heavy workload!)
This is going to be a real quick tutorial, given the ease of using the TextToSpeech.cs script (initially contributed to the HoloToolkit by Jared Bienz, thanks for that!)
So let’s get started…
1. Create a new Unity project
2. Delete the main camera
3. Import the latest MR Toolkit (according to the version of Unity on your system)
4. Add the MixedRealityCameraParent.prefab in the Hierarchy. This will serve as the Main Camera
5. Change the Camera Settings to Hololens preferred:
Skybox to Solid Color
Background to 0,0,0,0
Save the scene.
Adding the TextToSpeech script from the HoloToolkit
So the HoloToolkit comes with the TextToSpeech.cs script already. But first, we will create a GameObject in our scene, which when we tap on, will say the words we ask it to say.
So first right click on the Hierarchy and add a new GameObject. For this example, I am adding a Cube. Position it such that the MR camera can see it.
Now click on the Cube and in it’s Inspector, search for TextToSpeech.cs and add it.
The script automatically adds on an Audio Source to the Cube. So later, when you tap on it, you can feel that the sound is coming from that particular object. This will be especially useful when there are many objects in the screen and you can attach the TextToSpeech.cs to each of them. This will take advantage of the spatial sounds in the HoloLens and it will seem like the sounds originate from these objects.
The TextToSpeech script has two inspector variables:
Audio Source is where you will drag and drop the Cube object (or the object which you want to speak in your project)
The Voice variable comes with 4 options. Default, David, Mark, Zira. You can choose whichever voice option you want.
Adding our Logic to call the TextToSpeech script
Now we will add our script to call the TextToSpeech script to make our Cube speak.
On the Cube, click on ‘Add Component‘ and create a New Script. Call it TextToSpeechLogic or something relevant
Copy the code which is below onto the script. This will create an Inspector variable called ‘Speak Text’ under the TextToSpeechLogic script
Enter the text that you want your GameObject to speak. It also takes in longer sentences (I have not checked the limit of this)
Now we need to add a Cursor to the scene and the InputManager.
From the Project window, search for DefaultCursor.prefab and drag and drop it to the scene.
Similarly, search for the InputManager.prefab and drag and drop it to the scene.
In the InputManager Inspector settings, under SimpleSinglePointerSelector Cursor variable, drag and drop the DefaultCursor
That’s it. Now save and build and deploy.
You can right click on the cube when it appears on the Emulator, and the cube will speak out your text.
Below is a video of the expected output:
Combine this with the Dictation tutorial and you can get the object to have a complete dialog with you 😉
P.S: Thank you for the article request, Jonathan!
Hi Nischita,
Thank for the article, I just wonder if you ever try to do the same thing with latest MRTL version 2.0-Beta2 mentioned here https://github.com/Microsoft/MixedRealityToolkit-Unity/releases or download here
https://github.com/Microsoft/MixedRealityToolkit-Unity/releases/download/2.0.0-Beta2-ReleaseCandidate/Microsoft.MixedReality.Toolkit.Foundation-v2.0.0-Beta2RC.unitypackage
It doesn’t have TextToSpeechManager class anymore.
Hey TLE, Thanks for the feedback. I usually don’t work with Beta releases as they are unstable. Interesting, if they removed TextToSpeechManager class, maybe there is another alternative… called something else maybe…
No they said they are port over yet! They are doing it right now under this issue https://github.com/Microsoft/MixedRealityToolkit-Unity/issues/3569
Thank link to TextToSpeech.cs in your post is also not found.
Ah! thanks for pointing it out. I have to follow this up closely… and update the article as well, eventually
In text to speech can we able to use different languages like Chinese, japanese korean etc.
Hi, looks like you can but you need to work with localization package from Unity. Here is an article on how to do that: https://localjoost.github.io/Getting-the-Unity-Localization-Package-to-work-with-HoloLens/