Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing sprite set sprite frame with an atlas does not auto add the atlas. #17727

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@ -369,7 +369,7 @@
* sprite.trim = true;
* ```
*/
@visible(function (this: Sprite) {

Check warning on line 372 in cocos/2d/components/sprite.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
return this._type === SpriteType.SIMPLE;
})
@displayOrder(8)
Expand Down Expand Up @@ -487,6 +487,7 @@

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 @@
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
Loading