iOSTTS is a plugin for Unity, which allows you to synthesize speech from a given text and listen to it on your iOS device (iPhone, iPad). This can be useful when creating monologues of characters, descriptions, instructions, actions, etc. in Unity-based games and applications.
The plugin works by using a built-in iOS class AVSpeechSynthesizer. This class produces synthesized speech from text on an iOS device, and provides methods for controlling or monitoring the progress of ongoing speech.
As an added bonus, allows you to debug an application in the process of development, while remaining within the Unity editor, we have developed a plug-in for Mac OS computers - OSXTTS.
1. Download iOSTTS of the Unity Asset Store.
2. Import iOS_TextToSpeech.cs and AnimateTextAttribute.cs in the Plugins directory (create the Plugins folder if necessary).
3. Import the files iOSTTS.h and iOSTTS.m in the Plugins / iOS directory (if iOS subdirectory not exist, create it).
4. Identify the object in your project, which will be carried out for speech synthesis, and in the corresponding script, add the following code:
5. Build an iOS project, then use Xcode to run the project on your iOS device and listen to synthesized speech with default language, speech rate, pitch and volume.
To pause, use the function:
To continue (after a pause), use the function:
To stop, use the function:
To get a list of all supported languages in the format "English United States", "French France", etc. use the function:
To get a list of all supported BCP-47 codes in the format "en-US", "fr-FR" etc. use the function:
For greater convenience provided special class "TTS_Languages" that contains a list of all supported BCP-47 codes, and all supported languages. For more details, see using of this class in "SettingsManager" of "iOS TTS Demo". For TTS_Languages use the function:
To set the language for the synthesis by its name (for example, "English United States"), use the function:
To set the language for the synthesis by its BCP-47 code (eg, "en-US"), use the function:
To get current language, use the function:
string currentLanguage = iOS_TextToSpeech .GetCurrentLanguage();
//Returns for example, "English United States"
To get current BCP-47 code, use the function:
string currentLanguageCode = iOS_TextToSpeech .GetCurrentLanguageCode();
//Returns for example, "en-US"
To set the sound volume, use the function:
//The value must be between 0.0f and 1.0f
float volume = 0.8f;iOS_TextToSpeech .SetVolume(volume));
To change the pitch, use the function:
//The value must be between 0.5f and 2.0f
float pitch = 1.4f;iOS_TextToSpeech .SetPitch(pitch));
To change the speech rate, use the function:
//The value must be between iOS MinimumSpeechRate (0.0f by default) and iOS MaximumSpeechRate (1.0f by default)
float speechRate = 0.5f;iOS_TextToSpeech .SetSpeechRate(speechRate));
To get the minimum speech rate, use the function:
float minimumSpeechRate = iOS_TextToSpeech .GetMinimumSpeechRate();
//Returns 0.0f by default
To get the maximum speech rate, use the function:
float maximumSpeechRate = iOS_TextToSpeech .GetMaximumSpeechRate();
//Returns 1.0f by default
Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage :
This function has three parameters : the name of the target GameObject, the script method to call on that object and the message string to pass to the called method.
iOS TTS provides this functionality, thus:
Note: This message also comes when you call the function:
All parameters can be changed by calling the appropriate functions:
The most important and most commonly used is a function to change the target GameObject name:
//Set the parent GameObject name (default value - "GameControl")
string gameObjectName = gameObject.name;
To assign a new method name in the called script, use the function:
//Change callback method name (default value - "DidFinishSpeak")
string methodName = "SuperMethod" ;
To change the message about the end of a word pronunciation, use the function:
//Change message about the end of a word pronunciation - (default value - "Word")
string wordParameterName = "DidFinishWord" ;
To change the message about the end of an utterance pronunciation, use the function:
//Change message about the end of an utterance pronunciation - (default value - "Utterance")
string utteranceParameterName = "DidFinishUtterance" ;
To change the message about the end of the all text pronunciation, use the function:
//Change message about the end of the all text pronunciation - (default value - "Text")
string textParameterName = "DidFinishText" ;
To support this effect iOSTTS provides callback function that can send messages of the completion of pronunciation of words, phrases, and all the text. To use it, you must:
1. Define or create in the current scene empty Game Object, which will control this scene (GameManager, SceneManager etc. In the demo project this is "DemoManager").
2. Add to assigned to the object script the following variables:
3. Add to the script two lines:
4. In the Inspector Window, assign for variable "Info" the text object (for which the words will be highlighted):
5. Add to the script:
6. Build iOS project using Xcode and then deploy the project on your iOS device and listen to synthesized speech, and the spoken words will be highlighted. Do not forget to call:
Note: the following "auto-scroll text example" is not a complete solution and is given merely as one of the possible options.
1. Use prepared prefab called "Scrolled Text" (is a part of the demo project), which includes the Unity UI Mask and Unity UI Text. In addition, you must add the Unity UI Scrollbar and associate it with prefab.
2. Add to the script the following variables:
3. In the Inspector Window, assign object Scrollbar to variable "Scrollbar":
4. Add to the script:
5. Make changes to the function Did Finish Speak:
6. Build iOS project using Xcode, deploy the project on your iOS device and listen to synthesized speech. The spoken words will be highlighted, and the text in the window will scroll. Do not forget to call:
Look at the use of iOS TTS plugin in the demo project:
OSX TTS allows you to customize the application using speech synthesis for iOS devices, while remaining within the Unity editor.
Features OSX TTS almost completely coincide with iOS TTS capabilities, with a few exceptions and modifications:
To connect to OS X TTS, you must add to the project OSX TTS plugin and OSX_TextToSpeech.cs file:
In OSX TTS uses the same function as in the iOS TTS, but it is necessary to carry out additional checks. For example, call for speech synthesis:
To configure callback function, use the following lines of code:
Examples of how to use OS X TTS and iOS TTS can be found in the demo project.