Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4. public class ChatController : MonoBehaviour {
  5. public TMP_InputField ChatInputField;
  6. public TMP_Text ChatDisplayOutput;
  7. public Scrollbar ChatScrollbar;
  8. void OnEnable()
  9. {
  10. ChatInputField.onSubmit.AddListener(AddToChatOutput);
  11. }
  12. void OnDisable()
  13. {
  14. ChatInputField.onSubmit.RemoveListener(AddToChatOutput);
  15. }
  16. void AddToChatOutput(string newText)
  17. {
  18. // Clear Input Field
  19. ChatInputField.text = string.Empty;
  20. var timeNow = System.DateTime.Now;
  21. string formattedInput = "[<#FFFF80>" + timeNow.Hour.ToString("d2") + ":" + timeNow.Minute.ToString("d2") + ":" + timeNow.Second.ToString("d2") + "</color>] " + newText;
  22. if (ChatDisplayOutput != null)
  23. {
  24. // No special formatting for first entry
  25. // Add line feed before each subsequent entries
  26. if (ChatDisplayOutput.text == string.Empty)
  27. ChatDisplayOutput.text = formattedInput;
  28. else
  29. ChatDisplayOutput.text += "\n" + formattedInput;
  30. }
  31. // Keep Chat input field active
  32. ChatInputField.ActivateInputField();
  33. // Set the scrollbar to the bottom when next text is submitted.
  34. ChatScrollbar.value = 0;
  35. }
  36. }