using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChatUI : MonoBehaviour { public GameObject chatItemPrefab; public Transform chatParent; public static ChatUI instance; public InputField chatInputField; public Button sendBtn; void Awake() { instance = this; //PURGE foreach(Transform t in chatParent.GetComponentInChildren()) { if(t != chatItemPrefab) { Destroy(t.gameObject); } } sendBtn.onClick.AddListener(SendMsg); chatInputField.onEndEdit.AddListener(SendMsg); } public void AddNewMessage(string message,bool isMe) { GameObject newChatItem = Instantiate(chatItemPrefab,chatParent); newChatItem.GetComponentInChildren().text = message; newChatItem.GetComponentInChildren().alignment = isMe ? TextAnchor.MiddleRight : TextAnchor.MiddleLeft; } public void SendMsg() { SendMsg(""); } public void SendMsg(string msg) { MPChat.instance.SendMsg(chatInputField.text); chatInputField.text = ""; } }