Generating speech from spreadsheets

AC integrates with Dialogue System, which supports importing from third-party speech writing tools such as articy:draft and ChatMapper. This provides an alternative to writing speech directly with ActionLists or Timelines.

However, it is also possible - through scripting - to import dialogue from external sources such as spreadsheets. The following is an example that causes **Dialogue: Play speech** Actions to be generated from a list of speech written in a CSV spreadsheet file.

To use it:

1. Paste the code below into a C# file named **ImportSpeechEditor.cs**, and place this file in a directory named Editor within your Assets folder in the Project window.
2. Using the Scene Manager, add a new Cutscene to your scene
3. Navigate to "Adventure Creator -> Import speech lines" at the top toolbar, and select the new Cutscene in the window that appears
4. Click on Import, and then choose the CSV file to import. This file requires that columns be separated with vertical bars '|', and has three columns. The first column determines if the line is spoken by the Player (1 if so, 0 if not), the second column is the name of the character in the scene who should say the line (if not the Player), and the third is the speech text itself. Sample CSV text can be found here: pasteall.org/1530898

Note that this is just an example - you can adapt this script to suit your own needs. Details of manipulating script-generated **Dialogue: Play speech** Actions can be found in the AC Scripting guide.

ImportSpeechEditor.cs:

```
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using AC;

public class ImportSpeechEditor : EditorWindow
{

 private Cutscene cutsceneToOverwrite;

 [MenuItem ("Adventure Creator/Import speech lines")]
 public static void Init ()
 {
 ImportSpeechEditor window = (ImportSpeechEditor) EditorWindow.GetWindow (typeof (ImportSpeechEditor));
 window.titleContent.text = "Speech Importer";
 }

 private void OnGUI ()
 {
 cutsceneToOverwrite = (Cutscene) EditorGUILayout.ObjectField ("Cutscene to overwrite:", cutsceneToOverwrite, typeof (Cutscene), true);
 GUI.enabled = cutsceneToOverwrite;
 if (GUILayout.Button ("Import from file"))
 {
 ImportFromFile ();
 }
 GUI.enabled = true;
 }

 private void ImportFromFile ()
 {
 string fileName = EditorUtility.OpenFilePanel ("Import inventory item data", "Assets", "csv");
 if (fileName.Length == 0)
 {
 return;
 }

 if (System.IO.File.Exists (fileName))
 {
 string csvText = Serializer.LoadFile (fileName);
 string[,] csvOutput = CSVReader.SplitCsvGrid (csvText);

 GenerateLines (csvOutput);
 }
 }

 private void GenerateLines (string[,] csvData)
 {
 int numCols = csvData.GetLength (0)-1;
 if (numCols < 3) return;

 int numRows = csvData.GetLength (1);

 List<ImportedSpeechLine> importedSpeechLines = new List<ImportedSpeechLine>();

 for (int row = 1; row < numRows; row ++)
 {
 if (csvData [0, row] != null && csvData [0, row].Length > 0)
 {
 int isPlayerInt = 0;
 if (int.TryParse (csvData [0, row], out isPlayerInt))
 {
 bool isPlayer = (isPlayerInt == 1);
 string characterName = csvData [1, row];
 string lineText = csvData [2, row];
 importedSpeechLines.Add (new ImportedSpeechLine (isPlayer, characterName, lineText));
 }
 }
 }

 ImportLines (importedSpeechLines.ToArray ());
 }

 private void ImportLines (ImportedSpeechLine[] importedSpeechLines)
 {
 if (cutsceneToOverwrite != null)
 {
 cutsceneToOverwrite.actions.Clear ();

 foreach (ImportedSpeechLine importedSpeechLine in importedSpeechLines)
 {
 ActionSpeech speechAction = importedSpeechLine.GenerateAction ();
 cutsceneToOverwrite.actions.Add (speechAction);
 }

 UnityVersionHandler.CustomSetDirty (cutsceneToOverwrite, true);
 }
 }

 private struct ImportedSpeechLine
 {

 private string text;
 private bool isPlayer;
 private string speakerName;

 public ImportedSpeechLine (bool isPlayer, string speakerName, string text)
 {
 this.isPlayer = isPlayer;
 this.speakerName = speakerName;
 this.text = text;
 }

 public ActionSpeech GenerateAction ()
 {
 ActionSpeech speechAction = (ActionSpeech) ScriptableObject.CreateInstance (typeof (ActionSpeech));

 speechAction.messageText = text;
 speechAction.isPlayer = isPlayer;

 if (!isPlayer && !string.IsNullOrEmpty (speakerName))
 {
 AC.Char[] allCharacters = GameObject.FindObjectsOfType <AC.Char>();
 foreach (AC.Char character in allCharacters)
 {
 if (character.GetName () == speakerName)
 {
 speechAction.speaker = character;
 break;
 }
 }
 }

 return speechAction;
 }

 }

}

```