Unity实现跑马灯效果的示例代码

 更新时间:2022年05月06日 16:48:13   作者:龙胖胖的博客  
这篇文章主要为大家详细介绍了如何利用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实现跑马灯效果的示例代码的文章就介绍到这了,更多相关Unity跑马灯效果内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#常用GDI+文字操作汇总

    C#常用GDI+文字操作汇总

    这篇文章主要介绍了C#常用GDI+文字操作,包括文字投影、倒影、旋转等特效,对于提升程序界面的视觉效果有很大的用处,需要的朋友可以参考下
    2014-08-08
  • C#中Socket通信编程的异步实现流程分析

    C#中Socket通信编程的异步实现流程分析

    Socket编程的异步实现是指按照异步过程来实现Socket编程,即在完成了一次调用后通过状态、通知和回调来告知调用者,本文给大家介绍C#中Socket通信编程的异步实现流程分析,感兴趣的朋友一起看看吧
    2024-12-12
  • npoi2.0将datatable对象转换为excel2007示例

    npoi2.0将datatable对象转换为excel2007示例

    这篇文章主要介绍了npoi2.0将datatable对象转换为excel2007示例的相关资料
    2014-04-04
  • c# 实现发送邮件的功能

    c# 实现发送邮件的功能

    这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • Unity时间戳的使用方法

    Unity时间戳的使用方法

    这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • Unity使用ScrollRect制作摇杆

    Unity使用ScrollRect制作摇杆

    这篇文章主要为大家详细介绍了Unity使用ScrollRect制作摇杆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • WPF TextBox实现按字节长度限制输入功能

    WPF TextBox实现按字节长度限制输入功能

    这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • 关于C#理解装箱与拆箱

    关于C#理解装箱与拆箱

    这篇文章主要介绍了关于C语言理解装箱与拆箱的相关资料,需要的朋友可以参考下面文章内容
    2021-09-09
  • C#批量删除Excel重复项的实现方法

    C#批量删除Excel重复项的实现方法

    当从不同来源导入Excel数据时,可能存在重复的记录,为了确保数据的准确性,通常需要删除这些重复的行,本文将提供一个使用C# 快速查找并删除Excel重复项的免费解决方案,需要的朋友可以参考下
    2024-04-04
  • C#调用python脚本的方法详解

    C#调用python脚本的方法详解

    这篇文章主要为大家详细介绍了C#调用python脚本的方法,文中通过示例代码介绍的非常详细,感兴趣的朋友们下面随着小编来一起学习学习吧
    2023-11-11

最新评论