Chat log

This script adds each dialogue line to a single Text box, creating a log of all speech.

It can optionally tint the text according to the character who says each line, as well as include chosen Conversation options.

To use it:

1. Create a new C# file named ChatLog and paste in the code below
2. Add a Unity UI Text box to the scene, attach the Chat Log component to it, and assign the Text component in its Inspector.
3. (Optional) The UI can be converted to an AC Menu, but don't link the Text box itself to a Label element - only the UI canvas prefab.
4. (Optional) If the Text box is assigned as the Content of a Scroll Rect, assign it as the Auto Scroll Rect to have it scroll to the bottom automatically. Note that you'll also need to have a Content Size Fitter component on the Text box with its Vertical Fit set to Preferred Size. To allow for manual scrolling at the same time, attach the Chat Log component instead to the Scroll Rect.

ChatLog.cs:

```
 using System.Collections.Generic;
 using System.Text;
 using UnityEngine;
 using UnityEngine.UI;
 using AC;

 public class ChatLog : MonoBehaviour
 {

 #region Variables

 #if TextMeshProIsPresent
 [SerializeField] private TMPro.TextMeshProUGUI uiText;
 #else
 [SerializeField] private Text uiText;
 #endif
 [SerializeField] private ColorOption colorOption = ColorOption.Speaker;
 [SerializeField] private bool includeConversationOptions;
 private enum ColorOption { None, Speaker, Subtitle, Full };
 private readonly HashSet<LineBase> allLines = new HashSet<LineBase> ();

 #endregion

 #region UnityStandards

 private void OnEnable ()
 {
 EventManager.OnClickConversation += OnClickConversation;
 EventManager.OnStartSpeech\_Alt += OnStartSpeech;
 EventManager.OnChangeLanguage += OnChangeLanguage;

 RewriteLog ();
 }

 private void OnDisable ()
 {
 EventManager.OnStartSpeech\_Alt -= OnStartSpeech;
 EventManager.OnChangeLanguage -= OnChangeLanguage;
 EventManager.OnClickConversation -= OnClickConversation;
 }

 #endregion

 #region CustomEvents

 private void OnStartSpeech (Speech speech)
 {
 allLines.Add (new SpeechLine (speech));
 RewriteLog ();
 }

 private void OnChangeLanguage (int language)
 {
 RewriteLog ();
 }

 private void OnClickConversation (Conversation conversation, int optionID)
 {
 if (includeConversationOptions)
 {
 allLines.Add (new DialogOption (conversation, optionID));
 RewriteLog ();
 }
 }

 #endregion

 #region PrivateFunctions

 private void RewriteLog ()
 {
 if (uiText == null)
 {
 return;
 }

 StringBuilder sb = new StringBuilder ();

 foreach (var line in allLines)
 {
 string lineText = line.GetText (colorOption);
 if (!string.IsNullOrEmpty (lineText))
 {
 sb.Append (lineText).Append ("\n");
 }
 }

 uiText.text = sb.ToString ();
 }

 #endregion

 #region PrivateClasses

 private abstract class LineBase
 {

 public abstract string GetText (ColorOption colorOption);

 protected string WrapColorTags (string text, Color color)
 {
 string colorHTML = ColorUtility.ToHtmlStringRGBA (color);
 return "<color=#" + colorHTML + ">" + text + "</color>";
 }
 }

 private class SpeechLine : LineBase
 {
 private readonly Speech Speech;

 public SpeechLine (Speech speech)
 {
 Speech = speech;
 }

 public override string GetText (ColorOption colorOption)
 {
 string lineText = KickStarter.runtimeLanguages.GetTranslation (Speech.FullText, Speech.LineID, Options.GetLanguage (), AC\_TextType.Speech);
 if (string.IsNullOrEmpty (lineText))
 {
 return lineText;
 }

 if (colorOption == ColorOption.Subtitle)
 {
 lineText = WrapColorTags (lineText, Speech.GetColour ());
 }

 string speakerLabel = Speech.GetSpeaker (Options.GetLanguage ());
 if (!string.IsNullOrEmpty (speakerLabel))
 {
 if (colorOption == ColorOption.Speaker)
 {
 speakerLabel = WrapColorTags (speakerLabel, Speech.GetColour ());
 }
 lineText = speakerLabel + ": " + lineText;
 }

 if (colorOption == ColorOption.Full)
 {
 lineText = WrapColorTags (lineText, Speech.GetColour ());
 }

 return lineText;
 }

 }

 private class DialogOption : LineBase
 {

 private readonly string OriginalLabel;
 private readonly int LineID;

 public DialogOption (Conversation conversation, int optionID)
 {
 OriginalLabel = conversation.GetOptionNameWithID (optionID);
 LineID = conversation.GetOptionWithID (optionID).lineID;
 }

 public override string GetText (ColorOption colorOption)
 {
 string lineText = KickStarter.runtimeLanguages.GetTranslation (OriginalLabel, LineID, Options.GetLanguage (), AC\_TextType.DialogueOption);
 if (string.IsNullOrEmpty (lineText))
 {
 return lineText;
 }

 if (KickStarter.player)
 {
 string speakerLabel = KickStarter.player.GetName (Options.GetLanguage ());
 if (!string.IsNullOrEmpty (speakerLabel))
 {
 if (colorOption == ColorOption.Speaker)
 {
 speakerLabel = WrapColorTags (speakerLabel, KickStarter.player.speechColor);
 }
 lineText = speakerLabel + ": " + lineText;
 }
 }

 return lineText;
 }
 }

 #endregion

 }

```