Unity實(shí)現(xiàn)微信聊天框界面
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)微信聊天框界面的具體代碼,供大家參考,具體內(nèi)容如下
【原理】
一個(gè)聊天界面主要由三個(gè)部分組成:內(nèi)容區(qū)、可見區(qū)、滑動(dòng)條
可見區(qū)在內(nèi)容區(qū)上邊,內(nèi)容區(qū)會(huì)隨著聊天內(nèi)容變得非常長(zhǎng),但只有位于可見區(qū)的部分才能被看見,其他區(qū)域的看不見。通過滑動(dòng)條上下移動(dòng)內(nèi)容區(qū),看見的內(nèi)容發(fā)生變化。
【步驟】
- 新建一個(gè)UI->Panel,重命名為ChatPanel,添加Scroll Rect組件
- 在ChatPanel下新建一個(gè)UI->Panel,重命名為ViewPort,添加Mask組件
- 在ChatPanel下新建一個(gè)UI->Scrollbar,Direction選擇Bottom To Top
- 在ViewPort下新建一個(gè)UI->Panel,移除Image組件,重命名為Content,調(diào)整其Anchor和Pivot
- 調(diào)整ViewPort、Content、Scrollbar的Height一致

- 給Scroll Rect組件復(fù)制如下

- 創(chuàng)建聊天氣泡
效果如下

- 在bubble上添加 ContentSizeFitter和Vertical Layout Group組件,使得bubble大小隨文本大小改變。在Text上添加LayoutElement。效果如下

- 創(chuàng)建右邊的聊天氣泡,效果如下:

- 新建一個(gè)腳本,名為ChatPanelManager,掛在ChatPanel下
【腳本】
using UnityEngine;
using UnityEngine.UI;
public class ChatPanelManager : MonoBehaviour
{
public GameObject leftBubblePrefab;
public GameObject rightBubblePrefab;
private ScrollRect scrollRect;
private Scrollbar scrollbar;
private RectTransform content;
[SerializeField]
private float stepVertical; //上下兩個(gè)氣泡的垂直間隔
[SerializeField]
private float stepHorizontal; //左右兩個(gè)氣泡的水平間隔
[SerializeField]
private float maxTextWidth;//文本內(nèi)容的最大寬度
private float lastPos; //上一個(gè)氣泡最下方的位置
private float halfHeadLength;//頭像高度的一半
public void Init()
{
scrollRect = GetComponentInChildren<ScrollRect>();
scrollbar = GetComponentInChildren<Scrollbar>();
content = transform.Find("ViewPort").Find("Content").GetComponent<RectTransform>();
lastPos = 0;
halfHeadLength = leftBubblePrefab.transform.Find("head").GetComponent<RectTransform>().rect.height / 2;
}
public void AddBubble(string content, bool isMy)
{
GameObject newBubble = isMy ? Instantiate(rightBubblePrefab, this.content) : Instantiate(leftBubblePrefab, this.content);
//設(shè)置氣泡內(nèi)容
Text text = newBubble.GetComponentInChildren<Text>();
text.text = content;
if (text.preferredWidth > maxTextWidth)
{
text.GetComponent<LayoutElement>().preferredWidth = maxTextWidth;
}
//計(jì)算氣泡的水平位置
float hPos = isMy ? stepHorizontal / 2 : -stepHorizontal / 2;
//計(jì)算氣泡的垂直位置
float vPos = - stepVertical - halfHeadLength + lastPos;
newBubble.transform.localPosition = new Vector2(hPos, vPos);
//更新lastPos
Image bubbleImage = newBubble.GetComponentInChildren<Image>();
float imageLength = GetContentSizeFitterPreferredSize(bubbleImage.GetComponent<RectTransform>(), bubbleImage.GetComponent<ContentSizeFitter>()).y;
lastPos = vPos - imageLength;
//更新content的長(zhǎng)度
if (-lastPos > this.content.rect.height)
{
this.content.sizeDelta = new Vector2(this.content.rect.width, -lastPos);
}
scrollRect.verticalNormalizedPosition = 0;//使滑動(dòng)條滾輪在最下方
}
public Vector2 GetContentSizeFitterPreferredSize(RectTransform rect, ContentSizeFitter contentSizeFitter)
{
LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
return new Vector2(HandleSelfFittingAlongAxis(0, rect, contentSizeFitter),
HandleSelfFittingAlongAxis(1, rect, contentSizeFitter));
}
private float HandleSelfFittingAlongAxis(int axis, RectTransform rect, ContentSizeFitter contentSizeFitter)
{
ContentSizeFitter.FitMode fitting =
(axis == 0 ? contentSizeFitter.horizontalFit : contentSizeFitter.verticalFit);
if (fitting == ContentSizeFitter.FitMode.MinSize)
{
return LayoutUtility.GetMinSize(rect, axis);
}
else
{
return LayoutUtility.GetPreferredSize(rect, axis);
}
}
}
【測(cè)試腳本——按空格添加內(nèi)容】
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
public ChatPanelManager cpm;
private int count;
private List<string> dialogue = new List<string>();
void Start()
{
cpm.Init();
dialogue.Add("永恒之星");
dialogue.Add("永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星");
dialogue.Add("永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星永恒之星");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
cpm.AddBubble(dialogue[count],Random.Range(0,2)>0);
count++;
if (count > dialogue.Count-1)
{
count = 0;
}
}
}
}
【測(cè)試結(jié)果】

【補(bǔ)充說明】
這里核心是實(shí)現(xiàn)了聊天氣泡內(nèi)容的添加,至于頭像和name,比較簡(jiǎn)單,我們可以在AddBubble方法中自己補(bǔ)充實(shí)現(xiàn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中Winfrom默認(rèn)輸入法的設(shè)置方法
這篇文章主要介紹了C#中Winfrom默認(rèn)輸入法的設(shè)置方法,以實(shí)例形式較為詳細(xì)的分析了C#中輸入法設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Python設(shè)計(jì)模式編程中的備忘錄模式與對(duì)象池模式示例
這篇文章主要介紹了Python設(shè)計(jì)模式編程中的備忘錄模式與對(duì)象池模式,文中分別舉了表單和線程的相關(guān)示例,需要的朋友可以參考下2016-02-02
winform基于異步委托實(shí)現(xiàn)多線程搖獎(jiǎng)器
這篇文章主要介紹了winform基于異步委托實(shí)現(xiàn)多線程搖獎(jiǎng)器的方法,包含了線程的運(yùn)用及隨機(jī)數(shù)的生成,需要的朋友可以參考下2014-10-10

