Improvement proposal to the Portrait Native Size checkbox for get correct size when sprite is packed by Sprite Atlas.

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
ichijo
Posts: 2
Joined: Tue Aug 09, 2022 2:51 am

Improvement proposal to the Portrait Native Size checkbox for get correct size when sprite is packed by Sprite Atlas.

Post by ichijo »

Hi Tony,

From the Dialogue System 2.2.23, you've added a nicely option the Portrait Native Size checkbox to Standard UI Subtitle Panel.
That works very good, but the current source code has a little problem when the sprite is included Atlas.

Code: Select all

        protected virtual void SetPortraitImage(Sprite sprite)
        {
            if (portraitImage == null) return;
            Tools.SetGameObjectActive(portraitImage, sprite != null);
            portraitImage.sprite = sprite;
            if (usePortraitNativeSize && sprite != null)
            {
                portraitImage.rectTransform.sizeDelta = new Vector2(sprite.texture.width, sprite.texture.height);
            }
        }
In the SetPortraitImage function the resize procedue uses sprite.texture.width, but the texture size means souce texture of packed one, so if the sprite is included in 1024 x 1024 the portlait image set to 1024 x 1024 not the original sprite size.

So I proposal this way below: check the sprite is packed and use sprite.rect.width if it packed.

Code: Select all

        protected virtual void SetPortraitImage(Sprite sprite)
        {
            if (portraitImage == null) return;
            Tools.SetGameObjectActive(portraitImage, sprite != null);
            portraitImage.sprite = sprite;
            if (usePortraitNativeSize && sprite != null)
            {
                portraitImage.rectTransform.sizeDelta = sprite.packed ? 
                    new Vector2(sprite.rect.width, sprite.rect.height) : 
                    new Vector2(sprite.texture.width, sprite.texture.height);
            }
        }
I've added this modification for my usage,but if you feel its good for entire of the asset, please consider to pick this to next version.

Thanks,
Ichijo
User avatar
Tony Li
Posts: 21975
Joined: Thu Jul 18, 2013 1:27 pm

Re: Improvement proposal to the Portrait Native Size checkbox for get correct size when sprite is packed by Sprite Atlas

Post by Tony Li »

Thank you for the suggestion and for providing the code! I will definitely add this to version 2.2.31.
Post Reply