试了下如果是bmfont char模式可以将assembler.updateColor
方法替换,在写入顶点颜色数据的时候直接把后面几个字符刷成透明色。这样可以保证字体的位置始终不变。
let assembler = this.lblText._assembler;
assembler.updateColor = (comp, color) => {
let uintVerts = assembler._renderData.uintVDatas[0];
if (!uintVerts) return;
color = color != null ? color : comp.node.color._val;
let floatsPerVert = assembler.floatsPerVert;
let colorOffset = assembler.colorOffset;
let k = 0;
// showCount表示要显示的字符数,在外部控制
let showVertsCount = that._showCount * 4;
for (let i = colorOffset, l = uintVerts.length; i < l; i += floatsPerVert) {
if (k < showVertsCount)
uintVerts[i] = color;
else
uintVerts[i] = 0; // 后面的字符刷成透明色
++ k;
}
}
调整显示字符数的时候,增加showCount,并设置颜色的脏标记即可
this._showCount += 1;
this.lblText.node._renderFlag |= cc.RenderFlow.FLAG_COLOR;
不过对于非bmfont或非char模式,这种方式没有效果。
字体渲染的不同情况太多,已经看晕了。