Unity3d实现跑马灯广播效果
作者:心林相夕 发布时间:2022-11-13 20:08:12
标签: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;
}
}
界面
来源:https://blog.csdn.net/u014678046/article/details/119235704


猜你喜欢
- 前言这是上周在开发 C# 中使用 Proxy 代理时开发的一些思考和实践。主要需求是这样的,用户可以配置每次请求是否需要代理,用户可以配置
- Android自定义SwipeLayout仿QQ侧滑条目,供大家参考,具体内容如下先看动图 看布局文件activity_main.xml&l
- 1.控制屏幕常亮首先导入模块import brightness from '@system.brightness';接下来在
- 1.super介绍我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。用于访问父类的属性,方法,构造器2.super
- 不过在实际的工作中,很少会直接用到它。通常都是用的spring-quartz组件,直接通过配置,让spring框架来自动装配如下就是spri
- 本文实例为大家分享了Java实现简单GUI登录和注册界面的具体代码,供大家参考,具体内容如下先看效果图:登陆界面:注册界面:实现代码如下:一
- 最近一段时间 某台服务器上的一个应用总是隔一段时间就自己挂掉 用top看了看 从重新部署应用开始没有多长时间CPU占用上升得很快排查步骤1.
- 引言树形结构不论在生活中或者是开发中都是一种非常常见的结构,一个容器对象(如文件夹)下可以存放多种不同的叶子对象或者容器对象,容器对象与叶子
- 1.引入AOP依赖<dependency>
- 本文实例为大家分享C++二分查找算法,通过改变边界位置来进行查找的方法,代码如下:#include <iostream>usin
- 上篇文章中介绍了聊天功能,这里介绍通讯录是如何实现的。首先要加载公司的所有部门,树形结构,然后点击进入部门的人员列表,点击人员能查看详细信息
- 本文实例讲述了Java使用Socket通信传输文件的方法。分享给大家供大家参考,具体如下:前面几篇文章介绍了使用Java的Socket编程和
- 本文实例讲述了Java获得当前时间前指定几个小时具体时间的方法。分享给大家供大家参考,具体如下:package getBeforeHourD
- 概述一般我们在向文档添加水印时,会分为直接添加文字水印和加载图片添加图片水印两种情况。常见的,在添加文字水印时会多以声明文档版权、权威性的文
- 今天有个项目需要使用redis,并且有使用脚本的需求。但是因为之前没有写过,所以还有一点点不熟悉,今天记录一下。原因:原子操作,redis会
- 前言在日常的开发中StringBuilder大家肯定都有用过,甚至用的很多。毕竟大家都知道一个不成文的规范,当需要高频的大量的构建字符串的时
- 前言RadioGroup是继承LinearLayout,只支持横向或者竖向两种布局。所以在某些情况,比如多行多列布局,RadioGroup就
- 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下此篇博客为整理文章,供大家学习。1.首先下载common
- switch结构(开关语句)的语法switch(表达式 ){--->类型为int、char case 常量1 :---&g
- 话不多说,请看代码:package com.lxj.demo;import java.io.BufferedReader;import ja