搭建直播带货平台,实现直播间的即时聊天

如今直播火的简直不像样子了。在直播间里会有观众和主播交流的功能。主要方式是主播动口(说),观众动手(打字)。这篇文章讲解一下搭建直播带货平台聊天功能的实现。这里为了更清楚的看到效果功能,我做了一个客户端单机版来讲解。(该版本为unity5.3.2f1)
需求功能是:观众新发送了聊天消息会把之前的消息顶到上面,用户也可以通过滚动聊天栏翻看之前的用户聊天记录。

先看下面gif图功能:
 


下面讲如何实现:
第一:整个功能我分了三个组件,一个蓝色背景image,一个用来滑动的image(上图图中的黄色光芒图片),一个text的预设物体。(如下图:)
为了方便将这三个物体的pivot都设置为(0,0)。
如下图(根据需求可自定义大小坐标等):


第二:给蓝色背景图片添加滑动组件和Mask组件,指定滑动目标为光芒的那个图片。
添加脚本slidertext,然后把text的预设和预设生成的父物体(光芒的那个图片)拖到对应位置。
如下图:


第三:脚本slidertext的源码:(这才是重点)
该代码和之前我写的弹幕生产的方法相似
脚本里实现了两种文本移动方法:一种是直接跳到上面的位置,另个是缓慢移动上去(上面gif图的样子),缓慢移动使用了DoTween插件来实现搭建直播带货平台的即时聊天。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System;
 
public class productsliderm : MonoBehaviour
{
    public GameObject Textslidermessage, Textslidermessage_parents;
    private GameObject texts;//生成的物体
    public Queue<GameObject> Textslider_queue = new Queue<GameObject>();//物体的队列(生成物体)  
    
   
    
     
     
    private Vector3 textpositon;
    private Quaternion textrotation;
 
    private string content;//文字内容(ceshi)
    float production_timer = 2;//生成的时间间隔
 
 
    void Update()
    {
        #region//仅测试用
 
        production_timer -= Time.deltaTime;
 
        if (production_timer <= 0f)
        {
            int i = UnityEngine.Random.Range(0, DanMuStrings.Length);//弹幕的随机内容
            content = DanMuStrings[i];
 
          
            createDanMuEntity(content);
            production_timer = 2;
        }
        #endregion
 
 
 
      
        if (Textslider_queue.Count > 100)//退出队列方法一
        {
            GameObject go = Textslider_queue.Peek();
            Textslider_queue.Dequeue();
                Destroy(go);//销毁弹幕
             
        }
    }
 
    
    public void createDanMuEntity(string textMsg)
    {
      
 
        texts = (GameObject)(Instantiate(Textslidermessage, textpositon, textrotation));//生成text框
        if (texts != null)
        {
            texts.transform.SetParent(Textslidermessage_parents.transform);
 
            texts.transform.localScale = new Vector3(1, 1, 1);
            textrotation.eulerAngles = new Vector3(0, 0, 0);
            texts.transform.localRotation = textrotation;
            texts.transform.localPosition = new Vector3(0, 0, 0);
 
             
            texts.GetComponent<Text>().text = textMsg;
 
          
 
            if (texts.GetComponent<DOTweenAnimation>() == null)//移动组件添加
                texts.AddComponent<DOTweenAnimation>();
 
          
 
 
 
            if (Textslider_queue.Count >= 1)
            {
                 
                foreach (GameObject textssliders in Textslider_queue.ToArray())//凡是在队列中的每一个都要移动
                {
                    Debug.Log("fouzei++++" + texts.GetComponent<RectTransform>().sizeDelta.y);
 
                    //直接移动
                    //textssliders.transform.localPosition= new Vector3(textssliders.transform.localPosition.x, textssliders.transform.localPosition.y + texts.GetComponent<RectTransform>().sizeDelta.y, 0f);
 
 
                    #region //缓缓移动 
 
                    Vector3 kk  = new Vector3(textssliders.transform.localPosition.x, textssliders.transform.localPosition.y + texts.GetComponent<RectTransform>().sizeDelta.y, 0f);
                   
                    textssliders.transform.DOLocalMove(kk, 2,true);
                    #endregion
                }
 
            }
 
            Textslider_queue.Enqueue(texts);//添加到队列
        }
 
        
    }
 
 
 
 
    [HideInInspector]
 
    #region 测试用
    public string[] DanMuStrings =
   {
        "这个剧情也太雷人了吧!",
        "还是好莱坞的电影经典啊,这个太次了还是好莱坞的电影经典啊,这个太次了",
        "是电锯惊魂的主角,尼玛",
        "这个游戏还是很良心的么",
        "卧槽还要花钱,这一关也太难卧槽还要花钱,这一关也太难了卧槽还要花钱,这一关也太难了卧槽还要花钱,这一关也太难了了",
        "这个游戏好棒偶",
        "全是傻逼",
        "好帅呦,你这个娘们儿",
        "欠揍啊,东北人不知道啊",
        "我去都是什么人啊,请文明用语还是好莱坞的电影经典啊,这个太次了是胸再大点就更",
        "这个还是不错的",
        "这个游戏必须顶啊",
        "还是好莱坞的电影经典啊,这个太次了还是好莱坞的电影经典啊,这个太次了怎么没有日本动作爱情片中的角色呢?",
        "好吧,这也是醉了!",
        "他只想做一个安静的美男子!"
    };
    #endregion
 
}

————————————————
声明:本文由云豹科技转发自紫龙大侠博客,如有侵权请联系作者删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值