Cocos Creator 图片/文字的渐变色实现!

支持 Sprite 和 Label!参数可调!摆地摊的时候学习一波~

效果预览:

a8f232a6a928b5def43212a8400c5f73.gif

如何使用?

cc.Spritecc.Label 添加脚本 ColorAssembler2D

调整颜色 colors 参数即可。

每个点的对应位置如下:

46930d5d0806150ab9bffe114a2cbfe7.jpeg

如何实现的呢?

对于 cc.RenderComponent 都有一个 _assembler

fc55ac4105f7d8176f8a47986490e0b4.jpeg

只要这个 _assembler 是继承 cc.Assembler2D , 就有一个 updateColor 的方法。

eb176211ec67a50228aced1ccd90e7e4.jpeg

只要依葫芦画瓢,修改一下顶点的颜色值就行了,参考代码如下。

// private _updateColors() {
const cmp = this.getComponent(cc.RenderComponent);
if (!cmp) return;
const _assembler = cmp['_assembler'];
if (!(_assembler instanceof cc['Assembler2D'])) return;
const uintVerts = _assembler._renderData.uintVDatas[0];
if (!uintVerts) return;
const color = this.node.color;
const floatsPerVert = _assembler.floatsPerVert;
const colorOffset = _assembler.colorOffset;
let count = 0;
for (let i = colorOffset, l = uintVerts.length; i < l; i += floatsPerVert) {
    uintVerts[i] = (this.colors[count++] || color)['_val'];
}

当然这个方法要在引擎渲染之后再修改才有效。

onEnable() {
    cc.director.once(cc.Director.EVENT_AFTER_DRAW, this._updateColors, this);
}

如果移除了这个组件,还要告诉引擎重新渲染这个颜色。

onDisable() {
    cc.director.off(cc.Director.EVENT_AFTER_DRAW, this._updateColors, this);
    this.node['_renderFlag'] |= cc['RenderFlow'].FLAG_COLOR;
}

以上就是白玉无冰使用 Cocos Creator v2.3.3 关于 "图片/文字 的渐变色实现" 的技术分享,再次感谢白玉无冰~


如果您在使用 Cocos Creator 2D/3D 的过程中

 get 了独到的开发心得、见解或是方法

欢迎随时向我们投稿

帮助更多开发者们解决技术问题

让游戏开发更简单~

期待您与我们联系~

“在看”是最大的鼓励▼

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值