Changing Element' Sprite via script, how to reset size?

Hello, I’m creating simple Radio Button / Group. When one is selected, it changes its sprite. My two sprites don’t have the same dimension since there are some outline effects.

I change them this way:

RadioButton.attributes.add('spriteOn', { type: 'asset', assetType: 'sprite' });

this.entity.element.spriteAsset = this.spriteOn;

However since it’s not the same size, it’s distorted. How can I call a Reset Size like in the editor? I didn’t find a method for that.

The Editor button for reset size is purely for the editor.

From the sprite resource, you can get access to the texture atlas and all the frame data.

The sprite resource also has the data about what frame(s) it uses from the texture atlas.

From there, you can get the information about width and height:

1 Like

Thanks @yaustar, got it via:

RadioButton.prototype.forceResize = function()
    const rect = this.entity.element.sprite.atlas.frames[this.entity.element.sprite.frameKeys[0]].rect;
    this.entity.element.width = rect.z;
    this.entity.element.height = rect.w;

Which is a bit tricky… Some method like SetNativeSize from Unity would be really useful Unity - Scripting API: UI.Image.SetNativeSize.

1 Like