Code: Select all
using UnityEngine;
namespace PixelCrushers.DialogueSystem
{
/// <summary>
/// Add this script to your Dialogue Manager if you want to keep track
/// of the current conversation and dialogue entry. When you load a game,
/// it will resume the conversation at that point.
/// </summary>
public class RememberCurrentDialogueEntry : MonoBehaviour
{
public void OnConversationStart(Transform actor)
{
var actorName = (DialogueManager.CurrentActor != null) ? DialogueManager.CurrentActor.name : string.Empty;
var conversantName = (DialogueManager.CurrentConversant != null) ? DialogueManager.CurrentConversant.name : string.Empty;
DialogueLua.SetVariable("CurrentConversationActor", actorName);
DialogueLua.SetVariable("CurrentConversationConversant", conversantName);
Debug.Log("<color=cyan>Starting conversation between " + actorName + " and " + conversantName + "</color>");
}
public void OnConversationLine(Subtitle subtitle)
{
DialogueLua.SetVariable("CurrentConversationID", subtitle.dialogueEntry.conversationID);
DialogueLua.SetVariable("CurrentEntryID", subtitle.dialogueEntry.id);
Debug.Log("<color=cyan>Recording current conversation position as conversation #" + subtitle.dialogueEntry.conversationID + " and entry #" + subtitle.dialogueEntry.id + "</color>");
}
public void OnConversationEnd(Transform actor)
{
Debug.Log("<color=cyan>Recording that the conversation ended.</color>");
DialogueLua.SetVariable("CurrentConversationID", -1);
}
public void OnApplyPersistentData()
{
if (!enabled) return;
var conversationID = DialogueLua.GetVariable("CurrentConversationID").AsInt;
var entryID = DialogueLua.GetVariable("CurrentEntryID").AsInt;
Debug.Log("<color=cyan>Checking if we should resume conversation # " + conversationID + " entry #" + entryID + "</color>");
if (conversationID >= 0 && entryID > 0)
{
var conversation = DialogueManager.MasterDatabase.GetConversation(conversationID);
var actorName = DialogueLua.GetVariable("CurrentConversationActor").AsString;
var conversantName = DialogueLua.GetVariable("CurrentConversationConversant").AsString;
var actor = GameObject.Find(actorName);
var conversant = GameObject.Find(conversantName);
var actorTransform = (actor != null) ? actor.transform : null;
var conversantTransform = (conversant != null) ? conversant.transform : null;
Debug.Log("<color=cyan>Yes. Stopping old conversation (if any).</color>");
DialogueManager.StopConversation();
Debug.Log("<color=cyan>Resuming conversation '" + conversation.Title + "' between " + actorTransform + " and " + conversantTransform + "</color>");
DialogueManager.StartConversation(conversation.Title, actorTransform, conversantTransform, entryID);
}
}
}
}