Unity3d實(shí)現(xiàn)跑馬燈廣播效果
更新時(shí)間:2022年01月05日 14:28:43 作者:心林相夕
這篇文章主要為大家詳細(xì)介紹了Unity3d實(shí)現(xiàn)跑馬燈廣播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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í)間越長"+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í)間越長"+ (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>測試一下這個(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>把鯨魚送回大海!七米長須鯨擱淺 多方力量開展救援</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; ? ? } }
界面
好了,就這樣
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net中調(diào)用oracle存儲(chǔ)過程的方法
存儲(chǔ)過程是在大型數(shù)據(jù)庫系統(tǒng)中,一組為了完成特定功能的SQL 語句集,存儲(chǔ)在數(shù)據(jù)庫中經(jīng)過第一次編譯后再次調(diào)用不需要再次編譯,用戶通過指定存儲(chǔ)過程的名字并給出參數(shù)來執(zhí)行它,下面給大家介紹下asp.net中調(diào)用oracle存儲(chǔ)過程的方法,需要的朋友可以參考下2015-08-08C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法,針對(duì)decimal變量保留2位有效小數(shù)有多種方法,可以使用Math.Round方法以及ToString先轉(zhuǎn)換為字符串等操作來實(shí)現(xiàn)。具體實(shí)現(xiàn)方法感興趣的朋友跟隨小編一起看看吧2019-10-10C#開發(fā)微信門戶及應(yīng)用(2) 微信消息處理和應(yīng)答
文章主要為大家詳細(xì)介紹了C#開發(fā)微信門戶及應(yīng)用第二篇,微信消息處理和應(yīng)答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06C#TreeView 無限級(jí)別分類實(shí)現(xiàn)方法
2013-04-04