欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2022-08-28 09:29:17|栏目:.NET代码|点击:

一、效果

二、需要动画插件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 AssetBundle打包工具示例详解

栏    目:.NET代码

下一篇:C#使用foreach遍历哈希表(hashtable)的方法

本文标题:Unity实现跑马灯效果的示例代码

本文地址:http://www.codeinn.net/misctech/212080.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有