46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
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<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 = "";
|
|
}
|
|
}
|