init
This commit is contained in:
45
Assets/Scripts/ChatUI.cs
Normal file
45
Assets/Scripts/ChatUI.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
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 = "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user