Unity3d实现跑马灯广播效果

 更新时间:2022年01月05日 14:28:43   作者:心林相夕  
这篇文章主要为大家详细介绍了Unity3d实现跑马灯广播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity3d实现跑马灯广播效果的具体代码,供大家参考,具体内容如下

废话不多说,直接上代码

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Utils;
//挂在UI上面
public class BroadcastUI : MonoBehaviour
{
    private bool inited = false;
    private BroadcastMan bm;
    public Transform parent;
    public GameObject prefab;
    public float parentWith = 0f;
    private Queue<GameObject> textList = new Queue<GameObject>();
    public string curPlayMsg;
    private int curPlayTime = 0;
    public float moveTime = 5f;
    private int curWaitMsgNum = 0;
    private int curEndIndex = 0;
    private void Init()
    {
        bm = this.gameObject.AddComponent<BroadcastMan>();
        parentWith = parent.GetComponent<RectTransform>().rect.width;
        moveTime = moveTime * Screen.width / 812f;
        Debug.LogError("move speed ==" + moveTime);
        inited = true;
    }
    // Start is called before the first frame update
    private void Awake()
    {
        if (!inited) Init();
    }

    private IEnumerator ScrollMsg()
    {
        curWaitMsgNum = bm.MsgCount;
        parent.gameObject.SetActiveFast(true);
        while (bm.MsgCount > 0 || curPlayTime < bm.repetTime)
        {
            if (curPlayMsg == "")
            {
                curPlayMsg = bm.GetAMsgFromQueue();
                curPlayTime = 1;
            }
            else
            {
                if (bm.repetTime > 1)
                {
                    if (curPlayTime >= bm.repetTime)
                    {
                        curPlayTime = 1;
                        curPlayMsg = bm.GetAMsgFromQueue();
                    }
                    else
                    {
                        curPlayTime++;
                    }
                }
                else
                {
                    curPlayMsg = bm.GetAMsgFromQueue();
                }
            }
            Debug.LogError("msg:" + curPlayMsg);
            GameObject text = GetAText();
            text.GetComponent<Text>().text = curPlayMsg;
            text.transform.SetParent(parent, false);
            text.transform.localPosition = prefab.transform.localPosition;
            yield return new WaitForEndOfFrame();
            float textWith = text.GetComponent<RectTransform>().rect.width;
            float moveDis = textWith + parentWith;
            float curMoveTime = moveTime * moveDis / parentWith;
            //Debug.LogError("当前移动时间,当前播放字越多,时间越长"+curMoveTime);

            Tweener tweener = text.transform.DOLocalMove(new Vector3(text.transform.localPosition.x - moveDis, text.transform.localPosition.y, 0), curMoveTime).SetEase(Ease.Linear)
            .OnComplete(() =>
            {
                curEndIndex++;
                textList.Enqueue(text);
                if (bm.MsgCount == 0 && curEndIndex == curWaitMsgNum * bm.repetTime)
                {
                    parent.gameObject.SetActiveFast(false);
                }
            });
            //Debug.LogError("下一条等待时间,当前字越多,等待时间越长"+ (0.5f * parentWith + textWith) / (moveDis / curMoveTime));
            yield return new WaitForSeconds((0.5f * parentWith + textWith) / (moveDis / curMoveTime));
        }
    }

    private void AddMsg()
    {
        List<string> msgs = new List<string>();
        msgs.Add("<color=#C0FF35>测试一下这个跑马灯的效果>>>>>>>>>>></color>");
        msgs.Add("<color=#C14848>中国第七金!祝贺庞伟、姜冉馨</color>");
        msgs.Add("<color=#6BEE5B>第10金!中国组合获得东京奥运会女子四人双桨金牌</color>");
        msgs.Add("<color=#EE5BBB>台风“烟花”</color>");
        msgs.Add("<color=#DB2136>把鲸鱼送回大海!七米长须鲸搁浅 多方力量开展救援</color>");
        bm.AddMsgToQueue(msgs);
    }

    private void OnDestory()
    {
        inited = false;

    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) && bm.MsgCount == 0)
        {
            AddMsg();
            StartCoroutine(ScrollMsg());
        }
    }

    private GameObject GetAText()
    {
        if (textList.Count > 0)
        {
            return textList.Dequeue();
        }
        return GameObject.Instantiate(prefab);
    }
}
using System.Collections.Generic;
using UnityEngine; 
//这个放收到的消息
public class BroadcastMan : MonoBehaviour
{
    private bool inited = false;
    private Queue<string> msgQueue;//灯队列.
    public int repetTime = 2;//广播重复次数
    private void Init()
    {
        msgQueue = new Queue<string>();
        inited = true;
    }
    // Start is called before the first frame update
    private void Awake()
    {
        if (!inited) Init(); 
    }

    public void AddMsgToQueue(List<string> msgs)
    {
        for (int i = 0; i < msgs.Count; i++)
        {
            msgQueue.Enqueue(msgs[i]);
        }
    }

    public string GetAMsgFromQueue()
    {
        if (msgQueue.Count > 0)
        {
            return msgQueue.Dequeue();
        }
        return "";
    }

    public int MsgCount
    {
        get => msgQueue.Count;
    }

    private void OnDestory()
    {
        inited = false;
    }
}

界面

好了,就这样

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#多线程之线程同步WaitHandle

    C#多线程之线程同步WaitHandle

    这篇文章介绍了C#多线程之线程同步WaitHandle的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#异步的世界(下)

    C#异步的世界(下)

    这篇文章主要介绍了C#异步的世界(下),对异步感兴趣的同学,可以参考下
    2021-04-04
  • C#中List和数组之间转换的方法

    C#中List和数组之间转换的方法

    这篇文章主要介绍了C#中List和数组之间转换的方法,涉及比较简单的转换技巧,需要的朋友可以参考下
    2015-02-02
  • C# 中string.split用法详解

    C# 中string.split用法详解

    本文给大家分享了C# 中string.split用法的相关知识,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-06-06
  • asp.net中调用oracle存储过程的方法

    asp.net中调用oracle存储过程的方法

    存储过程是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数来执行它,下面给大家介绍下asp.net中调用oracle存储过程的方法,需要的朋友可以参考下
    2015-08-08
  • C#中decimal保留2位有效小数的实现方法

    C#中decimal保留2位有效小数的实现方法

    这篇文章主要介绍了C#中decimal保留2位有效小数的实现方法,针对decimal变量保留2位有效小数有多种方法,可以使用Math.Round方法以及ToString先转换为字符串等操作来实现。具体实现方法感兴趣的朋友跟随小编一起看看吧
    2019-10-10
  • C#中richtextbox使用方法详解

    C#中richtextbox使用方法详解

    这篇文章主要介绍了C#中richtextbox使用方法,分析较为详尽,需要的朋友可以参考下
    2014-07-07
  • C#开发微信门户及应用(2) 微信消息处理和应答

    C#开发微信门户及应用(2) 微信消息处理和应答

    文章主要为大家详细介绍了C#开发微信门户及应用第二篇,微信消息处理和应答,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#中的协变与逆变小结

    C#中的协变与逆变小结

    这篇文章主要介绍了C#中的协变与逆变的相关知识,在泛型或委托中,如果不使用协变或逆变,那么泛型类型是一个固定类型,而使用协变或逆变的话,则泛型类型可以实现多态化,需要的朋友可以参考下
    2021-10-10

最新评论