Subtitles - POINT-VR/POINT-VR-Chapter-1 GitHub Wiki
This guide contains the following sections:
Adding new audio files and corresponding subtitles
- Navigate to the folder
Assets/POINT/Audio/Narration/Resources
. - Add the following files to the folder
-
- An Unity-compatible audio file (Supported types according to Unity Documentation: .mp3, .ogg, .wav, .aiff / .aif, .it, .s3m, .xm)
-
- A .txt file containing contents from either a .vtt or .srt file, with the suffix "_<ISO 639-1 Language Code>" (e.g. "Narration_en.txt")
💡The caption file must be of file type
.txt
in order to be loaded into Unity. For formatting of captions, currently only .srt font color and size tags, as well as bolding and italics have been developed.
- To trigger the reading of the audio files and the subtitles, call the function
PlayClipWithSubtitles
by theNarrationManager
script attached to the Player. Enter the name of the audio file (WITHOUT the file type) as the parameter.
Debugging
Q: The audio clip is playing but I do not see the subtitles.
A: Ensure that (1) subtitles have been enabled in the Settings menu and (2) the subtitles are in a .txt
file converted from a .vtt
or .srt
file, with the language code as a suffix (refer to Assets/POINT/Audio/Narration/Resources/Narration_test_en.txt
as an example). To convert, simply rename the .vtt
or .srt
extension to .txt
.
--
Q: Some subtitle characters (in a different language) cannot be loaded.
A: Refer to Localization below.
Implementation details
Narration and subtitles are handled using the script Assets/POINT/Audio/Narration/NarrationManager.cs
, currently attached to the Player GameObject.
Parsing Subtitles
- The
GenerateSubtitles()
IENumerator takes in the time at which the audio clip started playingplayTime
. - It loops through every line in the subtitle .txt file, and identifies lines containing timestamps via the presence of the string
-->
. - It automatically assumes that the corresponding subtitles are in the proceeding line, and sets the subtitles to be the proceeding line at the time.
- Wait functions are used to coordinate the timing of subtitles.
Localization
The current implementation handles different font assets for different languages by serializing them as public variables. To save on redundant memory, as of time of writing, we have loaded only Noto Sans Regular (support for Latin alphabet, Crylic and Hindi) into the project. To add support for more fonts, create a new font asset based on the .tff or .otf file, and link it to the corresponding variable in the Inspector.
In the script, the font is then loaded in the function LanguageCode()
, which also assists in identifying the correct subtitle file to refer to via ISO 639-1 code in the case of multiple languages.
💡This method was chosen in preference to Dynamic Fonts, which would otherwise cost a lot of memory.
Parsing style tags
- The
ParseStyleTags()
function takes in a string (subtitle from .srt) as an input, and outputs the string with all .srt style tags converted to Unity TextMeshPro Rich Text Tags. - Bold and italics are left as is due to the same format being used.
- For font color and font size, the string
font
is removed from the .srt tagsfont color
andfont size
since TextMeshPro usescolor
andsize
instead. - Additional parsing has to be done for colors due to separate handling of cases where the value provided is a color name (e.g. "blue") versus a HEX code.