For my Game Design program’s Capstone year I have taken on the primary role, “Programmer”, for Team Voyager, and as such will be chronicling the process of creating an management system for subtitles within our game.
To begin, I wrote out some of the requirements that the system would need. Specifically, I wanted to create a system that would require little to no programmer on the behalf of the user. Alongside this, I worked with our Accessibility Designer to ensure that we are following well-thought out guidelines for accessibility customization. The key requirements for this manager are as follows:
- Custom editor window (interface for the narrative designers)
- Dropdown list for selecting the character’s name during subtitle creation
- Allow the designer to preview what a given subtitle would look like within the Unity editor
- Automatic formatting of the given written text
- Automatic splitting of the written text to separate subtitles
- Adjustable Letterbox options
To begin, I wanted to create a custom editor tool, as I have recently learned about extending scripts from the Editor class, allowing for custom inspector windows. Following this, I came across the extensions for the EditorWindow class, which allows us to create full window tools.
Extending from the EditorWindow script allows for a few implemented functions to override, such as: GetWindow<>() and OnGUI():
public class SubtitleWindow : EditorWindow
{
// Create a custom toolbar menu
// When said menu entry is clicked, return
// Our custom window, typeof SubtitleWindow
[MenuItem("Voyager/Subtitle Editor)]
public static void ShowWindow()
{
GetWindow<SubtitleWindow>(title: "Subtitle Editor");
}
// Allows us to draw custom GUI elements within our new window
private void OnGUI() {}
}After creating a small thumbnail of how I pictured the custom window would look, I began to implement the custom GUI elements as such:
private string newSubtitleText = string.Empty;
private CharacterTag newCharacterTag;
void OnGUI()
{
// Creates a label at the top of the interior panel
GUILayout.Label("Subtitle Editor", EditorStyles.boldLabel);
// Creates a text field with a label
EditorGUILayout.TextField("New Subtitle Text", newSubtitleText);
// Creates a drop down selection menu for an enum attribute
EditorGUILayout.EnumPopup("Character Tag", newCharacterTag);
// Creates a button and ties a function call to its delegate
if (GUILayout.Button("Create new Subtitle"))
{
HandleSubtitleCreation(newSubtitleText, newCharacterTag);
}
// Update the GUI if there are any changes to the fields
if (GUI.changed)
{
Repaint();
}
}The resulting window, although underwhelming and currently not very useful, looks as such:

Following this, we have a few problems to address. First, we need to increase the height of the text field, as well as make the text we enter wrap around, as it currently just displays as one long line. To solve this, I created a new GUIStyle(), and set the word wrap bool to true. We then feed this GUIStyle into the text field’s creation code, as well as setting the fields MinHeight() to 40.
The next aspect I wished to add to the window is a property field for displaying the existing subtitles. Now this is usually a relatively easy task when extending a script from the Editor class, however, since I am inheriting from the EditorWindow, we are not given the useful “serializedObject” variable to add SerializedProperties to. To solve this, we have to manually implement it. The resulting code looks as such:
// Since this is an editor window, we don't get the fancy editor "serializedObject" call
// so we build it ourselves, automatically updates when the values are changed
void HandleSerializedSubtitles()
{
// Set subtitle target as object field with type subtitlemanager
// return if it doesnt exist
_subtitleManagerTarget = (SubtitleManager)EditorGUILayout.ObjectField("Subtitle Manager:", _subtitleManagerTarget, typeof(SubtitleManager), true);
// Find our subtitle manager script within the current scene
_subtitleManagerTarget = FindFirstObjectByType<SubtitleManager>();
// If the subtitle manager script is not in the scene, break
if (_subtitleManagerTarget == null) return;
// If the serialized object for the subtitle manager is not our current subtitle manager
// Set our subtitle manager target to a new serialized object in ref to our manager
// Set the property field to the _existingSubtitles list within our manager script
if (_serializedSubtitleMTarget == null || _serializedSubtitleMTarget.targetObject != _subtitleManagerTarget)
{
_serializedSubtitleMTarget = new SerializedObject(_subtitleManagerTarget);
_serializedSubtitleProperty = _serializedSubtitleMTarget.FindProperty("_existingSubtitles");
}
// Update the serialized target reference
_serializedSubtitleMTarget.Update();
// Create the property field for our existing subtitles
// Ensuring that the IncludeChildren param is true
EditorGUILayout.PropertyField(_serializedSubtitleProperty, true);
// Applying our modified properties should they change
_serializedSubtitleMTarget.ApplyModifiedProperties();
}Now, we can add a new subtitle with some text, select a character tag, and hit Create New Subtitle. Using my Subtitle struct, which takes a string and an enum value for the character tag, we create a new subtitle and append it to the _existingSubtitles list (List<Subtitle>) within subtitle manager script. The window now looks as such:

Alongside this, we have the ability to quickly remove individual subtitles, as well as reorder them within the property field. This is useful, as we can now create a prefab with a trigger attached, or directly call from the subtitle manager script to pull a given index and automatically retrieve the data for each subtitle, and process them in the background to split up longer dialogue portions into individual “slides”.
Saving and Loading Subtitles
Now the next step was to figure out a way to save and load these subtitles. I figured the easiest way would be through JSON. Thinking ahead to the secondary programmers tasks relating to options menus, I figured I would implement a system that would allow us to save generic lists of data. Unfortunately, the included jsonutility does not allow for saving of certain datatypes, such as List<>, by default.
For my first attempt to solve this issue, I tried to loop through each of the indices of a given list, and save them out individually via the jsonutility.ToJson() function. This resulted in as many files as there were indices within said list, which was not preferable. I then attempted to concatenate the indices with a divider such as “|”, then read them back, however, the jsonutility.FromJson() function expects only one root item, and I would have to workaround this.
Instead of continuing down this route, I decided to create a generic wrapper for any given list, and have said wrapper hold onto the values we wish to keep. This allows me to create alternative wrappers to hold multiple different lists of data, and easily read and write them utilizing the jsonutility class. This is particularly important for the secondary programmer, as we can save and load different option menu values with individual wrapper properties. I have omitted the single object save functions, as the majority of these cases will be handles by the jsonutility natively.
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
// Folder name enum
public enum JsonSavePath
{
Subtitles,
Options
};
// Wrapper for list objects, generic so we can use it for anything
// Must be serializable
[Serializable]
public class SaveWrapper<T>
{
// List of our given types of objects
public List<T> objectsToSave;
public SaveWrapper(List<T> objects)
{
this.objectsToSave = objects;
}
}
public static class SaveSystem
{
// Saves JSON list data to a given folder name and file name
public static void Save<T>(List<T> objectsToSave, JsonSavePath savePathType, string fileName)
{
// Creates wrapper for saved objects
// We use this as if we were to individually loop through all obj in the given list,
// JSON cannot read it back as the syntax is wrong
SaveWrapper<T> wrapper = new SaveWrapper<T>(objectsToSave);
// Save as JSON string
string jsonString = JsonUtility.ToJson(wrapper, true);
// Write string to given file path
File.WriteAllText(GetSavePathToFile(savePathType, fileName), jsonString);
}
// Loads JSON list data from a given folder name and file name
public static List<T> Load<T>(JsonSavePath savePathType, string fileName)
{
string path = GetSavePathToFile(savePathType, fileName);
if (!File.Exists(path))
{
Debug.LogAssertion($"Save System did not find a file at: {path}");
return new List<T>();
}
// Read json data at path
string jsonString = File.ReadAllText(path);
// Create wrapper with json data
SaveWrapper<T> wrapper = JsonUtility.FromJson<SaveWrapper<T>>(jsonString);
// Return wrappers saved objects as a list
return wrapper.objectsToSave;
}
// Returns the save path from a given type and a filename
private static string GetSavePathToFile(JsonSavePath savePathType, string fileName)
{
// Start in the Json folder cause all json data should be located there
string jsonPath = Path.Combine(Application.dataPath, "Json");
string savePath = string.Empty;
// Add cases here for new save types, this designates the folder name under Json in Assets folder
// E.g. To add control options,
switch (savePathType)
{
case JsonSavePath.Subtitles:
savePath = "Subtitles";
break;
case JsonSavePath.Options:
savePath = "Options";
break;
}
// Create new path with given type
string newPath = Path.Combine(jsonPath, savePath);
// Ensure folder exists
Directory.CreateDirectory(newPath);
// return filepath
return Path.Combine(newPath, $"{fileName}.json");
}
}Now to utilize this save system within the subtitle manager, we can create some simple function calls, and attach them to our custom editor window:
// Appends a given subtitle to the list of existing subtitles
private void AddExistingSubtitle(Subtitle newSubtitle)
{
_existingSubtitles.Add(newSubtitle);
}
// Load subtitles from file
public void LoadExistingSubtitlesFromFile()
{
// Clear existing subtitle list
_existingSubtitles.Clear();
// Get subtitles from subtitles file
var subtitles = SaveSystem.Load<Subtitle>(JsonSavePath.Subtitles, "TestSubtitles");
// Append each subtitle from file to the current existing subtitle list
foreach (var subtitle in subtitles)
AddExistingSubtitle(subtitle);
}
// Save subtitles to file
public void SaveCurrentSubtitlesToFile()
{
SaveSystem.Save(_existingSubtitles, JsonSavePath.Options, "TestOptions");
}Now our finalized subtitle editor looks as such:

Final Notes:
Personally, I had a very entertaining time devising this system, as well as learning about extending the functionality of the Unity Editor. Custom tools such as these allow us to create tools for designers, which requires no programming in order to implement. There are multiple ways of handling the creation of tools such as these, and I look forward to continuing my understanding of custom tool creation.