我是靠谱客的博主 矮小大侠,这篇文章主要介绍Unity3d实现跑马灯广播效果,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Unity3d实现跑马灯广播效果的具体代码,供大家参考,具体内容如下

废话不多说,直接上代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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);     } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;     } }

界面

好了,就这样

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是矮小大侠最近收集整理的关于Unity3d实现跑马灯广播效果的全部内容,更多相关Unity3d实现跑马灯广播效果内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(132)

评论列表共有 0 条评论

立即
投稿
返回
顶部