Clone Materials

How can i clone the materials (current using version 2.4.4 of Cocos creator) ?

右键materials在资源浏览器中显示->复制->粘贴

1赞

Thanks for your suggestion.
But my issue is : i want to clone material by script like we use cc.instantiate.
Do you have any other suggestion?

JSON.parse(JSON.strifity(cc.Material))

This error log when i use this function (Do you have any idea?) :
let material = this.sprite.getMaterial(0);
-> let newMaterial : cc.Material = JSON.parse(JSON.stringify(material));
Uncaught TypeError: Converting circular structure to JSON
–> starting at object with constructor 'cc_EffectAsset’
| property ‘_effect’ -> object with constructor 'Effect’
property ‘_asset’ closes the circle.

/**

 * !#en Get the material by index.

 * !#zh 根据指定索引获取材质

 * @method getMaterial

 * @param {Number} index 

 * @return {MaterialVariant}

 */

getMaterial return MaterialVariant
https://docs.cocos.com/creator/manual/en/render/material.html#set-material-in-script

the Renderer component will recreates a Material Variant based on the incoming Material during the setup process.

let newMaterial = cc.MaterialVariant.create(this.sprite.materials[0])
also can clone

1赞

I cloned the Material as you said with 2 object.
But maybe the MaterialVariant just mapping the old Material.
And when i change the properties of the Materials, all of it will changing effect at the same time.
Maybe great solution to create new Materials is just copy and paste it in the editor.