Unity制作一个BroadcastUI 跑马灯文字广播
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=#5BBEEE>奥运女足-难求一胜!中国2-8荷兰小组赛2负1平惨遭淘汰,真是太可惜了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</color>"); msgs.Add("<color=#5B63EE>暴</color>"); msgs.Add("<color=#BF5BEE>南京市疾控中心 :已锁定疫情病毒毒株为德尔塔毒株</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; } }