这篇文章主要为大家详细介绍了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;
}
}
界面
好了,就这样
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:Unity3d实现跑马灯广播效果
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14
- C#类和结构详解 2023-05-30
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# windows语音识别与朗读实例 2023-04-27
- C# List实现行转列的通用方案 2022-11-02