1设置
首先设置Text组件的overflow为scroll,如下图所示
2挂载脚本
将TextScroll脚本挂载到Text上,该脚本主要实现了手指或鼠标滑动文本的功能。
import Script = Laya.Script;
import Text = Laya.Text;
import Event = Laya.Event;export default class TextScroll extends Script {private _text: Text;private _mouseX: number = 0;private _mouseY: number = 0;onAwake(): void {this._text = this.owner as Text;}onMouseDown(e: Laya.Event): void {this._mouseX = this._text.mouseX;this._mouseY = this._text.mouseY;}onMouseMove(e: Event): void {let deltaX = this._mouseX - this._text.mouseX;let deltaY = this._mouseY - this._text.mouseY;this._text.scrollX = Math.max(0, Math.min(this._text.maxScrollX, this._text.scrollX + deltaX));this._text.scrollY = Math.max(0, Math.min(this._text.maxScrollY, this._text.scrollY + deltaY));this._mouseX = this._text.mouseX;this._mouseY = this._text.mouseY;}
}
3效果