using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChattingPanel : MonoBehaviour
{
public GameObject chattingPanel_Go;
Button m_OtherChat_Btn;
Button m_MyChat_Btn;
Button m_Close_Btn;
GameObject m_World_Go;
Button m_NewMessage_Btn;
Text m_NewMessage_Txt;
InputField m_Msg_InputField;
GameObject m_WorldChatPanel_Go;
Transform m_ChatContent_Ts;
private string valueText;
//输入框输入最大字节
int MaxLimit = 80;
//发送信息倒计时
int countTimeNum = 10;
//显示的最大聊天数量
int showMaxNum = 200;
//几条新消息
int newMessageNum = 0;
//所有的聊天(最大长度是展示的两倍)
List allChatItemList = new List();
//展示的聊天
List chatItemShowList = new List();
// Start is called before the first frame update
void Start()
{
allChatItemList.Clear();
chatItemShowList.Clear();
FunFind();
OnCloseNewMessage();
this.AddUIListener();
}
void FunFind()
{
m_OtherChat_Btn = chattingPanel_Go.transform.Find("root/TopBtns/OtherChat_Btn").GetComponent
2.WorldMyChatItem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WorldMyChatItem : MonoBehaviour
{
public Text m_PlayerName_Txt;
public Image m_HeadIcon_Img;
public Image m_HeadIconEdge_Img;
public Image m_DialogBg_Img;
public Text m_Msg_Txt;
public TextGenerator textGenerator;
TextGenerationSettings settings;
float width;//气泡的宽度
bool isShow = false;
// Start is called before the first frame update
void Awake()
{
m_PlayerName_Txt = this.gameObject.transform.Find("PlayerName_Txt").GetComponent();
m_HeadIcon_Img = this.gameObject.transform.Find("HeadIcon_Img").GetComponent();
m_HeadIconEdge_Img = this.gameObject.transform.Find("HeadIconEdge_Img").GetComponent();
m_DialogBg_Img = this.gameObject.transform.Find("DialogBg_Img").GetComponent();
m_Msg_Txt = this.gameObject.transform.Find("DialogBg_Img/Msg_Txt").GetComponent();
}
public void Init(string str)
{
gameObject.SetActive(true);
m_Msg_Txt.text = str;
width = m_DialogBg_Img.GetComponent().rect.width;
textGenerator = m_Msg_Txt.cachedTextGeneratorForLayout;
//显示省略号,与OnText()二选一
//SetTextWithEllipsis();
OnText();
}
void OnText()
{
settings = m_Msg_Txt.GetGenerationSettings(Vector2.zero);
Canvas.ForceUpdateCanvases();
//获取显示文本的长和宽
float textHeight = m_Msg_Txt.GetComponent().rect.height;
float textWidth = textGenerator.GetPreferredWidth(m_Msg_Txt.text, settings) / m_Msg_Txt.pixelsPerUnit;
//修改聊天背景框的大小
if (textWidth <= width)
{
m_DialogBg_Img.GetComponent().sizeDelta = new Vector2(textWidth + 70, textHeight + 30);
m_DialogBg_Img.GetComponent().anchoredPosition = new Vector2((textWidth / 2 * -1) - 210, -60);
}
else
{
m_DialogBg_Img.GetComponent().sizeDelta = new Vector2(width + 70, textHeight + 30);
m_DialogBg_Img.GetComponent().anchoredPosition = new Vector2((width / 2 * -1) - 210, -60);
}
isShow = true;
}
void LateUpdate()
{
if (isShow)
{
//修改item的大小,排列使用
//放在LateUpdate,延迟渲染
gameObject.GetComponent().sizeDelta = new Vector2(gameObject.GetComponent().rect.width, m_Msg_Txt.GetComponent().rect.height + 100);
isShow = false;
}
}
///
/// 文本超出部分省略号
///
/// 目标文本框
/// 文本
private void SetTextWithEllipsis(Text textTemp, string value)
{
var generator = new TextGenerator();
var rectTransform = textTemp.GetComponent();
//设置文本绘制范围
Vector2 vector2 = new Vector2(m_Msg_Txt.GetComponent().rect.width, 100);
var settings = textTemp.GetGenerationSettings(vector2);
generator.Populate(value, settings);
//设置可见的字符数
var characterVisibleCount = generator.characterCountVisible;
var updateText = value;
//超出部分以省略号显示
if (value.Length > characterVisibleCount)
{
updateText = value.Substring(0, characterVisibleCount - 1);
updateText += "...";
}
textTemp.text = updateText;
}
}
3.WorldOtherPeopleChatItem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WorldOtherPeopleChatItem : MonoBehaviour
{
public Text m_PlayerName_Txt;
public Image m_HeadIcon_Img;
public Image m_HeadIconEdge_Img;
public Button m_HeadIcon_Btn;
public Image m_DialogBg_Img;
public Text m_Msg_Txt;
public TextGenerator textGenerator;
TextGenerationSettings settings;
object m_Info;
float width;//气泡的宽度
bool isShow = false;
// Start is called before the first frame update
void Awake()
{
m_PlayerName_Txt = this.gameObject.transform.Find("PlayerName_Txt").GetComponent();
m_HeadIcon_Img = this.gameObject.transform.Find("HeadIcon_Img").GetComponent();
m_HeadIconEdge_Img = this.gameObject.transform.Find("HeadIconEdge_Img").GetComponent();
m_HeadIcon_Btn = this.gameObject.transform.Find("HeadIcon_Btn").GetComponent();
m_DialogBg_Img = this.gameObject.transform.Find("DialogBg_Img").GetComponent();
m_Msg_Txt = this.gameObject.transform.Find("DialogBg_Img/Msg_Txt").GetComponent();
}
public void Init(string str)
{
gameObject.SetActive(true);
m_Msg_Txt.text = str;
width = m_DialogBg_Img.GetComponent().rect.width;
textGenerator = m_Msg_Txt.cachedTextGeneratorForLayout;
OnText();
}
void OnText()
{
settings = m_Msg_Txt.GetGenerationSettings(Vector2.zero);
Canvas.ForceUpdateCanvases();
float textHeight = m_Msg_Txt.GetComponent().rect.height;
float textWidth = textGenerator.GetPreferredWidth(m_Msg_Txt.text, settings) / m_Msg_Txt.pixelsPerUnit;
if (textWidth <= width)
{
m_DialogBg_Img.GetComponent().sizeDelta = new Vector2(textWidth + 80, textHeight + 30);
m_DialogBg_Img.GetComponent().anchoredPosition = new Vector2((textWidth / 2) + 200, -60);
}
else
{
m_DialogBg_Img.GetComponent().sizeDelta = new Vector2(width + 80, textHeight + 30);
m_DialogBg_Img.GetComponent().anchoredPosition = new Vector2((width / 2) + 200, -60);
}
isShow = true;
}
public void LateUpdate()
{
if (isShow)
{
gameObject.GetComponent().sizeDelta = new Vector2(gameObject.GetComponent().rect.width, m_Msg_Txt.GetComponent().rect.height + 100);
isShow = false;
}
}
}