using LitJson; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChatTextManager : MonoBehaviour { public int TextCount; private Vector2 initSize; public RectTransform parent; public Text text; public RectTransform rect; public ScrollRect scrollRect; public Text TextInfo; // Start is called before the first frame update void Start() { initSize = parent.sizeDelta; } public void DeletText() { TextCount = 0; text.text = ""; TextInfo.text = "信息-" + TextCount+""; } public void SetText(string msg) { TextCount++; text.text += "\n" + "\n" + msg; TextInfo.text = "信息-" + TextCount + ""; scrollRect.verticalNormalizedPosition = 0f; } // Update is called once per frame void Update() { // 获取Text的Size Vector2 v2 = rect.rect.size; // width保持不变 rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, v2.x); // 动态设置height rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, text.preferredHeight); if(initSize.y < v2.y) { parent.sizeDelta = rect.rect.size; } } public void joinRoom() { SetText("您加入了房间"); } public void createRoom() { SetText("您创建了房间"); } public void leaveRoom(string n) { SetText(n+" 离开了房间"); } public void otherJoinRoom(string n) { SetText(n + " 加入了房间"); } public void SetTextManager(string method,JsonData data) { switch(method) { case "joinRoom": joinRoom(); if (data["room"]["users"].Count > 0) { for (int i = 0; i < data["room"]["users"].Count; i++) { otherJoinRoom(data["room"]["users"][i]["nickName"].ToString()); } } break; case "closed": if (SCRtcFactory.Instance.mSCRtcPeers != null && !string.IsNullOrEmpty(data["peerId"].ToString())) { if (SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["peerId"].ToString()) != null) { leaveRoom(SCRtcFactory.Instance.mSCRtcPeers.getPeer(data["peerId"].ToString()).name); } } break; case "joined": otherJoinRoom(data["nickName"].ToString()); break; case "chatMessage": SetText(data["from"].ToString()+":"+ data["chatMessage"].ToString()); break; } } }