untiy实现跑马灯

不得不说,在敲代码的过程当中一定要有一个面向对象的思想,还有一定不要钻牛角尖,这条路行不通就换条路去走。
废话不多说了,就直接上代码吧!
在做的过程当中,需要把文字及其他的背景当做一个对象,有消息来就创建这个对象让他滚动,在创建之前一定要记得清除一下:

以下我只赋上对象滚动的代码:

   using DG.Tweening;
    using Hades.Proto.Com;
    using Hades.Ui.Comm;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using UnityEngine;
    using UnityEngine.UI;
    
    namespace Hades.UI.Home
    {
        class ImageBgWidget : Widget
        {
            private Queue<string> _MsgQueue;//在创建这个对象的时候传过来的参数赋到这里。
            private Text _textLamp;
            private float _beginX;
            private float _leftX;
            private float _speed;
            private float _txtWidth;
            private float _distance;
            private float _duration;
    
            private void Awake()
            {
                _textLamp = transform.Find("TextLamp").GetComponent<Text>();
                if(gameObject.activeSelf==false)
                {
                    gameObject.SetActive(true);
                }
            }
    
            public void Init(Queue<string> MsgQueue)
            {
                _MsgQueue = MsgQueue;
            }
    
            protected override void Start()
            {
                base.Start();
                _beginX = 100;
                _duration = 5f;
                _speed = 60f;
                _txtWidth = _textLamp.preferredWidth;//文本自身的长度.  
                _leftX =- _txtWidth-150;
                _distance = _beginX - _leftX + _txtWidth;
                _duration = _distance / _speed;
                if (_MsgQueue != null)
                {
                    string msg = _MsgQueue.Dequeue();//Queue  队列  先入先出==》Dequeue出栈
                    _textLamp.text = msg;
                }
                StartCoroutine(ScrollText());
            }
    
            IEnumerator ScrollText()
            {
                float loop = 1;
                while (loop-- > 0)
                {
                    Vector3 pos = _textLamp.rectTransform.localPosition;
                    _textLamp.rectTransform.localPosition = new Vector3(_beginX, pos.y, pos.z);
                    _textLamp.rectTransform.DOLocalMoveX(-_distance, _duration).SetEase(Ease.Linear);
                    yield return new WaitForSeconds(_duration);
                }
                gameObject.SetActive(false);
                yield break;
            }
        }
    }

有问题在下方评论就可以啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值