[Unity3D] 字体垂直自动滚动&鼠标拖拽滑动字体滚动

复制代码
 添加UI及组件:
    Hierarchy面板下右键UI添加
    Canvas(一级层)
      Panel(二级层)
        
Panel下添加组件Scroll Rect
          属性:
          Content:   选择Text文本
          Horizontal:    左右滚动    自由选择
          Vertical:    上下滚动    自由选择
          Movement Type:滚动类型(自由选择)
                  Unrestricted:  无限制的滚动,无回滚
                  Elastic:     有限制的滚动,有回滚
                  Clamped:    有限制的回滚,无回滚
          Scroll Sensitivity:滚动的灵敏度  自由设置
        Panel下添加UI→Text文本(三级层)
           属性:Text  在这文本框内输入要滚动的内容

                添加组件
            Content Size Fitter
            属性:
            PreferredSize:  勾选
            Panel下添加组件Mask遮罩(三级层)
                属性:Show Mask Graphic 取消勾选
           勾选        显示遮罩图层
           不勾选       不显示
           显不显示可自选






代码:

1
using System.Collections; 2 using System.Collections.Generic; 3 using System.Threading.Tasks; 4 using UnityEngine; 5 using UnityEngine.UI; 6 7 public class TextMoveForUp : MonoBehaviour { 8 9 /* 当前时间,用来在Update中固定时间移动字体的,不然移动速度无法控制 */ 10 private float CurrentTime; 11 12 /* 判断当前字体是否移动 */ 13 private bool IsMove = false; 14 15 /* 字体移动速度 */ 16 public float FontMoveSpeed = 5; 17 18 private void Awake() { 19 /* 为了让故事背景字体从下往上播放,不然是从中间开始播放的 */ 20 this.transform.localPosition = new Vector3(0,-2000,0); 21 } 22 23 24 void Update() { 25 /* 调用字体移动方法 */ 26 FontMoveUp(); 27 28 } 29 30 private void FontMoveUp() { 31 32 CurrentTime += Time.deltaTime; 33 /* 限制每0.2秒移动一次 */ 34 if (CurrentTime >= 0.2f) { 35 float y = this.transform.localPosition.y; 36 this.transform.localPosition = new Vector3(0, y + FontMoveSpeed, 0); 37 IsMove = true; 38 /* 循环播放,让字体从显示框上方消失后,在显示框下方再出现,如此循环*/ 39 if (y >= 2400) { 40 this.transform.localPosition = new Vector3(0, -2400, 0); 41 } 42 } 43 44 if (IsMove == true) { 45 CurrentTime = 0; 46 IsMove = false; 47 } 48 49 } 50 }
复制代码

附GIF效果图

posted @   伊凡晴天  阅读(2122)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示