Advertisement

pixels and units in Unity explanation?

Started by March 27, 2018 07:38 PM
6 comments, last by ethancodes 6 years, 8 months ago

Hello! I have read a bunch of articles, watched several videos, and I am still confused on pixels and Unity units, what their relationship is, the importance of that relationship, what it means for the rest of my project, and how to know how to manipulate that relationship. For example, I have a project I am working on that is basically an Arkanoid clone. I want the levels to be procedurally generated though. The brick sprites are 128x41 pixels, the background is 800x600 pixels. The brick sprites pixels per unit is set to 128. The background pixels per unit is set to 50. My gameboard is 16x12 units. The main part I am getting confused about is that my bricks are rectangular, not square. So when  I want to procedurally generate them, I can't just put 1 in each square on the grid, because there will be spaces between each line of bricks. But my bricks are like 3.1423 high. So I can't just put like 2 in one grid because it still wouldn't fit right. I think that I'm probably over complicating this, but I just can't seem to get a solid understanding of it. Do we just pick arbitrary numbers for the pixels per unit? How do I let the procedural generation program know exactly how to get the bricks side by side and right on top of each other? Thanks so much for any help. This topic is really kicking my butt.

 

Edit: maybe I should also note on here, I am making this game for mobile, and tablets to be played on a vertical screen. So I also get confused about how to make sure it works on multiple screen sizes.

The engine in general is designed to work independent of pixels. 

In the typical case developers want the same display regardless of window size.  It shouldn't matter if you have 1920×1080 or 1366×768 or 1280×720, they should all appear the same. In this situation developers typically do not want anything dealing with pixels.

Your edit says you are wanting something for mobile and also for varied screen sizes, which further reinforces that you don't want pixels.

 

If you're doing Unity's 2D mode there are a few things to do, but it is still a bit of work:

You need to make sure you've got an orthographic camera so everything is square. Your orthographic scale needs to be set based on your window size or your device's screen size.

You need to turn off mipmapping, turn off anisotropic filtering, turn off antialiasing. The sprites will need to use "point filtering" to get the nearest pixel color instead of interpolation. There are a few other settings in the sprite you need to set, but I forget them off-hand. I recall there is a preset option to mark them as UI sprites that sets them for you.

After that, you need to make sure you position items at exact pixel boundaries. You can't position items at 123.45 or 123.98, they need to be exactly at positions 123, 124, 125, and other integer values that match to pixels.

Scaling and rotating are going to fail terribly in that pixel-perfect world because they normally don't fall precisely on pixel boundaries. 

 

If you go with that approach you will also struggle with devices of different size.  Since each pixel makes a difference screens of different sizes will have different appearance. Different resolutions will have more pixels or less pixels in any particular screen. High-density screens will have tiny pixels and take a small portion of the screen compared to low-density screen that will struggle to display anything.

 

There are some packages in the asset store that will do much of that, setting up your camera correctly and ensuring 2D objects are snapped to integer values. 

 

If you don't need pixel perfect display you can render to a fixed size texture and then display that texture on the screen.

Advertisement

Hi. Don't fear, pixels are less important than you think. In truth you only ever need to worry about pixels when they produce less than desired results. :)

4 hours ago, ethancodes said:

I am still confused on pixels and Unity units, what their relationship is, the importance of that relationship

No surprize here because it does not exist. There is no defined relationship between pixels and Units. It's up to YOU to decide what relationship they will have.

Often it's easy to work with 1:1, 1:10, 1:100. Meaning that 1 Unity unity = a 100 pixels; this should be the default.

Unit_1To100.jpg.12e7ec0680496992379f5255897a6361.jpg

The above image shows my button at 150 by 50 sprites. However I want each Unit to be 50 pixels large. So how do I set that?

Unit_1To50.jpg.13bbe2b3aa4cce7a6329b7cbdafc89f1.jpg

Simple. I select the image, then in the inspector I select what Pixel per unit I want. OK. Notice how things got blurry? That is because my game isn't pixel perfect. For most art styles this doesn't matter but let's fix this.

 

The 800 by 600(4:3) ratio you are using has a problem. That is the 3 on the X axis is a odd number.

BlockOut.jpg.c4271f3f947cfbc27570abd20cb4f923.jpg

Here we can see it is out by exactly 1/3. Because the computer is limited it can't solve 1/3. To fix this we scale the camera's Orthographic option to a base of 3.  3,6,9,12 etc.

Result4By3.thumb.jpg.aeb1cd216cf0d370fb10b48a8ffaf55c.jpg

The above image shows how the problem is solved by setting the Orthographic to 3. Now My Units, camera and pixels all line up.

In short: if one of your axis is a problem then use a power of that axis to correct the size of the camera. This lines everything up.

Sorry, if I confused anyone. I submitted before I was ready.

I'm still so confused. Do I need to change the pixels in my sprite in photoshop? Or should my image be good and I can just adjust whatever needs to be changed in Unity? And I'm still confused on how to handle the height of the sprites. If they are 128 pixels wide, and the pixels per unit are set to 128, is that pixel perfect? If the sprites are a fraction of a unit high, what do I do to handle that? I'm sorry if this is confusing or if I'm not asking the right questions. This is a subject that seriously drives me nuts and I just want to finally get a good understanding of it so I know how to layout my gameboard correctly.

 

22 hours ago, ethancodes said:

I'm still so confused. Do I need to change the pixels in my sprite in photoshop? Or should my image be good and I can just adjust whatever needs to be changed in Unity?

Make the sprite like you want and then fix it in Unity. The values in Unity mean what you want them to mean.

To be clear, Units are abstract, they have no meaning whatsoever. You decide how much pixels a unit is.

 

22 hours ago, ethancodes said:

If they are 128 pixels wide, and the pixels per unit are set to 128, is that pixel perfect?

No. Pixel perfect has nothing to do with Unity units.

Pixel perfect means that it is either 1:1, 1:4, 1:16, 1:64 pixel ratio of rendering. To explain:

2D_Pixel.jpg.2bdf30ec0fa65f1271309b70be9c8555.jpg

Pixels are 2D objects. meaning that if we double the size of a pixel it becomes 4 pixels. If we double it again it's 16 pixels. This is known as 1X,2X,3X design.

So look at this image again:

Result4By3.thumb.jpg.aeb1cd216cf0d370fb10b48a8ffaf55c.jpg

See how the camera is 800*600? Yet I can only fit 400*300 pixels in that view. This is still pixel perfect because every 4 pixels of the 800*600 pixels would equal 1 of my pixels. This is a 1:4 ratio.

If I use a orthographic setting of 6, I would get a 1:1 ratio. Where every pixel on my image would = one pixel in the render result. Any orthographic setting higher than 6 will result in my pixels being too small, and some won't be rendered.

The reason I just used 3 is because it doesn't matter. The 4:3 setting in Unity is an abstract setting, it tells me that this art will look correct on any device using a 4:3 ratio. So even if the screen is 960* 720 it will still look pixel perfect.

 

You seem confused. The Unity units are a tool to allow you to measure and move things around, they will have no impact on your game except for the scale of math(even that you can adjust). It's up to you to decide what you want the units to be.

 

22 hours ago, ethancodes said:

If the sprites are a fraction of a unit high, what do I do to handle that?

You could solve the math. Say for example you use 1 Unity unit = 100 pixels. 64/100 = 0.64 so if your block is at 0 the next block should be at 0.64. This is a very awful way of working. Because it involves math that isn't needed.

Take this example:

DevideExample.jpg.e5eed623e6f2a4affe1dc87c25ce7e82.jpg

Here I have a 64* 64 block and a 128* 64 block. Now I need to add 0.64 to each time. For the next block I need to add 1.28 this is a very awful way of working.

So instead I set each of the sprites pixels per Unit to a value I can easily work with. Both my tiles are divisions of 64 so let's use that.

SetPixelUnit.thumb.jpg.26af98a5e4a6c3836f51f2023124fc33.jpg

You can now see that the blocks now fit the Unity units perfectly. Each Unity unit is 64 pixels. I could also have used 32 or even 1. That way each Unity unit is exactly 1 pixel long.

 

OK, but what if we want the level to be 10 of our blocks long? Well for that you take your ratio: 4:3 means each block is 3/4 = 0.75.

We take 0.75(Ratio) * 10(the blocks we want) = 7.5 Now we divide by 2 because the camera is at 0 meaning half should be positive and the other negative. 7.5/2 = 3.75.

SetToTen.jpg.362aade5ec0bfddf7c42ed11aa4595dd.jpg

 

In short: Set Unity units to match the size of your sprites.

 

Advertisement

Thank you so much for this explanation. I think I understand it much better now. I'm going to test it out tonight and see if I've got as good of a grasp on it as  I think I do. lol. The whole pixels and units, scale, etc has always been confusing for me for some reason. Thanks again!

This topic is closed to new replies.

Advertisement