private void Update() { if (FreezeTime) { return; } if (selected && InputField.text != "") { ResetTimer(); if (UnityEngine.Input.GetKey(KeyCode.Return)) { if (UnityEngine.Input.GetKey(KeyCode.LeftShift)) { if (!InputField.text.EndsWith("\n")) { InputField.ActivateInputField(); InputField.text += "\n"; StartCoroutine(MoveToEndOfText()); } } else { playerChatManager.SendMessage(); } } } else { timeLeftUntilAutoClose -= Time.unscaledDeltaTime; if (timeLeftUntilAutoClose <= 0) { playerChatManager.HideChat(); FreezeTime = true; } } }
private void Update() { if (FreezeTime) { return; } if (selected) { if (!string.IsNullOrEmpty(InputField.text)) { ResetTimer(); if (UnityEngine.Input.GetKey(KeyCode.Return)) { if (UnityEngine.Input.GetKey(KeyCode.LeftShift)) { if (!InputField.text.EndsWith("\n")) { InputField.ActivateInputField(); InputField.text += "\n"; StartCoroutine(MoveToEndOfText()); } } else { // Detect if there's a ghost message on top of the list (one that wasn't sent but still saved) if (sentMessagesIndex != sentMessages.Count && sentMessages.Count > 0) { sentMessages.RemoveAt(sentMessages.Count - 1); } // If the list is too long, we'll just remove the first message of the list if (sentMessages.Count > historyLength) { sentMessages.RemoveAt(0); } sentMessages.Add(InputField.text); _sentMessagesIndex = sentMessages.Count; playerChatManager.SendMessage(); } } } // Chat history stuff // GetKeyDown means it's only getting executed once per press if (UnityEngine.Input.GetKeyDown(KeyCode.UpArrow)) { // If we're currently on the newest message, we want to save it to be able to come back to it (a ghost message) if (sentMessagesIndex == sentMessages.Count && sentMessages.Count > 0) { sentMessages.Add(InputField.text); _sentMessagesIndex = sentMessages.Count; } sentMessagesIndex--; } else if (UnityEngine.Input.GetKeyDown(KeyCode.DownArrow)) { // We shouldn't execute this check if we're already on top of the list if (sentMessagesIndex < sentMessages.Count) { sentMessagesIndex++; // If we're back to the newest message, we can delete it from the list because it has not been sent yet if (sentMessagesIndex == sentMessages.Count && sentMessages.Count > 0) { sentMessages.RemoveAt(sentMessages.Count - 1); _sentMessagesIndex = sentMessages.Count; } } } } else { timeLeftUntilAutoClose -= Time.unscaledDeltaTime; if (timeLeftUntilAutoClose <= 0) { playerChatManager.HideChat(); FreezeTime = true; } } }