playcanvas / playcanvas/engine

Changing Element’ Sprite via script, add method for ResetSize

Open
#3,379 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: ui enhancement
Dominant language
JavaScript
Stars
16.8k
Forks
2k
Avg merge
4h 32m
Merged PRs (30d)
222

Description

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. I've to do this :

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 or ResetSize like in the editor 😉

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the Element component's spriteAsset handling and inspect how sprite dimensions are applied when the asset changes. Define the reset-size behavior around the requested Unity-like native sizing, then verify that changing sprites through script updates the element without distortion. The issue names no source file or test, so locating the relevant component and existing UI tests is the first step.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
game-dev
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.