Unity UGUI制作字幕滚动效果,长字幕左右来回滚动

效果:

请添加图片描述

描述:

当文字长度小于边框时保持在边框的中间,长度大于边框时来回滚动
使用时调用此方法:
在这里插入图片描述

创建:

  • 创建一个Image并调整大小
  • 在Image下创建Text并调整Text的大小,颜色等参数
  • 将TextScroll 挂载到Image上并调整TextScroll参数

代码使用了动态添加组件,确保组件能正常使用

注意:

代码是根据RectTransform的宽高或位置进行自适应
不要使用下图红框内的锚点调整UI,如果使用会自动改变宽高或位置的值:
**加粗样式**

代码:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace TextScroll
{

    [RequireComponent(typeof(Mask))]
    [RequireComponent(typeof(CanvasGroup))]
    public class TextScroll : MonoBehaviour
    {
        [Range(0.1f, 10)]
        public float speed = 3;
        public float scrollDelayTime = 2;
        private RectTransform contentTransform; // Content的RectTransform
        Vector2 StartPoint;
        Text content;
        Color myColor = Color.clear;
        bool activate = false;
        CanvasGroup myAlpha;
        void Start()
        {
            myAlpha = GetComponent<CanvasGroup>();
            content = GetComponentInChildren<Text>();
            if (!content.GetComponent<ContentSizeFitter>())
            {
                content.gameObject.AddComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
            }
            contentTransform = content.GetComponent<RectTransform>();
            myColor = content.color;
        }
        public void ShowContent(string mContent = "")
        {
            if (myAlpha.alpha != 1)
                myAlpha.alpha = 1;
            if (mContent!="")
                content.text = mContent;

            if (coroutine!=null)
                StopCoroutine(coroutine);
            StartCoroutine(Delay());

            activate = false;
            content.color = Color.clear;
        }
        public void HideContent()
        {
            if (myAlpha.alpha != 0)
                myAlpha.alpha = 0;
            if (myColor == Color.clear)
                myColor = content.color;

            activate = false;
            content.color = Color.clear;
        }
        bool LoopDirection = true;
        void Update()
        {
            if (activate)
            {
                //contentTransform.anchoredPosition -= new Vector2(speed * 100 * Time.deltaTime, 0f);
                //if (contentTransform.anchoredPosition.x < -StartPoint.x)
                //{
                //    contentTransform.anchoredPosition = StartPoint;
                //}
                if (LoopDirection)
                {
                    contentTransform.anchoredPosition -= new Vector2(speed * 100 * Time.deltaTime, 0f);
                    if (contentTransform.anchoredPosition.x < -StartPoint.x)
                    {
                        //contentTransform.anchoredPosition = StartPoint;
                        activate = false;
                        LoopDirection = false;
                        coroutine = StartCoroutine(DelayTextScroll(scrollDelayTime));
                    }
                }
                else
                {
                    contentTransform.anchoredPosition += new Vector2(speed * 100 * Time.deltaTime, 0f);
                    if (contentTransform.anchoredPosition.x > StartPoint.x)
                    {
                        activate = false;
                        LoopDirection = true;
                        coroutine = StartCoroutine(DelayTextScroll(scrollDelayTime));
                    }
                }
            }
            //测试
            if (Input.GetKeyDown(KeyCode.O))
            {
                ShowContent("好像纯纯的东西都很容易令人入睡,比如纯音乐,比如纯牛奶,再比如你。");
            }
            if (Input.GetKeyDown(KeyCode.I))
            {
                ShowContent("音乐的浪漫之处在于:它能将封存的记忆迅速拼凑起来,你会清晰的记起当时,听这首歌的感觉和状态,就像时空真的倒回某一刻。");
            }
            if (Input.GetKeyDown(KeyCode.P))
            {
                HideContent();
            }
        }
        void TextReset()
        {
            if (contentTransform.sizeDelta.x< GetComponent<RectTransform>().sizeDelta.x)//文字不够长,显示在中间
            {
                StartPoint = contentTransform.anchoredPosition;
                StartPoint.x = 0;
                contentTransform.anchoredPosition = StartPoint;
            }
            else//文字太长,来回滚动显示
            {
                //activate = true;
                //StartPoint = contentTransform.anchoredPosition;
                //StartPoint.x = (GetComponent<RectTransform>().sizeDelta.x / 2) + (contentTransform.sizeDelta.x / 2);
                //contentTransform.anchoredPosition = StartPoint;
                StartPoint = contentTransform.anchoredPosition;
                StartPoint.x = (contentTransform.sizeDelta.x / 2)-(GetComponent<RectTransform>().sizeDelta.x / 2);
                contentTransform.anchoredPosition = StartPoint;
                LoopDirection = true;
                coroutine = StartCoroutine(DelayTextScroll(scrollDelayTime));
            }
            content.color = myColor;
        }
        Coroutine coroutine;
        IEnumerator DelayTextScroll(float mDelayTime)
        {
            yield return new WaitForSeconds(mDelayTime);
            activate = true;
        }
        IEnumerator Delay()
        {
            yield return new WaitForSeconds(0.2f);
            TextReset();
        }
        private void OnDestroy()
        {
            if (coroutine != null)
                StopCoroutine(coroutine);
        }
    }
}

Demo下载地址:点击下载

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐沢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值