網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2022-03-19 C#?壓榨cpu的辦法(推薦)_C#教程
- 2022-10-03 numpy中nan_to_num的具體使用_python
- 2022-10-18 linux下shell腳本備份文件的方法實現_linux shell
- 2023-03-22 React?Native全面屏狀態欄和底部導航欄適配教程詳細講解_React
- 2022-07-18 BeanUtils工具類
- 2021-11-26 linux服務器磁盤空間擴充方法_Linux
- 2022-05-08 Python與C語言分別完成排序流程_python
- 2023-06-16 C語言中如何在結構體內定義函數_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支