How to do it...

To synchronize subtitles with a cutscene, follow these steps:

  1. Import the cutscene the same way as in the previous recipe.
  2. Create a new Image (go to Game ObjectUIImage).
  3. Name it SubtitlesContainer.
  4. Create a new Text game object and parent it to the SubtitlesContainer. To do so, right-click on the SubtitlesContainer and choose UI |Text.
  5. Change the color of the SubtitlesContainer's Image component to semitransparent black. Change the color of the Text component to white. Adjust the position and size of the objects as desired.
  6. Place the model in the Scene.
  7. Create a new script and call it Subtitles.cs. This script has a string subtitlesText variable in which we will store the text displayed in the subtitles. It also has public references to the SubtitlesContainer and Text game objects: public GameObject subtitlesContainer and public Text textComponent. This script has one public void SetSubtitles(string text) function. It is called from an Animation Event and is used to set the subtitlesText variable's value:
        public void SetSubtitles(string text) 
        { 
            subtitlesText = text; 
        } 
  1. In the Update() function of this script, we check if the subtitlesText variable contains any text. If so, we enable the SubtitlesContainer game object and set the Text game object to display our subtitles. If subtitlesText is an empty string, we disable the SubtitlesContainer:
        if (string.IsNullOrEmpty(subtitlesText) 
        && subtitlesContainer.activeSelf) 
        { 
            subtitlesContainer.SetActive(false); 
        } 
        else if (!string.IsNullOrEmpty(subtitlesText) 
        && !subtitlesContainer.activeSelf) 
        { 
            subtitlesContainer.SetActive(true); 
        } 
        else 
        { 
            textComponent.text = subtitlesText; 
        } 
  1. Attach the script to the cutscene game object (the one that has the Animator component and animates all the props).
  2. Open the Animation Import Settings for the cutscene animation.
  3. You can scrub through the timeline and add Animation Events.
  4. Add an Animation Event and name the function as SetSubtitles. Enter the text you want to display as subtitles in the String parameter, as shown in the following screenshot:
SetSubtitles Animation Event with the "Goooal!" text
  1. If you want the subtitles to disappear, add a SetSubitles Animation Event and leave the String parameter empty.
  2. Apply the Animation Import Settings.
  3. Play the game to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.181.61