So I have texture of 128*128 pixel, what camera size should I use to make the texture fit perfectly with the camera’s hight?
I understand that camera size is half of the camera height in world unit, but what about this size to pixel conversion?
So I have texture of 128*128 pixel, what camera size should I use to make the texture fit perfectly with the camera’s hight?
I understand that camera size is half of the camera height in world unit, but what about this size to pixel conversion?
To make pixel perfect art, set your camera ortho size to SCREEN_HEIGHT / 2 / PIXELS_TO_UNITS.
To fit this texture by scaling it, set its scale.y to SCREEN_HEIGHT / PIXELS_TO_UNITS assuming ortho size mentioned above.
I actually just got done finally figuring this out as we speak.
I’m creating textures at 128 * 128.
Then on their import settings I’m having them import at 128 pixels to units. (This way I can snap them to the unity grid)
Then in my camera script I have:
Took a lot of messing around but it finally looks super sharp.
I also have a material applied to them with “pixel snapping” checked. I don’t know if it’s doing anything, but it helped some others on the forum.
Hi Tomas!
Can you help me with these numbers, please? I have done everything like you said above. That is:
My current screen is temporarily 1024 * 768, but later I want to make the game universal - e.g. make it perfect for every iOS screen size (iPhones and iPads). So, Screen.height is 768.
I set camera ortho size to Screen.height / 2 / pixelsToUnits (which is 100 for every sprite in my game). That is 3.84.
I set my obects’ scale to Screen.height / pixelsToUnits, but not only localScale.y, also localScale.x, otherwise objects scale is not perfect. That is 7.68 for every object.
My camera scale is (1, 1, 1). As a result, all the objects are huge, they are even bigger than the camera is, I mean they don’t even feat the screen
Did I do anything wrong?
Thanks!
Guys, any advice? How to setup every game object’s size correctly? Now it looks really terrible - main camera is too small, game objects are too large.
You objects scale has to be 1, not 3.84
I don’t agree with you.
First, I have tried this. If I set local scale to (1, 1, 1), my objects are still too big.
Second, They should change their sizes depending on the screen size. For example, they must have different sizes for iPad with 1024 * 768 and iPad with 2048 * 1536 sizes.
I don’t understand the reason. It worked really well on Unity 4.2. But now I’m converting my game to new 2d tool in Unity 4.3 and I have only problems, nothing else. It’s almost impossible to set up the game correctly, but it was really easy in the previous version.
Your object’s scale should be 1, if your art is 100%.
Let’s say you have an image that is 100x100
Your screen is 1024x768 and your camera size 3.84 and PixelsToUnits is 100;
When you place it on the screen at scale 1, it will be the correct size (100x100px).
The ideal situation is that you have a set of images for each resolution (for pixel perfect). If you don’t, using the above example, switching to 2048x1536.
You will need to set your camera properly to 7.68.
If you are using the same images the will have to have a scale of (new cam size)/(old cam size) which is 2.
So 100x100 becomes 200x200. Which works perfectly since the screen is doubled.
It’s really not difficult, but it is different (and more accurate). Build and plan your assets so they are 100% of your display.
Also ensure that when you are setting the camera’s size, you are setting in the size field in inspector. Don’t monkey with the camera scale. it doesn’t affect anything.
I have had no problems with it so far, it is just a few extra steps though. Writing classes to handles scale changes and resolutions helps a lot.
Thanks for the explanation, I could understand a lot, but not everything.
Finally I have come to the task to make the 2d game flexible for all the screen sizes. Here I found that it’s impossible to change PixelsToUnits at runtime, they advise to change the transfrom.scale. But you say it should be (1, 1, 1).
I change my camera size at runtime, as well, like Camera.main.orthographicSize = Screen.height / 2 / SettingsManager.Instance.PixelsToUnits;
Hot to change the game objects size without changing their scale? I have tried my game with different screen sizes and without changing game objects localScale - they are not scaled automatically, so I should do something with them. But what?
Thanks a lot for the advice!
I didn’t mean to imply it should (1,1,1) all the time, but that is what you want it set at to display pixel/100% art. It’s normal to change the scale of something during gameplay.
First off, you shouldn’t need to change anything. At least in terms of game functioning/measurements. If you build you game to a iPod or a iPad3, generally, it should function just the same. (with the exception of graphic quality, and the extra space on the side for wide screen). You don’t need to do anything it will just work, because it is placing things in space by units, and not pixels.
And don’t change your camera size and don’t change the PixelsToUnits (in the game) at runtime for dealing with different resolutions. That is what the the pixelstounits attibute in textures is for. You just set those up once, and use that as a basis for creating your game. You can have different values on different assets that are use for different resoultions, but there is no practical to change them on existing assets at runtime for the purpose of resolution.
People have different ways of approaching this depending on many factors. But first off the PixelsToUnits isn’t a variable factor, it shouldn’t be changed runtime, that would defeat the purpose. And, except for narrative or other reasons, you don’t need to change the camera size. Especially for the purposes of device resolution. Whatever device you use won’t change its resolution.
The purpose/use of pixelstounits and scale settings and calculations is a way for you to define sizes in your game agnostically. For instance, in flash you can change the width and height of a sprite. That is measured in pixels (well, twixels actually), but there is no camera. And because of varying resolution, it become complex and no longer accurate. With Unity you simply set your own conversions, and then only need deal with units and scale. It is a much more flexible approach.
So, for what you want to do there are a few approaches, here are some:
Double all your art sizes.
This is the drop dead easiest. It is also potentially the worst way to go. (unless you have minimal graphics). You can simply double all the art and then reduce it to 50% in the scene. It will then look good in retina and standard. It will also increase the size of your game and more importantly memory usage. Not a great thing to do to users. But this approach requires almost no effort. And it is also a reasonable solution for games with minimal unique art content. Like a puzzler or match 3 style game. If you have a ton of characters and backgrounds, its not a great way to go.
Swap art at runtime.
You can programmatically swap art (high or low depending on device). This is a better solution, as it won’t impact memory, but it will increase size as you have to include both. But that also means you will have to have the code to handle it. Anywhere an image is loaded, or a prefab instantiated or whatever, you will need to hvae a way to swap the art. If build this in from the beginning, its not so bad, but may be a pain to try to add this to an existing game. I am not a fan of this, because you have a lot of code and overhead that has nothing to do with the actual game play.
Separate builds.
You can, like many games do, have a standard version and HD version. No art swapping at run time, and no extra art included in the build that isn’t used. Kind of tedious, but a clean option.
Load art assets separately
This is the method I prefer. Similar to the separate builds, but you are only maintaining separate packages and not builds. This solution is really the most optimal. There is a little work up front in setting up your loading/pipeline, but during run time and gameplay, the game code won’t have to know or care about image/device size. When you set it up, the packages will exactly the same, same file names and structure. Then only difference is that the HD version will images twice the size, and you can also set the PixelsToUnits in the HD package to 200 (or whatever), for pixel perfect display.
I does require the user connect to internet the first time they play, since it have to download the bundles initially. But they can stay in the game after that.
So how you do it will depend a lot on what stage you are in now. If the game is built already are far along I would avoid swapping art at runtime as that can be a lot of fiddly work if your code isn’t highly structured from the get go. I would either go with separate builds or remotely loading assets.
Hope that helps!
Cheers,
ZG.
zombiegorilla, thanks a lot for this wide reply!
Frankly, I don’t like any of the suggested ways, because I agree it’s bad to double the art, I also don’t want to make a few builds (not convenient) etc.
I would prefer to make one flexible build. So my way it so make a high res art.
Then why is it bad to change camera size? It is done for different devices (with different screens) when a scene is being loaded. So you calculate the size and the only thing left to do is to make your art look nice with this screen (camera) size - that is increase or decrease its size. I would prefer to go this way, but I’m a bit worried if it’s really good (or terribly bad) ![]()
I’m rather new to the game development and I would like to make my good game creation style right away. And this scaling is a part of this style. I think this scaling is at least easy and not so terrible for performance. But I’m not sure.