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