How to lock Unity WebGL screen ratio on all mobile screen sizes - webgl

What I'm trying to achieve is as simple as setting the game aspect ratio to be the exact same even on different mobile sizes. (9:16)
For example:
this is how it looks like on a phone with a screen size of 1080 x 1920 (this is the size i built the game on)
looks all nice right? but then, this is what happens on a screen of size 1284 x 2778
So how should I go about not letting the sides on the 1284 x 2778 screen got cut off and instead just scaling down the game while preserving the 9 by 16 ratio so that nothing got cut off and have a black bar on top and bottom to cover for the missing pixels?
Something like this is what I'm trying to achieve:
(I got that temporarily by literally scaling down every single gameobject which is very inefficient)
What I've tried:
using the free webGL template on the assets store to no avail
manually writing css codes in the index.html file to also no avail
note: I uses 1284 x 2778 only for reference but it would be even better if the solution could work with the many screen sizes of mobile phones out there. Cheers!
Cheers!

Related

Proper way of designing layout dimensions for all the iPhones in Xcode

I spent hours of searching about this topic, but nothing is exactly dealing with my problem. Maybe you guys can help me better.
I want to ask what is the proper way of designing a layout to properly support all the iPhones. Also the easiest way for me as possible.
My style is that I am designing for the smallest device first and then scale things up for the other devices. Is that alright or is this not how it is supposed to be done?
My problem is that I am creating a layout that should scale up according to display size.
I will give an example of Original iPhone which has screen of 320 x 480 points (which equals 320 x 480 pixels) and iPhone 6, which has screen of 375 x 667 points (which is 750 x 1334 pixels because of the different pixel density).
Now I want to make a button or textbox or rectangle (whatever) on the Original iPhone that will get bigger if I launch it on bigger device like iPhone 6.
So if I have a button having height of 30 (pt) on Original iPhone, it should be bigger on iPhone 6. But how much bigger?
Do I have to calculate those 30 pt into pt on bigger device myself every time, or is there better method that works automatically so I don't have to re-count all the dimensions for every device? What is the correct way?
Btw: I am aware of that when designing the icons, buttons, or anything that is a .png file, I have to create them in dimensions of 1x, 2x and 3x so it can scale on all the devices, but how to work with these while constructing the actual layout?
What is the logic or how is it ment to be done?
THIS PICTURE is showing the dimension proportions I found on the web and little bit of idea of my problem below.
EDIT: Please look at this website: https://designcode.io/iosdesign-guidelines
It seems like there are some Apple's measurement standarts in (pt).
For example on one of the pictures they are showing that the margin from the sides should be 8pt. Since [pt] is an universal unit, I would expect it to adapt on other devices. So if I set the margin of 8 in editor, will it make different ammount of pixels on the other devices so it looks visually the same?
If you calculate every view like this one , it will not work.
Just set constrains on that view which you want same in other devices.
Like if you fix a view's height is 30 then it will 30 in every device. It is not going to be bigger or smaller in other devices.
But if you think that, a view is 20 pixel from right and 20 pixel from left, 10 pixel from top bar and bottom layout then this view gonna same ratio in every device.
I hope you understand what i want too say.
Please comment if i am unable to make you understand. :)

iPhone different screen sizes in flash? (Getting Black Bars)

I'm new to the whole world of coding, and actionscript 3 is my first real experience, so sorry if I don't understand your answer straight away.
I've built an iPhone app using Adobe Flash CC in AIR for iOS. All the code is either in the timeline or separate .as files (so not using documents classes).
The core concept of the game is randomly generated objects fall from the top of the screen and the user has to tap them to make them disappear before they touch the bottom.
My problem is my document size is 640 x 960. I think this fits the iPhone 4 (haven't tested that) but when I test it on my iPhone 5s I get back bars at the top and bottom. Obviously they have different screen sizes but I want the app to be able to run on many all the different size iPhones.
I have spent hours googling this and still don't understand what I'm meant to do. I've tried playing around with the stage.scaleMode settings but nothing changes. I have also added a file called default-568h#2x.png (just a green rectangle with the dimensions 640 x 1136) in the included files but this doesn't show either.
So essentially I want to know how to change my app and AS3 code to allow my app to fit all the different size iPhones?
Any help would be very much appreciated.
LAUNCH IMAGES
First, before anything else, you need to make sure you have the correct launch images included in your project.
Here is a table from Adobe's website:
Default~iphone.png | iPhone 4 (non-retina) 640 x 960 Potrait
Default#2x~iphone.png | iPhone 4, 4s 640 x 960 Potrait
Default-568h#2x~iphone.png | iPhone 5, 5c, 5s 640 x 1136 Potrait
Default-375w-667h#2x~iphone.png | iPhone 6/7/8 750 x 1334 Potrait
Default-414w-736h#3x~iphone.png | iPhone 6+/7+/8+ 1242 x 2208 Potrait
Default-Landscape-414w-736h#3x~iphone.png | iPhone 6+/7+/8+ 2208 x 1242 Landscape
Default-Landscape-812h#3x~iphone.png | iPhone X 2436 x 1125 Landscape
Default-812h#3x~iphone.png | iPhone X 1125 x 2436 Portrait
Once you have those images made (and named exactly as shown), include them in your project (They have to be in the root of your application) by doing the following:
In FlashPro
go to your publish settings
go to the AIR for iOS Settings.
Go to the "General" tab
add all those images to the "included files" list (the root)
SCALING YOUR CONTENT
OPTION 1, FILL AND CROP
If you don't mind cropping your content a little bit, you can just do this when your app starts:
stage.scaleMode = StageScaleMode.NO_BORDER
This will scale your swf so it fills the whole screen while keeping aspect ratio. It's pretty easy to figure out how much padding you need to make this method work well for the small variations in aspect ratios for the various iPhones.
However, if you want to allow orientation changes (portrait to landscape), the cropping will likely be too severe.
OPTION 2 - RESPONSIVE DESIGN
The best way to accommodate varying screen resolutions and aspect ratios though, is to make your application responsive. This involves the following code at the start of your application:
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
Now your stage bounds (stage.stageWidth & stage.stageHeight) will be the full width and height of the device*. (some devices still have a software toolbar at the bottom or the top band)
You can then position things through code.
If you want an easy way convert what you have (you don't want to use code to resize and align absolutely everything), just put all your content in a container MovieClip with an instance name of container, you could then size and position it like this:
//scale the container as big as possible while still fitting entirely in the screen:
//figure out which dimension should match the stage (widht or height)
if(container.width - stage.stageWidth >= container.height - stage.stageHeight){
container.width = stage.stageWidth;
container.scaleY = container.scaleX;
}else {
container.height = stage.stageHeight
container.scaleX = container.scaleY;
}
//center it on the screen:
container.x = (stage.stageWidth - container.width) * 0.5;
container.y = (stage.stageHeight - container.height) * 0.5;
It's also a good idea to listen for resize events, in case the screen size changes (eg you maximize/restore on desktop, or go from portrait to landscape on mobile).
You do that by listening for the resize event on the stage:
stage.addEventListener(Event.RESIZE, redrawScreen);
function redrawScreen(e:Event):void {
//resize everything as the window size has changed.
}
You the coder are in charge of providing different solutions for different screen sizes. You check the device size and then you present the content accordingly. All in all it is not that different from showing different content based on rotation. If you hope for a magical solution that would do all that for you in AIR you are out of luck cos there's none.
Messing with the stage scalemodes is not recommended (you should always use no scale on mobile) as you then give up completely the ability to compare the position of your displayobject according the the real physical device size (basically you won't know for sure if whatever you display is in the screen or completely out of it).
If you thought developing for mobile was easy (not just using AIR but using any technology) then sorry, it's not especially cos you have to handle all those sizes.
The basic principle on how to deal with it:
get the real device size.
calculate the real density/ratio.
Compare that size to the size of your app. (again scale mode to no scale)
Extract a general ratio (size of your app compared to size of device)
Use that ratio to either, scale and place your main container (a container that contain your entire app), hard: scale and place all your DisplayObject in your app.
Since the app ratio is maintained fill the blank space with whatever.
Your app is filling correctly the entire screen on any device.

Confusion about #2x #3x Xcode images

The apple guides haven't helped me too much with this but I am starting to get serious about IOS development and was taken aback when I saw images go from #1x to #3x.
One source I found said they were based on sizes. For example:
If you are working on an inferred VC size and your image is 30x30 and you run that on a iPhone6 then your image should be 30x30 for #3x, 20x20 for #2x, and 10x10 for #1x.
Another source I said mentioned that it was based off the images resolution and not the size of the image.
Could someone clear this up for me and explain how to make sure my images look professional for all screens?
Thanks in advance!
When the first iPhone (and SDK) came out, all images were 1x (1 pixel of the image = 1 pixel of the screen), because that's all we had to care about. On that phone there were 166 horizontal or vertical pixels per inch (dpi). So to display an image that stretched from the left edge to the right edge, our image would need to be 320 wide (because that's what Apple told us - we didn't have to count pixels) by whatever height we wanted. Let's say 100 pixels tall. We'd name that image MyPhoto.png (because Apple told us the iPhone OS worked best with PNG images) and we'd display it.
When the iPhone 4 came out with the retina display, its retina display had 2 times the horizontal and 2 times the vertical resolution of the older phones. So now we needed higher resolution images to get the best looking interface. We were still displaying just a 2" wide image, but it would need twice the number of horizontal pixels and twice the number of vertical pixels (4 times the total number of pixels) to fill the same space. So now our image needs to be 640 pixels by 200 pixels. Since our app would still need to support the older phones as well as the new phones, we didn't want to just replace the old image with the new one. We needed a way to support both. So the old images kept the same name they had before (we didn't append "#1x") and the new images got the "#2x" filename extension to indicate they were twice the resolution. So we'd name this larger image MyPhoto#2x.png.
A common question is why not use just the higher resolution images on both old and new phones? With scaling, both appear to work great. The problem primarily is that the iPhone, especially the older phones, had very limited memory to work with. Using images that are 4 times larger than they need to be is really hard on that limited memory. That's why we had to have both 1x and 2x images in our apps.
For our sanity, we would still just use 1x while designing the interfaces. So screens remained 320 pixels wide in our code, even if we were going to display them on a retina display that was 640 pixels wide. If we wanted to draw a 1 pixel wide line on the retinal display, we'd make it half a pixel wide in our code.
With the larger screen of the iPhone 6 Plus, Apple had to pack even more pixels on the screen to maintain the high resolution customers came to expect. So now we're into 3x images. However, the iPhone 6 Plus doesn't really have three times the number of horizontal or vertical pixels. But again Apple has kept us sane by just asking us to supply "#3x" images, and it worries about scaling them down a bit on the device. As before, we still need to support smaller screen resolutions too, so we still include "#2x" and the 1x images in our apps.
So, to continue our example, the #3x image would need to be three times the width and height of the 1x image. So now we need a 960 wide by 300 pixel tall image. We'd name it MyPhoto#3x.png.
Notice that although I mentioned earlier something about a 166 dpi display, that's not really important when creating these images. In your photo editing app you don't really care if the image is 166 or 72 dpi or anything else. The only important part is the number of pixels in width and height of the final image we export.
So to answer your question (hopefully), you'll want to include all three of these images in your iPhone compatible apps. You can skip the #3x image for iPad-only apps, because there's no 3x screen yet for the iPad. But continue to think only in the 1x size when working in Interface Builder and code.
For example, now to display a 1 pixel wide line on the iPhone 6 Plus, you'd draw a 0.33 wide line rather than a 1 point line. Of course, you still want to support all of the other screen resolutions too, so you'd use code like this to set your line width:
let lineWidth = 1.0 / UIScreen.mainScreen().scale
This will give you 1.0 for the non-retinal iPhones, 0.5 for the iPhone 4, 5 & 6, and 0.33 for the iPhone 6 Plus.

Can iOS Retina images have odd dimensions i.e. 28 x 15 px?

We are currently working with a design who is supplying Retina images to us with odd dimensions i.e. 28 x 15 px which I believe is incorrect as when you divide it you get an odd number like 14 x 7.5 px.
This is a rule I have always worked on but the designer is not getting the point and I thought I should double check what the exact rules are.
I've had add look on the web but cannot seem to find any references on this so it would be great to hear what everyone thinks on this matter.
Thanks
Yes you can, but NOT recommend.
For example, if you have an #2x image with 28 x 15px , your normal image will be 14 x 8px.
If you look close into the normal image, the pixels are not aligned well.
It is always recommended to use even number of pixel in dimension.
The #2x image needs to be exactly twice the width and twice the height of the standard image, or the automatic loading of it won't happen - your app will load and pixel-double the non-Retina image.
The standard image file will as a matter of course be a whole number of pixels wide and high, so you'll need the #2x to be even in its dimensions.
Tell your designer to catch on ;)
It's not possible because in Xcode you design your application with classic resolution pictures, and you can't use a float for width or height. So, you will have a one pixel gap difference between your classic and retina design. Maybe the easiest way to solve your problem is to add a transparent line of pixels in your high resolution picture.

Content Scaling with a non-dynamic world size in Corona SDK

I am using the Corona SDK to create a game in which I want a non-dynamic world size across all of the devices (i.e. 1440 x 960). However, the auto-scaling in Corona is not allowing me to do this consistently. For example, on the iPhone the screen moves two screens (480 * 2) pixels to the right (landscape mode), which is effectively 1440 px. However when switching to the iPad the scrolling is still moving two screen sizes to the right because it's viewing the iPad as 480 and not 1024 (the config file is set to 480 height and 320 width). Is there anyway to do this without turning off the content scaling? If I have to turn off content scaling, does that negate the advantages of the Corona SDK and the ability to code without thinking about the device?
thanks,
The point of Corona content scaling is exactly to not code multiple values on the logic...
So you have to either ignore the device size (if your world is "2 screen wide" it will be "2 screen wide" in ANY device) or turn off the scaling and handle the screen size manually...
I would not use the second option unless you want to torture users of smaller phones, that will see only a very small area of your playfield.

Resources