Windows

MEADOWSPhone

//Показываем

Unity Assets Store

iOS Text To Speech (iOSTTS) - placed in Unity Assets Store


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.

iOSTTS allows:

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.

  • Arabic (Saudi Arabia) (ar-SA)
  • Czech (Czech Republic) (cs-CZ)
  • Danish (Denmark) (da-DK)
  • German (Germany) (de-DE)
  • Greek (Greece) (el-GR)
  • English (Australia) (en-AU)
  • English (United Kingdom) (en-GB)
  • English (Ireland) (en-IE)
  • English (United States) (en-US)
  • English (United States) (en-US)
  • English (South Africa) (en-ZA)
  • Spanish (Spain) (es-ES)
  • Spanish (Mexico) (es-MX)
  • Finnish (Finland) (fi-FI)
  • French (Canada) (fr-CA)
  • French (France) (fr-FR)
  • Hebrew (Israel) (he-IL)
  • Hindi (India) (hi-IN)
  • Hungarian (Hungary) (hu-HU)
  • Indonesian (Indonesia) (id-ID)
  • Italian (Italy) (it-IT)
  • Japanese (Japan) (ja-JP)
  • Korean (South Korea) (ko-KR)
  • Dutch (Belgium) (nl-BE)
  • Dutch (Netherlands) (nl-NL)
  • Norwegian (Norway) (no-NO)
  • Polish (Poland) (pl-PL)
  • Portuguese (Brazil) (pt-BR)
  • Portuguese (Portugal) (pt-PT)
  • Romanian (Romania) (ro-RO)
  • Russian (Russia) (ru-RU)
  • Slovak (Slovakia) (sk-SK)
  • Swedish (Sweden) (sv-SE)
  • Thai (Thailand) (th-TH)
  • Turkish (Turkey) (tr-TR)
  • Chinese (China) (zh-CN)
  • Chinese (Hong Kong SAR China) (zh-HK)
  • Chinese (Taiwan) (zh-TW)

How to use plugin


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:

iOS_TextToSpeech .StartSpeech( "Your text" );

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.

  • Go to File->Build Settings and switch the platform to iOS.
  • Bring-up PlayerSettings in the inspector.
  • Under Settings for iOS:
    • Set your Company Name.
    • Set Product Name.
    • Set Default Orientation.
    • Set the Bundle Identifier to appropriate value for your provisioning profile.
    • Set the SDK to iOS Latest.
    • Set the Target iOS Version to not less 7.0!!!
  • Go to File->Build Settings and click Build.
  • Select the build location of your preference.
  • When the build is complete, double-click the generated Unity-iPhone.xcodeproj to open it in Xcode.
  • After the project is opened, you can see the files "iOS TTS.h" and "iOSTTS.m" in the Project Navigator window in a subdirectory ..Libraries/Plugins/ iOS /

  • Set the build target to Unity-iPhone > "device" (your device needs to be plugged in at this point).
  • Build the project (Cmd-B).
  • The project should build without errors.
  • Run the project (click the Run button).
  • Enjoy!

Additional Features


Pause and Stop synthesis

  • To pause, use the function:

    iOS_TextToSpeech .PauseSpeech();
  • To continue (after a pause), use the function:

    bool canContinue = iOS_TextToSpeech .ContinueSpeech();
  • To stop, use the function:

    iOS_TextToSpeech .StopSpeech();

Up ^

Selecting and changing the language parameters

  • To get a list of all supported languages ​​in the format "English United States", "French France", etc. use the function:

    string [] allLanguages = iOS_TextToSpeech .GetAllLanguages();
  • To get a list of all supported BCP-47 codes in the format "en-US", "fr-FR" etc. use the function:

    string [] allLanguageCodes = iOS_TextToSpeech .GetAllLanguageCodes();
  • 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:

    TTS_Languages tts_Languages = iOS_TextToSpeech .GetAllLanguagesWithCodes();
  • To set the language for the synthesis by its name (for example, "English United States"), use the function:

    iOS_TextToSpeech .SetLanguage( "English United States" );
  • To set the language for the synthesis by its BCP-47 code (eg, "en-US"), use the function:

    iOS_TextToSpeech .SetLanguageByCode( "en-US" );
  • 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"


Up ^

Changing speech parameters

  • 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


Up ^

Cherry on the cake: using callback function


Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage :

UnitySendMessage( "GameObjectName", "MethodName", "Message To Send");

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:

  • the name of the target GameObject by default - "GameControl"
  • the script method name by default - "DidFinishSpeak"
  • message about the end of a word pronunciation by default - "Word"
  • message about the end of an utterance pronunciation by default - "Utterance"
  • message about the end of the all text pronunciationt by default - "Text"

Note: This message also comes when you call the function:

iOS_TextToSpeech .StopSpeech();

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;

    iOS_TextToSpeech .SetUnityObjectName(gameObjectName);

  • To assign a new method name in the called script, use the function:

    //Change callback method name (default value - "DidFinishSpeak")

    string methodName = "SuperMethod" ;

    iOS_TextToSpeech .SetUnityMethodName(methodName);

  • 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" ;

    iOS_TextToSpeech .ChangeUnityWordParameterName(wordParameterName);

  • 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" ;

    iOS_TextToSpeech .ChangeUnityUtteranceParameterName(utteranceParameterName);

  • 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" ;

    iOS_TextToSpeech .ChangeUnityTextParameterName(textParameterName);



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:

iOS_TextToSpeech .StartSpeech(info.text);

Up ^

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:

Speech(info.text);

Up ^
Up ^

Look at the use of iOS TTS plugin in the demo project:


OSX TTS plugin


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:

  • In this version of OS X TTS does not support changing the pitch.
  • Speech rate is set in a range from 0.0f to approximately 500.0f (in iOS TTS from 0.0f to 1.0).
  • OS X uses a different method to set language (using identifiers like "com.apple.speech.synthesis.voice.fiona"), and list of languages differs from the list of iOS languages.
  • To use the callback function for Unity, additional configuration is required.

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.


Enjoy!