Unity 跑马灯效果

一、效果在这里插入图片描述
二、需要动画插件DOTween
三、脚本
1.每个格子上的脚本文件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class MarqueeUIItem : MonoBehaviour
{
    private RawImage m_RawImage;
    private string thisIndex;
    private Coroutine m_coroutine;
    private void Start()
    {
        m_RawImage = GetComponent<RawImage>();

        thisIndex = transform.GetSiblingIndex().ToString();
    }
    public void UpdateImageColorA()
    {
        KillDOTween();
        m_RawImage.color = Color.white;
        m_coroutine= StartCoroutine(ShowUI());
    }
    private IEnumerator ShowUI()
    {
        yield return new WaitForSeconds(0.1F);
        m_RawImage.DOColor(Color.clear, 1.5f).SetId(thisIndex);
    }
    public void KillDOTween()
    {
        if (DOTween.IsTweening(thisIndex))
        {
            if (m_coroutine != null)
            {
                StopCoroutine(m_coroutine);
            }
            DOTween.Kill(thisIndex);      
        }
    }
}

2.管理脚本文件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MarqueeUIManager : MonoBehaviour
{
    [Header("时间间隔")]
    public float time_interval=0.05f;
    public RawImage m_firstImage; 
    public RawImage[] m_allImage;
 
    private Coroutine m_LeftCor;
    private Coroutine m_RightCor;
    private void Start()
    {
        m_firstImage.color=Color.clear;
        for (int i = 0; i < m_allImage.Length; i++)
        {
            m_allImage[i].color=Color.clear;
        }               
    }
        private void Update()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            LeftRotationUI();
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            RightRotationUI();
        }
    }
    private void LeftRotationUI()
    {
        if (m_RightCor != null)
        {
            StopCoroutine(m_RightCor);
        }
          if(m_LeftCor!=null)
        {
            StopCoroutine(m_LeftCor);
        }
        m_LeftCor = StartCoroutine(LeftRoatation());
    }
    private void RightRotationUI()
    {
        if (m_LeftCor != null)
        {
            StopCoroutine(m_LeftCor);
        }
           if (m_RightCor != null)
        {
            StopCoroutine(m_RightCor);
        }
        m_RightCor = StartCoroutine(RightRoatation());
    }


    private IEnumerator LeftRoatation()
    {
        KillAllDOTween();
        yield return new WaitForSeconds(0.01f);
        m_firstImage.GetComponent<MarqueeUIItem>().UpdateImageColorA();
        yield return new WaitForSeconds(time_interval);
        for (int i = m_allImage.Length-1; i > -1; i--)
        {
            m_allImage[i].GetComponent<MarqueeUIItem>().UpdateImageColorA();
            yield return new WaitForSeconds(time_interval);
        }
        yield return new WaitForSeconds(time_interval);
        m_firstImage.GetComponent<MarqueeUIItem>().UpdateImageColorA();

    }
    private IEnumerator RightRoatation()
    {
        KillAllDOTween();
        yield return new WaitForSeconds(0.01f);
        m_firstImage.GetComponent<MarqueeUIItem>().UpdateImageColorA();
        yield return new WaitForSeconds(time_interval);
        for (int i = 0; i < m_allImage.Length; i++)
        {
            m_allImage[i].GetComponent<MarqueeUIItem>().UpdateImageColorA();
            yield return new WaitForSeconds(time_interval);
        }
        yield return new WaitForSeconds(time_interval);
        m_firstImage.GetComponent<MarqueeUIItem>().UpdateImageColorA();
    }
    private void KillAllDOTween()
    {
        m_firstImage.GetComponent<MarqueeUIItem>().KillDOTween();
        m_firstImage.color = Color.clear;
        for (int i = 0; i < m_allImage.Length; i++)
        {
            m_allImage[i].GetComponent<MarqueeUIItem>().KillDOTween();
            m_allImage[i].color = Color.clear;
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

脚本效率不高

Unity中实现跑马灯效果,可以使用UI组件中的Text组件,结合代码控制实现文字的滚动。 具体实现步骤如下: 1. 创建一个空的GameObject,命名为MarqueeText。 2. 在MarqueeText下创建一个Text组件,并设置好文字内容、字体大小、颜色等。 3. 在MarqueeText下再创建一个空的GameObject,命名为Content,用于容纳Text组件。 4. 将Text组件拖拽到Content下,调整Content的位置和大小,使Text显示在Content的左边。 5. 编写脚本MarqueeText.cs,用于控制Text的滚动。代码如下: ```csharp using UnityEngine; using UnityEngine.UI; public class MarqueeText : MonoBehaviour { public float speed = 50f; // 滚动速度 private RectTransform contentRect; // Content的RectTransform组件 private Text text; // Text组件 private float textWidth; // Text的宽度 private float contentWidth; // Content的宽度 void Start() { contentRect = transform.Find("Content").GetComponent<RectTransform>(); text = transform.Find("Content/Text").GetComponent<Text>(); textWidth = text.preferredWidth; contentWidth = contentRect.rect.width; } void Update() { contentRect.localPosition -= new Vector3(speed * Time.deltaTime, 0, 0); if (contentRect.localPosition.x <= -textWidth) { contentRect.localPosition += new Vector3(contentWidth + textWidth, 0, 0); } } } ``` 6. 将MarqueeText.cs挂载到MarqueeText对象上,并设置好速度。 7. 运行程序,就可以看到文字在Content中滚动了。 以上就是实现Unity跑马灯效果的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值