Unity3d实现跑马灯广播效果
程序员文章站
2022-06-09 18:08:26
本文实例为大家分享了unity3d实现跑马灯广播效果的具体代码,供大家参考,具体内容如下废话不多说,直接上代码using dg.tweening;using system.collections;us...
本文实例为大家分享了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; } }
界面
好了,就这样
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。