Unity实现跑马灯效果

进入Unity先建立三个floder

  • prefabs
  • Scripts
  • Scenes

建立六个Cube,在建立一个空物体挂载脚本

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


public class paoma : MonoBehaviour
{
    // Use this for initialization
    private GameObject[] _cubes;//定义一个cube数组
    private Color[] _colors;//色块数组
    void Start()
    {
        //实例化色块数组
        _colors = new Color[] { Color.black, Color.blue, Color.green, Color.red, Color.yellow, Color.white };
        //标签带MyCube
        _cubes = GameObject.FindGameObjectsWithTag("MyCube");
        //开启一个协程
        StartCoroutine("changeColorEnumerator");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            //关掉协程
            StopCoroutine("changeColorEnumerator");
        }
    }
    //协程
    IEnumerator changeColorEnumerator()
    {
        int j = 0;
        while (true)
        {
            for (int i = 0; i < _colors.Length; i++)
            {
                //_cubes中第i个赋值颜色为                                 _colors的下标I+J除六的余数,j每次增加一
                _cubes[i].GetComponent<MeshRenderer>().material.color = _colors[(i + j) % 6];
            }
            //等待三秒后,J++
            yield return new WaitForSeconds(3);
            j++;
            if (j >= int.MaxValue - 7)//给J一个范围
            {
                j = 0;
            }
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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马灯效果的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值