Skip to content

Commit

Permalink
Fixing sprite set sprite frame with an atlas does not auto add the at…
Browse files Browse the repository at this point in the history
…las. (#17727)

* Fixing sprite component dragging in a sprite frame with an atlas does not auto add the atlas.
  • Loading branch information
knoxHuang authored Oct 17, 2024
1 parent 8d0d5ef commit 8dfa2b6
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ccclass, help, executionOrder, menu, tooltip, displayOrder, type, range
import { BUILD, EDITOR } from 'internal:constants';
import { SpriteAtlas } from '../assets/sprite-atlas';
import { SpriteFrame, SpriteFrameEvent } from '../assets/sprite-frame';
import { Vec2, cclegacy, ccenum, clamp, warnID } from '../../core';
import { Vec2, cclegacy, ccenum, clamp, warnID, error } from '../../core';
import { IBatcher } from '../renderer/i-batcher';
import { UIRenderer, InstanceMaterialType } from '../framework/ui-renderer';
import { PixelFormat } from '../../asset/assets/asset-enum';
Expand Down Expand Up @@ -487,6 +487,7 @@ export class Sprite extends UIRenderer {

if (EDITOR) {
this._resized();
this._applyAtlas(this._spriteFrame);
this.node.on(NodeEventType.SIZE_CHANGED, this._resized, this);
}
}
Expand Down Expand Up @@ -709,6 +710,32 @@ export class Sprite extends UIRenderer {
spriteFrame.on(SpriteFrameEvent.UV_UPDATED, this._updateUVs, this);
}
}

if (EDITOR) {
this._applyAtlas(spriteFrame);
}
}

private _applyAtlas (spriteFrame: SpriteFrame | null): void {
if (!EDITOR) return;

if (!spriteFrame) return;

if (spriteFrame.atlasUuid.length === 0) {
this.spriteAtlas = null;
return;
}

if (!this.spriteAtlas || this.spriteAtlas.uuid !== spriteFrame.atlasUuid) {
cclegacy.assetManager.loadAny(spriteFrame.atlasUuid, (err: Error, asset: SpriteAtlas) => {
if (err) {
this.spriteAtlas = null;
error(err);
} else {
this.spriteAtlas = asset;
}
});
}
}
}

Expand Down

0 comments on commit 8dfa2b6

Please sign in to comment.