栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

unity 聊天界面

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity 聊天界面

结果展示:



界面搭建: 1.ChattingPanel(根据名字和后缀可以知道类型,无明确类型的,为空物体)


2.WorldMyChatItem(根据名字和后缀可以知道类型,无明确类型的,为空物体)



3.WorldOtherPeopleChatItem(根据名字和后缀可以知道类型,无明确类型的,为空物体)


代码:一共三个脚本 1.ChattingPanel.cs
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
提示:如果这还不懂,就去下载项目看看吧

项目下载地址

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/898213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号