From 4a8040c368f596e959c447940fd5347e24ec05cd Mon Sep 17 00:00:00 2001 From: knoxHuang Date: Wed, 16 Oct 2024 15:52:01 +0800 Subject: [PATCH 1/2] Fixing sprite component dragging in a sprite frame with an atlas does not auto add the atlas. --- cocos/2d/components/sprite.ts | 64 ++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/cocos/2d/components/sprite.ts b/cocos/2d/components/sprite.ts index eb1b2621d67..8b8538b9a14 100644 --- a/cocos/2d/components/sprite.ts +++ b/cocos/2d/components/sprite.ts @@ -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'; @@ -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); } } @@ -638,26 +639,24 @@ export class Sprite extends UIRenderer { } private _resized (): void { - if (!EDITOR) { - return; - } - - if (this._spriteFrame) { - const actualSize = this.node._uiProps.uiTransformComp!.contentSize; - let expectedW = actualSize.width; - let expectedH = actualSize.height; - if (this._sizeMode === SizeMode.RAW) { - const size = this._spriteFrame.originalSize; - expectedW = size.width; - expectedH = size.height; - } else if (this._sizeMode === SizeMode.TRIMMED) { - const rect = this._spriteFrame.rect; - expectedW = rect.width; - expectedH = rect.height; - } + if (EDITOR) { + if (this._spriteFrame) { + const actualSize = this.node._uiProps.uiTransformComp!.contentSize; + let expectedW = actualSize.width; + let expectedH = actualSize.height; + if (this._sizeMode === SizeMode.RAW) { + const size = this._spriteFrame.originalSize; + expectedW = size.width; + expectedH = size.height; + } else if (this._sizeMode === SizeMode.TRIMMED) { + const rect = this._spriteFrame.rect; + expectedW = rect.width; + expectedH = rect.height; + } - if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this._sizeMode = SizeMode.CUSTOM; + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { + this._sizeMode = SizeMode.CUSTOM; + } } } } @@ -709,6 +708,31 @@ 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) { + if (spriteFrame) { + if (spriteFrame.atlasUuid.length > 0) { + 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; + } + }); + } + } else { + this.spriteAtlas = null; + } + } + } } } From ba35e391a4121998b7bc8099c95ea77cd5c391e0 Mon Sep 17 00:00:00 2001 From: knoxHuang Date: Thu, 17 Oct 2024 09:53:23 +0800 Subject: [PATCH 2/2] refine code --- cocos/2d/components/sprite.ts | 67 ++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/cocos/2d/components/sprite.ts b/cocos/2d/components/sprite.ts index 8b8538b9a14..d1c4155cbf6 100644 --- a/cocos/2d/components/sprite.ts +++ b/cocos/2d/components/sprite.ts @@ -639,24 +639,26 @@ export class Sprite extends UIRenderer { } private _resized (): void { - if (EDITOR) { - if (this._spriteFrame) { - const actualSize = this.node._uiProps.uiTransformComp!.contentSize; - let expectedW = actualSize.width; - let expectedH = actualSize.height; - if (this._sizeMode === SizeMode.RAW) { - const size = this._spriteFrame.originalSize; - expectedW = size.width; - expectedH = size.height; - } else if (this._sizeMode === SizeMode.TRIMMED) { - const rect = this._spriteFrame.rect; - expectedW = rect.width; - expectedH = rect.height; - } + if (!EDITOR) { + return; + } - if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this._sizeMode = SizeMode.CUSTOM; - } + if (this._spriteFrame) { + const actualSize = this.node._uiProps.uiTransformComp!.contentSize; + let expectedW = actualSize.width; + let expectedH = actualSize.height; + if (this._sizeMode === SizeMode.RAW) { + const size = this._spriteFrame.originalSize; + expectedW = size.width; + expectedH = size.height; + } else if (this._sizeMode === SizeMode.TRIMMED) { + const rect = this._spriteFrame.rect; + expectedW = rect.width; + expectedH = rect.height; + } + + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { + this._sizeMode = SizeMode.CUSTOM; } } } @@ -715,23 +717,24 @@ export class Sprite extends UIRenderer { } private _applyAtlas (spriteFrame: SpriteFrame | null): void { - if (EDITOR) { - if (spriteFrame) { - if (spriteFrame.atlasUuid.length > 0) { - 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; - } - }); - } - } else { + 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; } - } + }); } } }