Unity UGUI图文混排源码(四) : 聊天气泡

发表于2017-04-26
评论1 2k浏览

怎么使用UGUI图文混排做聊天气泡,考虑到有些人没有这方面的经验,下面就给大家介绍下利用UGUI图文混排实现聊天气泡的方法,一起来看看吧。


这里有同学建议在做聊天气泡时,可以更改为一张图集对应多个Text,这样能节省资源,不过我突然想到每个Text一个图集,可以随时更换图集,这样表情图更丰富一些,于是我就先将现有的聊天demo改为了聊天气泡,于是一张图集对应多个Text的功能,只有下次更新,哈哈。


1.我更新了原来的表情文件,不过资源也来源网络


2.在图文三的时候,为了做动态表情,将索引改为了ID,这里我有将ID改为了name,代码的检测中只要包含了name的图片都会加在动态数组里

[csharp] view plain copy
 
  1. #region 解析动画标签  
  2.            List<string> tempListName = new List<string>();  
  3.            for (int i = 0; i < m_spriteAsset.listSpriteInfor.Count; i++)  
  4.            {  
  5.               // Debug.Log((m_spriteAsset.listSpriteInfor[i].name));  
  6.                if (m_spriteAsset.listSpriteInfor[i].name.Contains(match.Groups[1].Value))  
  7.                {  
  8.                    tempListName.Add(m_spriteAsset.listSpriteInfor[i].name);  
  9.                }  
  10.            }  
  11.            if (tempListName.Count > 0)  
  12.            {  
  13.                SpriteTagInfor[] tempArrayTag = new SpriteTagInfor[tempListName.Count];  
  14.                for (int i = 0; i < tempArrayTag.Length; i++)  
  15.                {  
  16.                    tempArrayTag[i] = new SpriteTagInfor();  
  17.                    tempArrayTag[i].name = tempListName[i];  
  18.                    tempArrayTag[i].index = match.Index;  
  19.                    tempArrayTag[i].size = new Vector2(float.Parse(match.Groups[2].Value) * float.Parse(match.Groups[3].Value), float.Parse(match.Groups[2].Value));  
  20.                    tempArrayTag[i].Length = match.Length;  
  21.                }  
  22.                listTagInfor.Add(tempArrayTag[0]);  
  23.                m_AnimSpiteTag.Add(listTagInfor.Count - 1, tempArrayTag);  
  24.                m_AnimIndex.Add(listTagInfor.Count - 1);  
  25.            }  
  26. endregion  

3.于是我们就可以更新一下表情文件的命令,一个动态表情的名称,尽量规范,大概看一下我的命名


4.然后就可以开始制作聊天气泡了,聊天气泡主要的因素就在于Item的制作,这里顺便看一下文本的表情表用格式,其实跟之前也是一样的


5.检测到输入的时候,再不断的生成预设就ok啦,具体一些参数,看看脚本应该能理解,并不难,于是就没怎么写出注释

[csharp] view plain copy
 
  1. float chatHeight = 10.0f;  
  2.     float PlayerHight = 64.0f;  
  3.     void ClickSendMessageBtn()  
  4.     {  
  5.         if (inputText.text.Trim() == null || inputText.text.Trim() == "")  
  6.             return;  
  7.   
  8.         GameObject tempChatItem = Instantiate(goprefab) as GameObject;  
  9.         tempChatItem.transform.parent = goContent.transform;  
  10.         tempChatItem.transform.localScale = Vector3.one;  
  11.         InlieText tempChatText = tempChatItem.transform.FindChild("Text").GetComponent();  
  12.         tempChatText.text = inputText.text.Trim();  
  13.         if (tempChatText.preferredWidth + 20.0f < 105.0f)  
  14.         {  
  15.             tempChatItem.GetComponent().sizeDelta = new Vector2(105.0f, tempChatText.preferredHeight + 50.0f);  
  16.         }  
  17.         else if (tempChatText.preferredWidth + 20.0f > tempChatText.rectTransform.sizeDelta.x)  
  18.         {  
  19.             tempChatItem.GetComponent().sizeDelta = new Vector2(tempChatText.rectTransform.sizeDelta.x + 20.0f, tempChatText.preferredHeight + 50.0f);  
  20.         }  
  21.         else  
  22.         {  
  23.             tempChatItem.GetComponent().sizeDelta = new Vector2(tempChatText.preferredWidth + 20.0f, tempChatText.preferredHeight + 50.0f);  
  24.         }  
  25.   
  26.         tempChatItem.SetActive(true);  
  27.         tempChatText.SetVerticesDirty();  
  28.         tempChatItem.GetComponent().anchoredPosition = new Vector3(-10.0f, -chatHeight);  
  29.         chatHeight += tempChatText.preferredHeight + 50.0f + PlayerHight + 10.0f;  
  30.         if (chatHeight > goContent.GetComponent().sizeDelta.y)  
  31.         {  
  32.             goContent.GetComponent().sizeDelta = new Vector2(goContent.GetComponent().sizeDelta.x, chatHeight);  
  33.         }  
  34.         while (scrollbarVertical.value > 0.01f)  
  35.         {  
  36.             scrollbarVertical.value = 0.0f;  
  37.         }  
  38.         inputText.text = "";  
  39.     }  
6.最后看一下效果截图,怕动态加载不出,多放一张静态图片:


如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引