Unity3d实现跑马灯广播效果
作者:心林相夕 时间:2022-11-13 20:08:12
本文实例为大家分享了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
标签:Unity3d,跑马灯,广播
0
投稿
猜你喜欢
利用Java工具类Hutool实现验证码校验功能
2022-07-15 02:31:22
Java设计模式之java桥接模式详解
2023-10-15 15:19:10
android使用Path绘制出多边形
2021-11-11 19:53:38
Android实现发送短信验证码倒计时功能示例
2023-04-03 09:07:48
javaweb 项目初始配置的方法步骤
2021-12-23 17:34:23
C# Color.FromArgb()及系统颜色对照表一览
2021-05-24 08:21:02
Android小程序实现选项菜单
2022-04-12 00:34:01
如何使用两个栈实现队列Java
2023-11-29 17:48:09
使用Docker搭建Java环境的步骤方法
2022-09-07 16:03:59
java 使用BigDecimal进行货币金额计算的操作
2023-12-05 23:19:34
Android画板开发之撤销反撤销功能
2023-11-28 10:08:37
Java 继承与多态的深入理解
2023-10-05 04:25:41
Spring Boot实现发送邮件
2023-11-08 22:19:40
解决spring boot2集成activiti6踩过的坑
2022-03-31 16:46:06
java用applet画图用到的方法(涉及双缓冲)
2021-07-09 17:27:32
SpringBoot详细讲解静态资源导入的实现
2023-07-26 13:23:21
JavaCV实现照片马赛克效果
2023-04-27 15:55:14
详解Java使用JMH进行基准性能测试
2021-08-01 12:22:32
C#过滤DataTable中空数据和重复数据的示例代码
2022-03-15 19:33:45
Java NIO实现聊天功能
2022-06-12 08:31:31