123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- public class ChatTextManager : MonoBehaviour
- {
- public int TextCount;
- private Vector2 initSize;
- public RectTransform parent;
- public TMP_Text text;
- public RectTransform rect;
- public ScrollRect scrollRect;
- public TMP_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("<color=yellow>您加入了房间</color>");
- }
- 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());
- FileList.Instance.AddMsgItem(null, data["from"].ToString(), data["chatMessage"].ToString());
- break;
- }
- }
- }
|