Unity3d实现跑马灯广播效果
作者:心林相夕
这篇文章主要为大家详细介绍了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; } }
界面
好了,就这样
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。