添加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效果圖