NPC dialogue - Loading a text file to a string?

Hi! So when the player in my game talks to npcs, I use a string variable to show what they say (in a GUI.Label), and i edit this string in the inspector. But if the NPC has a long thing to say, in the inspector I can’t do any "
" or anything, and it also just isn’t very convenient because the little box it has for you to enter isn’t that great.

So I was thinking, maybe if i type out the things for npc’s to say in a text file, then load that file through a script and save line _ to _ as a string variable? How would i do this?

Or is there a better alternative?

  • Thanks

Here is an example of where you might want to start with this using a TextAsset:

C#

using UnityEngine;
using System.Collections;

public class TextRead : MonoBehaviour 
{

	public TextAsset textFile;
	string[] dialogLines;
	
	// Use this for initialization
	void Start () 
	{
		// Make sure there this a text
		// file assigned before continuing
		if(textFile != null)
		{
			// Add each line of the text file to
			// the array using the new line
			// as the delimiter
			dialogLines = ( textFile.text.Split( '

’ ) );
}
}
}

JavaScript

var textFile : TextAsset;
var dialogLines : string[];

function Start()
{
	// Make sure there is a text
	// file assign before continuing
	if(textfile)
	{
		// Add each line of the text file
		// to the array using the new line
		// as the delimiter
		dialogLines = (textFile.text.Split("

"[0]));
}
}

When you want to display a certain line from your file just call the appropriate index in the array of strings like this:

C#

// Assign the first string
// in the array to a variable
string dialog = dialogLines[0];

JavaScript

// Assign the first string
// in the array to a variable
var dialog : string = dialogLines[0];