import FGUtil from "./gear_2.3.4/fgui/FGUtil"; const { ccclass, property } = cc._decorator; @ccclass export default class CustomRich extends cc.Component { /** * 富文本節點 */ Rich: fgui.GRichTextField = null; /** * 源字符串 */ Ostr: string = "" /** * 表情數據,記錄當前第幾幀 */ emoList: any = {} /** * 刷新幀數計時 */ time: number = 0 initInfo(str: string, cusRich: fgui.GRichTextField) { fairygui.UIConfig.linkUnderline = false this.Ostr = str; this.Rich = cusRich this.emoList = []; var hasEmo = /\[emo=#\d{1,3}]/g; var arr = str.match(hasEmo); for (let i in arr) { // 獲取表情id var id = arr[i].split("=#")[1]; id = id.substr(0, id.length - 1); // 檢測是否存在表情 var prefix = ("000" + id.toString()).substr(-3); var frameName = prefix + "0000"; let item = fgui.UIPackage.getItemByURL(`ui://main_ui/${frameName}`); if (item != null) this.emoList[`${id}`] = { frame: 0, prefix: prefix } } var newStr = str.replace(hasEmo, (f, m) => { // 獲取表情id var id = f.split("=#")[1]; id = id.substr(0, id.length - 1); // 檢測是否存在表情 if (!this.emoList[id]) return ""; var emoName = this.emoList[id].prefix + (("000" + this.emoList[id].frame).substr(-4)) var temp = `[img]ui://main_ui/${emoName}[/img]` return temp; }) cusRich.text = newStr; cusRich.on(fgui.Event.LINK, this.onClickLink, this) } onClickLink(p) { console.log(p) } update(dt) { // 1秒6幀 this.time += dt; if (this.time > 0.16) { this.time = 0; } else return; var frameName = ""; for (let key in this.emoList) { this.emoList[key].frame++; frameName = this.emoList[key].prefix + (("000" + this.emoList[key].frame).substr(-4)); let item = fgui.UIPackage.getItemByURL(`ui://main_ui/${frameName}`); if (item == null) { this.emoList[key].frame = 0; } } var str = this.Ostr; var hasEmo = /\[emo=#\d{1,3}]/g; var newStr = str.replace(hasEmo, (f, m) => { // 獲取表情id var id = f.split("=#")[1]; id = id.substr(0, id.length - 1); // 檢測是否存在表情 if (!this.emoList[id]) return ""; var emoName = this.emoList[id].prefix + (("000" + this.emoList[id].frame).substr(-4)) var temp = `[img]ui://main_ui/${emoName}[/img]` return temp; }) this.Rich.text = newStr } }