123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 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;
-
- foreach(Transform t in chatParent.GetComponentInChildren<Transform>())
- {
- 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>().text = message;
- newChatItem.GetComponentInChildren<Text>().alignment = isMe ? TextAnchor.MiddleRight : TextAnchor.MiddleLeft;
- }
- public void SendMsg()
- {
- SendMsg("");
- }
- public void SendMsg(string msg)
- {
- MPChat.instance.SendMsg(chatInputField.text);
- chatInputField.text = "";
- }
- }
|