Easy Subtitles System
Subtitles displays nice-looking subtitles and closed captions for your game with smooth animations and fade in/out effects.
- Supports very long text and word wrap (make sure "Word Wrap" is checked in your GUI Skin)
- It uses the label and the box elements of the GUI Skin.
- Documentation/ - Folder containing the documentation
- Subtitles.cs - Main script
- Subtitles Box.png - Sample background image for subtitles
- Subtitles GUI Skin.guiskin - Sample GUI Skin for subtitles
- Editor/SubtitlesEditor.cs - Script of the SubtitlesEditor window
- TestScene/ - Folder containing the demo Unity scene
Show subtitles with a single line of code
After importing the package, add the Subtitles.cs script anywhere in your scene (the most common place is on your main camera). Set the GUI Skin parameter in the inspector (you can use the default one in the Scripts/ folder of the addon. You can now show a simple subtitle with one of these functions:
Show subtitles with XML
You can also load subtitles from an XML file like this:
<style id="npc" red="0.25" green="0.5" blue="1.0" italic="1" />
<style id="hero" red="1.0" green="1.0" blue="1.0" italic="1" />
<style id="ambient_sounds" red="0" green="1.0" blue="1.0" italic="0" />
<subtitle id="npc_guard_spotted" duration="2.5" style="npc">Freeze!</subtitle>
<subtitle id="hero_spotted_response" duration="3.5" style="hero">I don't think so.</subtitle>
The top part is the <styles> section. You define a style with an id, a color (red, green and blue) and if its in italic or not. Then comes the <texts>/<subtitle> part, where the "style" attributes must be a style id defined in the precedent <styles> section.
You can load the XML file with the function Subtitles.loadFromXML(string file):
Subtitles.loadFromXML("Subtitles/english"); // english.xml must be in a "Resources/Subtitles" folder
You can also give a TextAsset objet to the function instead of a string:
You can then show a subtitle by its ID with Subtitles.showByID(string id):
See the demo scene for a complete example.
- Fade In Time
- Duration of the fade in effect when subtitle appears (in seconds).
- Fade Out Time
- Duration of the fade out effect when subtitle disappears (in seconds).
- Line Spacing
- Here you can add an extra line spacing. Can be negative.
- Margin Percent
- Left and right margin, in % of the screen width
- Position Y
- Y position of the most recent subtitle, in % of screen height
- Box Draw
- Should it draw a box around subtitles?
- Box Margins
- Inner margins of the box.
- GUI Skin
- The GUI Skin to use for Subtitles
You can also edit your XML Subtitles files with the new Subtitles Editor. You can access the editor in the Window menu:
When you first open the editor window, only two buttons are available : New file... and Open file..., they are pretty straightforward. Click on New file... and create somewhere in your project the XML Subtitles file.
Save will save the file, + Add a style and + Add a subtitle will add a style and a subtitle. In the main part of the window are the style on the top and the subtitles on the bottom. For each style row you can:
- Specify its unique ID
- Choose the color
- Set if the text must be in italic or not
- Specify its unique ID
- Write the text
- Specify its duration in seconds
- Specify its style
- Up and Down buttons will reorder the rows.
- Ins will insert a new row below the current one.
- Del will delete the current row.
Subtitles.show("How are you?", 2.5f, Color(0.5f, 0.5f, 0.5f, 1f));
Subtitles.getMessageByID("subtitle_id"); // Returns the text associated with the specified id
You now know everything about this addon!