Unity3d實(shí)現(xiàn)跑馬燈廣播效果
本文實(shí)例為大家分享了Unity3d實(shí)現(xiàn)跑馬燈廣播效果的具體代碼,供大家參考,具體內(nèi)容如下
廢話不多說,直接上代碼
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("當(dāng)前移動(dòng)時(shí)間,當(dāng)前播放字越多,時(shí)間越長(zhǎng)"+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("下一條等待時(shí)間,當(dāng)前字越多,等待時(shí)間越長(zhǎng)"+ (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>測(cè)試一下這個(gè)跑馬燈的效果>>>>>>>>>>></color>");
? ? ? ? msgs.Add("<color=#C14848>中國第七金!祝賀龐偉、姜冉馨</color>");
? ? ? ? msgs.Add("<color=#6BEE5B>第10金!中國組合獲得東京奧運(yùn)會(huì)女子四人雙槳金牌</color>");
? ? ? ? msgs.Add("<color=#EE5BBB>臺(tái)風(fēng)“煙花”</color>");
? ? ? ? msgs.Add("<color=#DB2136>把鯨魚送回大海!七米長(zhǎng)須鯨擱淺 多方力量開展救援</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;?
//這個(gè)放收到的消息
public class BroadcastMan : MonoBehaviour
{
? ? private bool inited = false;
? ? private Queue<string> msgQueue;//燈隊(duì)列.
? ? public int repetTime = 2;//廣播重復(fù)次數(shù)
? ? 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;
? ? }
}界面

好了,就這樣
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)例
這篇文章主要介紹了DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)現(xiàn)方法,以實(shí)例形式講述了創(chuàng)建Drill-Down樣式的Title原理與實(shí)現(xiàn)過程,需要的朋友可以參考下2014-10-10
使用checked語句防止數(shù)據(jù)溢出的解決方法
本篇文章是對(duì)用checked語句防止數(shù)據(jù)溢出的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
.NET企業(yè)級(jí)項(xiàng)目中遇到的國際化問題和解決方法
這篇文章主要介紹了.NET企業(yè)級(jí)項(xiàng)目中遇到的國際化問題和解決方法,說明了理國際化問題的一些典型例子和經(jīng)驗(yàn)之談,需要的朋友可以參考下2014-07-07
c# String擴(kuò)展 讓你在PadLeft和PadRight時(shí)不再受單雙字節(jié)問題困擾
這篇文章主要介紹了c# String擴(kuò)展 讓你在PadLeft和PadRight時(shí)不再受單雙字節(jié)問題困擾,需要的朋友可以參考下2020-04-04
Unity實(shí)現(xiàn)移動(dòng)端手勢(shì)解鎖功能
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)移動(dòng)端手勢(shì)解鎖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
C# Socket 發(fā)送&接收&返回 簡(jiǎn)單應(yīng)用實(shí)例
下面小編就為大家分享一篇C# Socket 發(fā)送&接收&返回 簡(jiǎn)單應(yīng)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11
C#實(shí)現(xiàn)位圖轉(zhuǎn)換成圖標(biāo)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)位圖轉(zhuǎn)換成圖標(biāo)的方法,可實(shí)現(xiàn)將bmp格式位圖轉(zhuǎn)換成ico格式圖標(biāo)的功能,需要的朋友可以參考下2015-06-06

