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; } }
界面
好瞭,就這樣
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。