SanAndreasUnity/Assets/Scripts/UI/ChatInputController.cs

50 lines
1.2 KiB
C#
Raw Normal View History

2020-04-20 20:17:34 +00:00
using System.Collections;
using System.Collections.Generic;
2021-01-02 17:56:46 +00:00
using SanAndreasUnity.Utilities;
2020-04-20 20:17:34 +00:00
using UnityEngine;
using UnityEngine.UI;
namespace SanAndreasUnity.UI
{
public class ChatInputController : MonoBehaviour
{
public InputField inputField;
public Button sendButton;
void Start()
{
2021-01-02 17:56:46 +00:00
this.sendButton.onClick.AddListener(this.OnTextSubmitted);
this.inputField.onEndEdit.AddListener(this.OnEndEdit);
2020-04-20 20:17:34 +00:00
}
2021-01-02 17:56:46 +00:00
void OnTextSubmitted()
2020-04-20 20:17:34 +00:00
{
2021-01-02 17:56:46 +00:00
string msg = this.inputField.text;
2020-04-20 20:17:34 +00:00
this.inputField.text = "";
if (string.IsNullOrWhiteSpace(msg))
return;
Chat.ChatManager.SendChatMessageToAllPlayersAsLocalPlayer(msg);
}
2021-01-02 17:56:46 +00:00
void OnEndEdit (string value)
{
if (CustomInput.Instance.GetKeyDown (KeyCode.KeypadEnter) || CustomInput.Instance.GetKeyDown (KeyCode.Return))
{
// send chat message
this.OnTextSubmitted();
// set focus to input field
this.inputField.Select();
this.inputField.ActivateInputField ();
}
}
2020-04-20 20:17:34 +00:00
}
}