public void onUpdateChatMessage() { // 获取所有聊听记录 var unReadMsgList = LogicDataCenter.snsDataManager.SessionManager.getUnReadMsg(m_SessionName); foreach (var item in unReadMsgList) { SNSMsgContentItem data = new SNSMsgContentItem(); data.info = item; ChatContentView.DataSource.Add(data); } ChatContentView.GetScrollRect().normalizedPosition = new Vector2(0f, 0f); }
public void SetData(SNSMsgContentItem data) { m_nMsgID = data.info.msg.dwMsgID; Transform curTrans = null; if (isSelf(data.info.msg.dwFromAccountID)) { curTrans = rightTrans; leftTrans.gameObject.SetActive(false); rightTrans.gameObject.SetActive(true); } else { curTrans = leftTrans; leftTrans.gameObject.SetActive(true); rightTrans.gameObject.SetActive(false); } Transform timeTrans = curTrans.FindChild("Time"); if (timeTrans == null) { return; } Transform bubbleTrans = curTrans.FindChild("Bubble"); if (bubbleTrans == null) { return; } Transform contentTrans = curTrans.FindChild("Bubble/Content"); if (contentTrans == null) { return; } Image avatar = curTrans.FindChild("Avatar").GetComponent <Image>(); if (avatar == null) { return; } SNSDataManager.BuddyInfo buddy = LogicDataCenter.snsDataManager.getBuddyInfoByUserID(data.info.msg.dwFromAccountID); // 设置头像 avatar.sprite = USpriteManager.Instance.GetSprite(USpriteManager.ESpriteType.EST_PlayerHead, WndID.WND_ID_SNS_MAINPANEL, 1, buddy.Info.nSex + 1); timeTrans.GetComponent <Text>().text = Api.convertTimeStamp2DateTime(data.info.msg.dwTime).ToString("yyyy-MM-dd HH:mm"); // 设置content的最大宽度 RectTransform contentRect = contentTrans as RectTransform; contentRect.sizeDelta = new Vector2(MaxWidth, contentRect.sizeDelta.y); //contentRect.anchoredPosition = new Vector2(contentRect.anchoredPosition.x, 0); float contentHeight = 0; ChatContentItem item = contentTrans.GetComponent <ChatContentItem>(); if (item != null) { ChatContentItemData itemData = new ChatContentItemData(); itemData.objList = data.info.objList; item.SetData(itemData); contentHeight = item.Height; } else { Debug.LogError("item == null, This GameObject have no ChatContentItem component."); } // 设置内容的大小 float contentWidth = contentRect.sizeDelta.x; contentRect.sizeDelta = new Vector2(contentWidth, contentHeight); // 气泡与内容之间的边缘大小 Vector2 Margin; Margin.x = Math.Abs(contentRect.anchoredPosition.x); Margin.y = Math.Abs(contentRect.anchoredPosition.y); // 计算气泡的大小 RectTransform bubbleRect = bubbleTrans as RectTransform; bubbleRect.sizeDelta = new Vector2(contentWidth + Margin.x * 2, contentHeight + Margin.y * 2); float bubbleHeight = bubbleRect.sizeDelta.y; // 设置时间的坐标(时间显示在聊天内容下面),坐标为负数 float timePosY = bubbleRect.anchoredPosition.y + (-bubbleHeight); RectTransform timeRect = timeTrans as RectTransform; timeRect.anchoredPosition = new Vector2(timeRect.anchoredPosition.x, timePosY); // 计算总高度 m_Height = Math.Abs(bubbleRect.anchoredPosition.y) + bubbleHeight + TimeHeight + MarginY; if (m_Height < MinHeight) { m_Height = MinHeight; } // 设置总高度 RectTransform rect2 = curTrans as RectTransform; rect2.sizeDelta = new Vector2(rect2.sizeDelta.x, m_Height); // 设置DefautItem高度 RectTransform rect3 = transform as RectTransform; rect3.sizeDelta = new Vector2(rect3.sizeDelta.x, m_Height); m_Height = 0; }