在Creator 2.0以前,RichText是支持结点颜色的,如果没有特别指定,默认颜色会用node.color的。
其实这个特性还挺方便的,我们项目中很多地方用了这个机制,但是升级以后,这个特性没有了,导致我们很多界面的颜色不对,要一个个调整又比较麻烦。
不知道有没有其他人也有用过这个特性的,如果也赚修改麻烦,不妨用下面的黑科技兼容之:
let _oldupdateRichText = cc.RichText.prototype._updateRichText;
let _isSetNewString = false;
cc.RichText.prototype._updateRichText = function() {
// 这个变量是防止重复调用
if (_isSetNewString) return;
if (!this.enabled) return;
if (this.node) {
let color = this.node.color;
let strColor = '#' + color.toHEX("#rrggbb")
if (strColor != '#ffffff') {
let strText = this.string;
if (!strText.startsWith("<color")) {
_isSetNewString = true;
strText = cc.js.formatStr("<color=%s>%s</c>", strColor, strText);
this.string = strText;
_isSetNewString = false;
}
}
}
_oldupdateRichText.call(this);
}
总体思路就是:黑掉_updateRichText,如果结点的默认颜色不是白色,且RichText.string最外层没有指定标签,即强制在最外层给加上一个color。
上面方法亲测有效
不过还是希望引擎同学能把这个特性恢复回来。
这是翻了源码并理解了才修改的吧