在一些公告上我们经常会看到文字滚动跑马灯的效果。
那么在Unity上如何实现?
1、首先创建一个Text(或者TextMeshPro)组件,然后输入需要显示的文本内容,如图:

2、编写控制脚本TextRoll.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextRoll : MonoBehaviour
{
public float scrollSpeed = 200f;
private RectTransform textTransform;
// Start is called before the first frame update
void Start()
{
textTransform = GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
// 移动Text的位置
textTransform.position -= new Vector3(scrollSpeed * Time.deltaTime, 0, 0);
// 当Text完全移出屏幕时,将其重新放置在屏幕右侧
if (textTransform.position.x < -textTransform.rect.width)
{
textTransform.position = new Vector3(Screen.width, textTransform.position.y, textTransform.position.z);
}
}
}
把该脚本拉到步骤1创建的Text组件上,运行脚本,轻轻松松就实现文字跑马灯效果。
Unity Text文本实现滚动跑马灯效果
本文介绍了如何在Unity游戏引擎中使用Text组件和自定义脚本TextRoll实现文字跑马灯效果,包括创建Text组件、编写控制滚动速度的脚本并设置其位置逻辑。
2695

被折叠的 条评论
为什么被折叠?



