[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效果图

时间若流水,恍惚间逝去
原文地址:https://www.cnblogs.com/alanshreck/p/13603974.html