How to make the size of the sprite was equal to the specified value in pixels?
If just doing

Sprite.transform.localScale = new Vector3((70), ((70)), 0.001f); 

That sprite just swells gigantically and that is why - it has the external value localScale, it is initially = 1,1,1. But inside this value, the sprite itself already has some size, and this is definitely not a 1x1x1 pixel, but, say, 50x50x1 pixels. It turns out, with this operation, we get the size of 3500x3500, which of course is absolutely not necessary.

The question is how to get access to the internal size of the sprite, which is not 1x1x1 in the inspector, but in the explorer, for example, 320x160.

Or, if it doesn't make sense, how easy it is to scale the sprite, exactly as many pixels as I need. I use Toolkit2d in Unity, so that solutions will come from there.

    2 answers 2

    In general, it is impossible to change the size of the sprite, which already lies in the unit, as far as I know. In the sense that one was 300px and make 400px. That is, resizing in the game and the screen is not the result of increasing / decreasing it physically on the media.

    If you want to change the size of the sprite, starting from the initial value, which is listed in the inspector, you can try to follow the following way:

    The size of the sprite can be taken from the rect field of the Sprite class, which is in turn the sprite field of the SpriteRenderer class.

    Those. If the object with the SpriteRenderer component in the script is written as follows:

     Debug.Log(GetComponent<SpriteRenderer>().sprite.rect.size); 

    then we will see the true size of the sprite.

    Knowing this and knowing the amount by which we want to reduce / increase the sprite, we simply draw up the proportion by calculating the coefficient and apply it to localScale .

    For example, we want to reduce the sprite on the X axis by 100px:

     var originSize = sprite.sprite.rect.size; var originX = originSize.x; var destX = originX - 100; var ratio = destX / originX; var scale = transform.localScale; transform.localScale = new Vector3(scale.x * ratio, scale.y, scale.z); 

    All this can be carried out in a separate method, both for changing one coordinate and the whole sprite. This is at the discretion. The main principle above.

      The size of the sprite in pixels does not make sense, since the sprite is part of the game world. For example, if the camera "moves back", the absolute size of the sprite in pixels will change, because the pixel size will remain the same; but the size of the sprite relative to the game world will not change. That is, if you need, for example, to increase the sprite (Mario ate the mushroom and grew) - you need to change the relative value. You can also read on the subject matter, with an example.

      You can also change the size of the texture on which the sprite is based:

       someSprite.texture.Resize(640, 480); 

      But I'm not sure that this is what you need.