ChatController.cs 951 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using TMPro;
  5. public class ChatController : MonoBehaviour {
  6. public TMP_InputField TMP_ChatInput;
  7. public TMP_Text TMP_ChatOutput;
  8. public Scrollbar ChatScrollbar;
  9. void OnEnable()
  10. {
  11. TMP_ChatInput.onSubmit.AddListener(AddToChatOutput);
  12. }
  13. void OnDisable()
  14. {
  15. TMP_ChatInput.onSubmit.RemoveListener(AddToChatOutput);
  16. }
  17. void AddToChatOutput(string newText)
  18. {
  19. // Clear Input Field
  20. TMP_ChatInput.text = string.Empty;
  21. var timeNow = System.DateTime.Now;
  22. TMP_ChatOutput.text += "[<#FFFF80>" + timeNow.Hour.ToString("d2") + ":" + timeNow.Minute.ToString("d2") + ":" + timeNow.Second.ToString("d2") + "</color>] " + newText + "\n";
  23. TMP_ChatInput.ActivateInputField();
  24. // Set the scrollbar to the bottom when next text is submitted.
  25. ChatScrollbar.value = 0;
  26. }
  27. }