You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.2 KiB
92 lines
2.2 KiB
//*********************
|
|
// create by 流云
|
|
// time: 2021.01.05
|
|
// desc: 打印字符组件
|
|
//*********************
|
|
|
|
import { Component, Label, RichText, _decorator, Node } from "cc";
|
|
import { DEV } from "cc/env";
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
/**
|
|
* 打印字符组件
|
|
*/
|
|
@ccclass
|
|
export default class PrintChar extends Component {
|
|
|
|
|
|
@property({ tooltip: DEV && '打印速度' })
|
|
private speed: number = 1;
|
|
|
|
@property({ tooltip: DEV && '自动播放' })
|
|
private playOnLoad: boolean = false;
|
|
|
|
private _lb: Label | RichText ;
|
|
private _text: string = `少得了开关就傻掉了看风景撒了国家是,奥斯卡了刚加上了,打飞机按时,第三方`;
|
|
private _strArr = [];
|
|
private _curent_index = 0;
|
|
|
|
|
|
onLoad() {
|
|
this._lb = this.node.getComponent(Label);
|
|
if (!this._lb)
|
|
this._lb = this.node.getComponent(RichText);
|
|
if (this._lb)
|
|
this._text = this._lb.string;
|
|
else {
|
|
this._lb = this.node.addComponent(Label);
|
|
}
|
|
this._lb.string = ``;
|
|
}
|
|
|
|
onEnable() {
|
|
if (this.playOnLoad && this._text != ``)
|
|
this.startPrint();
|
|
}
|
|
|
|
onDisable() {
|
|
this.unscheduleAllCallbacks();
|
|
this._lb.string = "";
|
|
}
|
|
|
|
public print(str: string, speed = 1) {
|
|
this.speed = speed;
|
|
this._text = str;
|
|
if (this._lb)
|
|
this.startPrint();
|
|
}
|
|
|
|
public clear() {
|
|
this._text = ``;
|
|
this._lb.string = ``;
|
|
}
|
|
|
|
private startPrint() {
|
|
this._strArr = [];
|
|
this._curent_index = 0;
|
|
this._lb.string = "";
|
|
this.unscheduleAllCallbacks();
|
|
for (let i = 0; i < this._text.length; i++) {
|
|
this._strArr.push(this._text.substring(0, i + 1));
|
|
}
|
|
this.schedule(() => {
|
|
this.next();
|
|
}, this.speed);
|
|
}
|
|
|
|
private next() {
|
|
if (this._curent_index < this._text.length) {
|
|
this.showContent(this._strArr[this._curent_index]);
|
|
}
|
|
else {
|
|
this.unscheduleAllCallbacks();
|
|
}
|
|
this._curent_index++;
|
|
}
|
|
|
|
private showContent(str) {
|
|
if (this._lb)
|
|
this._lb.string = str;
|
|
}
|
|
}
|
|
|