Post news RSS Dev Blog: Subtitle System

Cave In Dev Blog discussing the creation of a subtitle system in Unity.

Posted by on

Being a puzzle adventure game we do plan to have spoken dialog from characters. We also feel it’s rather important to have some sort of subtitle system built in so I set to work on planning and building a dialog and subtitle system for Unity. We don’t plan to have conversational choices so we just needed a way to easily create and trigger spoken dialog with subtitles.

UML Diagram

I’ve been spending some time learning UML to better layout my class designs before going in for the code. I have a tendancy to jump in to the code too quickly without a plan so am trying to learn some better techniques for the planning stage to better ensure success with the implementation. After a few Google searches and tutorials I started using NClass for my UML designing.

While I haven’t fully mastered the proper connection types here is my basic UML diagram for my Dialog/Subtitle system.

Subtitles UML For Unity

The diagram was very useful for laying out my thoughts on how I wanted to design the system and how I wanted it to work. I also updated the diagram after writing the classes. Right now I’m just using the UML as a reference and not attempting to create fully workable classes.

Class Break down

Subtitle
Subtitle class is mainly a data container for the text that will appear and at what time during a dialog voice over.

DialogCharacter
DialogCharacter is a bit of a place holder at this point, but allows for re-usable character specific settings related to dialog. In the UML class I have color and name which could be used as part of the visual side of rendering the subtitles to the screen via the GUI.

AudioDialog
This is where stuff starts coming together. The AudioDialog contains the actual audio for the dialog, an array of subtitle elements and a reference to a DialogCharacter. When working with the AudioDialog in the inspector (shown further down) we can easy add subtitle elements with the text and times they should appear.

DialogGroup
This is the main container class for AudioDialog(s). This way if we have multiple characters chatting back and forth each side of the conversation would be represented by an AudioDialog, but then further encapsulated in to a DialogGroup

The DialogGroup is what is actually injected in to the DialogManager when we want to play some dialog. As we can see this gives us the leverage for simple one off dialog such as “This Door is locked” or a longer dialog conversation involving multiple characters talking back and forth.

DialogManager
This is the top level manager which handles the queue of potential DialogGroup(s) to be played.

Custom Assets

I’ve written custom assets for Unity in the past based on an article by Jacob Pennock which is great for handling re-usable data assets. In my case the DialogCharacter and DialogGroup are custom assets that I can create by right clicking in the Unity project window and just creating new instances of these object types as re-usable assets.

Doing things this way means that any specific character data can easily be modified and applied across the scope of the game instead of having to make character specific data changes on each piece of dialog. The dialog group is also a custom asset so that in cases where some dialog may be repeated in more than one location I can just set it up once and re-use wherever I need it.

Working in Unity

Now that I had a plan of attack I could start working in Unity and see what kind of issues I may encounter with my plan. I’m glad to say that for the most part everything came together quite quickly and easily inside of Unity. After creating some custom assets and setting up the classes here is how the rough (but functional) version appears in the Unity Inspector.

Subtitles Unity Inspector

This certainly covered what I needed for the raw data, but of course what I’m not covering here is some the underlying logic. Perhaps I can cover that in a separate article if there is enough interest, but after coding in the logic the system appears to be working to my expectations.

Some additional classes where also created such as a trigger class to trigger a dialog group to get played by the DialogManager when the player collides with the trigger. I also roughed in a GUISubtitles class to handle the rendering of an “active” dialog subtitle. So far it all works great, but I want to create a custom inspector to make the DialogGroup assets look a bit more friendly. There is also some code clean up required, but everything is in and functionally working which was great to see.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.