Friday, May 28, 2010

Subtitles and Sound

As part of my goal of completing my animatic without using any post editing I have managed to put together a script for subtitle text and to play my chosen background music.

The script for subtitles uses a GUI Text object and changes the text at required times. Behind the text I have created a GUI Texture which provides a black background to the white text. When hiding and showing the subtitles I have used the iTween script available here: http://itween.pixelplacement.com/ to faded the GUI Text and GUI Texture out with this line:

iTween.fadeTo(subtitleholder,{"alpha":0, "onComplete":"destroy"});
This solution works well and I have applied it to multiple scenes in my animatic.

The second new script I have put in place controls the animatics background music. I thought adding music would be quite simple until I realised that when moving between scenes all audio sources would be removed and all sounds from the previous scene would stop which is not ideal for background music.

To correct this I managed to work out the following script which goes in the first scene. It sets up a singleton which is accessible through all scenes, and prevents my audio source from being removed between scenes loads.

//Create instance of class.
private static var instance:GameMusic;
//Create singleton instance.
public static function GetInstance() : GameMusic {
return instance;
}

function Awake() {
if (instance != null && instance != this) {
Destroy(this.gameObject);
return;
} else {
instance = this;
}
//prevent audio source from being removed between scene loads.
DontDestroyOnLoad(this.gameObject);
}
In subsequent scenes I then load new background music by removing the object the above script is attached to and add it again with a different audio source.

Being able to do subtitles and sound within Unity should make it possible for me to capture the entire scene in real-time with not post editing.

No comments:

Post a Comment