Unity Text 文字滚动循环显示

请添加图片描述
在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class AutoDanMu : MonoBehaviour
{
    public enum TITLES
    {
        TITLE,
        TITLE2
    }

    private TITLES _titles;

    public Text title, title2;
    public Button btn_Click;


    public int moveSpeed;
    
    private int minWidth, maxWidth;
    private int width;

    private RectTransform tran;


    private bool isCanStart;

    private void Start()
    {
        Init("qwerttasdfgh");
    }

    private void Update()
    {
        if (isCanStart)
        {
            switch (_titles)
            {
                case TITLES.TITLE:
                    title.GetComponent<RectTransform>().anchoredPosition = new Vector2(
                        title.GetComponent<RectTransform>().anchoredPosition.x - Time.deltaTime * moveSpeed,
                        title.GetComponent<RectTransform>().anchoredPosition.y);

                    if (title.GetComponent<RectTransform>().anchoredPosition.x <= -width / 2) 
                    {
                        title2.GetComponent<RectTransform>().anchoredPosition = new Vector2(
                            title2.GetComponent<RectTransform>().anchoredPosition.x - Time.deltaTime * moveSpeed,
                            title2.GetComponent<RectTransform>().anchoredPosition.y);
                    }

                    LayoutRebuilder.ForceRebuildLayoutImmediate(title.GetComponent<RectTransform>());
                    if (title.GetComponent<RectTransform>().anchoredPosition.x <=
                        -width / 2 - title.GetComponent<RectTransform>().rect.width / 2)
                    {
                        title.GetComponent<RectTransform>().anchoredPosition =
                            new Vector2(tran.rect.width / 2 + title.GetComponent<RectTransform>().rect.width / 2, 0);
                        _titles = TITLES.TITLE2;
                    }

                    break;
                case TITLES.TITLE2:
                    title2.GetComponent<RectTransform>().anchoredPosition = new Vector2(
                        title2.GetComponent<RectTransform>().anchoredPosition.x - Time.deltaTime * moveSpeed,
                        title2.GetComponent<RectTransform>().anchoredPosition.y);

                    if (title2.GetComponent<RectTransform>().anchoredPosition.x <= -width / 2) 
                    {
                        title.GetComponent<RectTransform>().anchoredPosition = new Vector2(
                            title.GetComponent<RectTransform>().anchoredPosition.x - Time.deltaTime * moveSpeed,
                            title.GetComponent<RectTransform>().anchoredPosition.y);
                    }

                    LayoutRebuilder.ForceRebuildLayoutImmediate(title2.GetComponent<RectTransform>());
                    if (title2.GetComponent<RectTransform>().anchoredPosition.x <=
                        -width / 2 - title2.GetComponent<RectTransform>().rect.width / 2)
                    {
                        title2.GetComponent<RectTransform>().anchoredPosition =
                            new Vector2(tran.rect.width / 2 + title2.GetComponent<RectTransform>().rect.width / 2, 0);
                        _titles = TITLES.TITLE;
                    }

                    break;
            }
        }
    }

    public void Init(string content)
    {
        btn_Click.onClick.AddListener(() => { Debug.Log(123); });


        _titles = TITLES.TITLE;

        title.text = content;
        title2.text = content;

        LayoutRebuilder.ForceRebuildLayoutImmediate(title.GetComponent<RectTransform>());
        LayoutRebuilder.ForceRebuildLayoutImmediate(title2.GetComponent<RectTransform>());
        minWidth = (int)title.rectTransform.rect.width * 2 / 3;
        maxWidth = (int)title.rectTransform.rect.width;
        width = Random.Range(minWidth, maxWidth);

        Debug.Log(minWidth + "    " + maxWidth);

        tran = transform.GetComponent<RectTransform>();
        tran.sizeDelta =
            new Vector2(width, tran.rect.height);

        title.GetComponent<RectTransform>().anchoredPosition =
            new Vector2(tran.rect.width / 2 + title.GetComponent<RectTransform>().rect.width / 2, 0);

        Debug.Log(title.GetComponent<RectTransform>().rect.size.x);

        title2.GetComponent<RectTransform>().anchoredPosition =
            new Vector2(tran.rect.width / 2 + title2.GetComponent<RectTransform>().rect.width / 2, 0);

        isCanStart = true;
    }
}

代码很简单,挂在弹幕名称的组件上就可以了。弹幕框的长度是根据输入的字符长度来定义的。这样能确保输入过长或过短的字符也能正常滚动循环

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值