- Home /
Set width and height of 2D gameobject in PIXELS
So I'm new to Unity and having programmed games before without a game engine I'm used to just being able to specify the width and height of any sprite I use in PIXELS. The problem in unity is that the scale property in transform doesn't have anything to do with how many pixels it is. I just want a simple way to be able to do gameObject.setWidth(500px) and gameObject.setHeight(400px). I've searched online and I can't find anything just a bunch of complicated answers that don't make sense.
As you started Unity, you will have to unlearn the pixel thing. 1 unit in Unity is 100px but i would suggest you to get a hang of new unity ter$$anonymous$$ology.
I wanna set the width and height to be a certain percentage of the screen width or height so that my sprites are scaled appropriately for different resolutions. I feel that this is the best way to achieve that.
If you are using Canvas UI then you have anchors and pivots. If you are using sprites then you can get the Screen.width
and Screen.height
and can set according to that.
You can set the pixels per unit property on a sprite when you import it :
Example: If you want to move a 1000px sprite on x by 100px with the default value of 100 pixels / unity unit you will need to move 1 unity unit -> transform.position.x = 1;
Also you can get device resolution with Screen.width and Screen.height and then change to the appropriate sprite size on start. Also check this answer for generating sprites with different pixels/unit : link text
Update : Sprite and texture generation based on device resolution sample attach it to an empty gameobject maybe this is what you need:
using UnityEngine;
public class Test : $$anonymous$$onoBehaviour {
private Sprite sprite;
private Texture2D texture;
private SpriteRenderer rend;
float prevwidth;
float prevheight;
private void Start()
{
rend = gameObject.AddComponent<SpriteRenderer>();
}
void Update () {
if(prevwidth != Screen.width || prevheight != Screen.height)
{
GenTandS();
prevwidth = Screen.width;
prevheight = Screen.height;
Debug.Log("Screen Width : " + prevwidth + " Height : " + prevheight);
Debug.Log("Sprite size" + sprite.rect);
}
}
void GenTandS()
{
texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
//helping grid not required
//Here you can use texture.Resize(width, height);
int cellSize = 100;
for (int i = 0; i < texture.width; i++)
{
for (int j = 0; j < texture.height; j++)
{
if ((i + 1) % cellSize == 0 ||
(j + 1) % cellSize == 0 ||
i == 0 || j == 0)
{
texture.SetPixel(i, j, Color.white);
}
else
{
texture.SetPixel(i, j, Color.black);
}
}
}
texture.Apply();
sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f), 100);
rend.sprite = sprite;
}
}
No I'm not asking about moving or positioning I'm asking about SCALING. The pixels per unit are not gonna help with scale. Also, you said "Also you can get device resolution with Screen.width and Screen.height and then change to the appropriate sprite size on start." $$anonymous$$y question is HOW would I actually SET the sprite size on start? I wanna do this programmatically not in the editor.
Check the script I attached to the answer. You can also use Texture2D.Resize(width, height); function then Texture2D.Apply(); to resize your actual texture. After any pixel modifications you need to apply but please note that this is costly on performance so I would not recommend using it a lot. Is this what you meant by scaling?
Thanks for all the info! I would like to point out that your approach may work for a single sprite attached to a gameobject but what about a game object with many children? For example I have a character gameobject composed of legs, arms, head, etc child gameobjects. Now in the editor I can select this whole character and using the rect tool scale the whole thing up or down. However, again the problem comes with the fact that scaling uses these arbitrary numbers and not actual pixel values. So 1.5 in the x and y would scale it 1.5x it's original size but I don't even know it's original size since it's a gameobject composed of many children sprites arranged in different ways. I simply want to know the number of pixels that the rect encapsulates on a gameobject.
So to get this straight before I answer what you want to achieve is a pixel perfect 2D game?
I think you can assume pixel values when designing UI elements, not using Sprite for GameObjects. Simply because it depends on your camera settings.
https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html
The manual here says that orthographicSize
is a half of vertical view. I assume (not able to check it now) that when, for example, setting the property to 5, you get 10 units on a vertical axis and 17.77 (16:9 ratio) on horizontal. On sprite settings default 100 Pixels Per Unit (marked by @xphorsepower ) says that every 100 pixels of the image will take 1 unit.
$$anonymous$$nowing that and having for example Image with 500x500 size, putting that into object with scale 1, 1, 1 I will guess that it will take half of the screen on vertical axis.
I am not able to check it all above is true, but my thought is that using Sprite you cannot easily set width and height in pixels (but you can write your own implementation) because of orthographic size of the camera.
@Sonky108 you are right about camera settings the`Camera orthographic Size` is required as we are talking about 2D but it sets your overall scale ratio / camera distance from the sprite itself. Example if you want to display a 100px sprite on the whole screen height orthographic Size must be set to 0.5 ergo if it`s a 1000px sprite set it to 5 assu$$anonymous$$g pixels / unit is set to 100. But this is what happens if you want to fill the screen with different sprite sizes : 100x100 resolution / Sprite 100x100px / Camera orthographic Size 0.5 1000x1000 resolution / Sprite 100x100px / Camera orthographic Size 0.5 100x100 resolution / Sprite 1000x1000px / Camera orthographic Size 5 1000x1000 resolution / Sprite 1000x1000px / Camera orthographic Size 5 16:9 ratio / 711px w / 400px h 16:9 ratio / 1111px w / 625px h The problem is actual size of the sprite/texture used if you don`t use the proper ration your performance will be trash. Ideally you need to have big textures to start with then resize them down in order to display them properly at the smallest size you can
I have problems launching the images. Could you share them?