How to clone spriteFrame?


I need to create many modified sprites from one texture using part of this texture.
To do this, I clone the spriteFrame:

let spr:SpriteFrame = new SpriteFrame();
spr = this.sprite.spriteFrame.clone();

Everything works, but the .clone() method - “since v3.5.0, this is an engine private interface that will be removed in the future.”

let spr:SpriteFrame = new SpriteFrame();
spr = instantiate(this.sprite.spriteFrame);

does not work.

Can you please tell me if there is another way that will not be canceled in the future?

Here are all details you should need:

// this.spriteFrame is your sprite you want to clone
this.spriteComponent.sprite = this.spriteFrame;

// Create a new node
const newNode = new Node('New Node');
// Add a sprite component
// Get the newly created sprite 
const newSpriteComponent = newNode.getComponent(Sprite); component
// Here you use the same spriteframe
newSpriteComponent.sprite = this.spriteFrame;
// Add the new node as a child to this.node

Thank you Ronsku!

The problem is that when I use:

newSpriteComponent.spriteFrame.rect = new Rect(0,0,80,80);

the texture is clipped on both the original and the cloned sprite.

Here is a working code that cuts a 480x480 texture into 36 80x80 squares and puts them into one picture:

import { _decorator, Component, Node, SpriteFrame, Sprite, Rect, rect} from 'cc';
const { ccclass, property } = _decorator;

export class Cut_out extends Component {

    sprite: Sprite = null;
    protected spriteN2: Sprite[] = [];

    start() {
        let spr:SpriteFrame = new SpriteFrame();

        for (let i = 0; i < 6; i++) {
            for (let j = 0; j < 6; j++) {

            spr = this.sprite.spriteFrame.clone(); 
            spr.rect = new Rect(j*80,i*80,80,80);
            this.spriteN2[i*j] = new Node("New Sprite").addComponent(Sprite);

            this.spriteN2[i*j].spriteFrame = spr;
            this.spriteN2[i*j].node.setPosition(j*80-440, (i*80-280)*-1, 0);

The bottom three sprites swept away for clarity.

My main question is - what can replace the “clone ()” method in future versions of the engine?