OpenGL ES : getting a fixed size for an object - iphone

I have an open GL ES (1.1) scene with many 3d objects and a "player" model. I'd like the player to have the same pixel size, regardless of the screen orientation on an Android phone or Iphone.
I'm not using glOrtho or billboards. That's a perspective 3d scene, but I just want the objects to have the same size in both screen orientation. Currently, if I rotate the phone, I keep the same aspect ratio but the scene "zooms out" in landscape mode.
I suspect that I have to play with parameters to glFrustrum to get this; but can't figure out yet how to do it.
So any ideas are welcome!
Thanks

You will need to change the aspect ratio when the device is turned to go from a the otherwise the size of the objects are going to change. THink of yourself looking out through a window, the objects on the other side of the window are only going to be the same size if you don't change your distance from the window (i.e. zooom in and out), when you "turn" the window sidewayse, the aspect ratio of the window changes (the metaphor is starting to not work).
If you draw a square in the view with the side length being the short side of the screen, then you should still have a square when you turn the phone sideways, still covering the same area on the screen.
Things will probably be easier to calculate if you use the code from gluPerspective. You set the aspect ratio to the actual aspect ratio, fix the fovy for the first aspect ratio. You can then use what would be fovx for this aspect ratio as the fovy for your rotated view.

Related

Unity gameobjects and text ui change size and position while changing resolution

sorry I have to put all of this as an image but I couldn't post this with text as when creating the original post something happened to where it was flagged as spam. I figured this would be the only way?
Unity's UI screen is different from the in-game world space.
For the UI part you can easily fix the issue of the size and position by considering the following:
First thing you need to check is the Canvas Scaler component which is automaticaly generated on your Canvas gameobject.
Canvas Scaler Example
First under The UI Scale Mode you should choose the "Scale with Screen Size" which will make UI elements bigger the bigger the screen is.
Second is the Reference Resolution which is the resolution the UI layout is designed for. If the screen resolution is larger, the UI will be scaled up, and if it's smaller, the UI will be scaled down. A good example is to set the dimensions int something like this (1920x1080) which is the most common landscape resolution.
Third is the Screen Match Mode which is a mode used to scale the canvas area if the aspect ratio of the current resolution doesn't fit the reference resolution. With other words if you change your in-game resolution this will handle your UI elements.
Fourth is Match which will determine if the scaling is using the width or height as reference, or a mix in between. So if your game is in landscape mode you should set it to 0, if it's in portrait mode you should set it to 1.
Last thing to keep in mind that will affect your UI elements position is the Anchor presets which is part of the Rect Transform component on each of your UI elements.

How to scale unity UI for every device

I'm trying to make a level select screen for my game, but when testing the game build the ui isn't scaled that well
I'm currently using the Canvas Scaler with the following settings
These settings give me..interesting results to say the least
What the result is SUPPOSED to be is this
How am I able to do this?
There a few things that you should keep in mind while building the UI for different screens.
Have a target Aspect ratio. Generally it should be 16:9 for landscape and 9:16 for Portrait.
Make sure the reference resolution is in line with your target aspect ratio.
Set the Match option of canvas scalar to 0.5 to scale evenly. If you are sure the device aspect ratio will change in only one direction then set either height or width. For example a portrait game will scale only by height if you switch from 9:16 to 9:18.(0.5 has worked always for me)
Select the aspect ratio in the game window and set the Anchors of the UI to the corner of the UI element. That way Unity will know where the element should be based on Canvas size.
Here is a video that demonstrates it
https://youtu.be/08S1VGL9afg
First of all, use some custom resolution in game view instead of Free Aspect.
Then you could try adjusting Match with Height or width to 0.5.
In some case, you have to adjust custom anchors using these little guys
If you want to absolutely preserve aspects, use this option on Image component.

How to resize camera to work with multiple resolutions on Unity?

I'm working on a fun project on Unity and I want to support all mobile resolutions in landscape mode. I designed everything to work in 1920:1080 resolution.
Everything works in world space, including UI elements.
What's correct way of supporting all resolutions (including weird ones like square 1:1)?
I don't want the scene to be cropped or filled with blue, all I want is my camera's viewport to be scaled to fit the device screen. I don't care if objects in the scene will get thin or fat.
To support different aspect ratios for your UI Elements, I recommend making use of the anchors that come with every RectTransform. These will ensure that the position of an element is consistent across various aspect ratios. For example, setting the anchor of an element to be left on the x axis will make the origin and pivot on the far left. This means an x position of 10 will position the element 10 units away from the far left of the element's parent. This is made easier with Anchor Presets. Although you can set every value yourself, Anchor Presets provide an easier way to anchor your UI Elements. Provided you have a RectTransform on you UI Elements, every element should have the Anchor Presets available. No need to change the width and height of your Canvas.
Aside from that the Camera should automatically resize depending on the resolution of the game view. You can test how your elements' anchoring works with different aspect resolutions by setting your game view's Aspect to free aspect, which will change the aspect ratio of the game depending on game view's width and height.

Physical Camera Viewport should adjust with the UI for different resolution

I have a physical camera in the scene which renders a 3D Scene. The 3D Scene is rendered between the Top UI Bar and the Bottom UI Bar and has been set properly for reference resolution 720 x 1280.
So, the problem is when the resolution changes, the UI is set properly for that particular resolution. But the 3D scene that is rendered between the 2 UI parts, doesn't sit properly between them. I am attaching 2 reference images for easier understanding.
The below-given image is based on the reference resolution and the 3D Scene is properly fit between the 2 UI parts.
The below-given image is for another resolution where the UI adjusts itself accordingly. But the 3D scene doesn't, i.e the camera should move to fit the 3D Scene between the UI.
So, is there any way I can move the camera according to the resolution so that it fits properly between the UI for different aspect ratios.
Thank you.
First of all, try looking at the ViewportRect property of the camera - it enables you to crop the viewport to an area which should enable the effect you seek.
This however has the limitation of keeping the 'center scren' axis of perspective, which in some cases is uncalled for.
An 'should always work' solution is rendering to a RednerTexture - camera adapts its screen aspect to the aspect of the RenderTexture, and if you display your texture on RawImage, you should get dececnt results. This however has some performance cost (memory readout is not the fastest on mobile).
In case the cost woul be unnaceptable, its possible to construct custom camera projection matrix, that would respect your desired viewport rect, and also allow arbitrary perspective, but its not trivial unfrotunately. Here is some more information
https://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html

Canvas too big for the camera in Unity

I have created a 2D game with an orthogonal camera and using 16:9 display size.
I dragged my background image onto the hierarchy (it's about 2048x1152) and then set the camera size to be 22.5, which made it fit the background perfectly and displays just right.
However, when I add a Canvas for a UI it is absolutely giant, about 100 times bigger. It only becomes 'normal' size with respect everything else added when I set the camera to its default size of 5. So when I add a small graphic, it too becomes giant.
I'm simply following a book I read and I'm not doing anything to deviate.
Am I doing something wrong? Below is what I mean. The background image is the little image in the bottom right and the outlined rectangle is the canvas with a small graphic added.
Thanks.
To force your Hierarchy Canvas UI to the same resolution as the Camera View in your Unity Editor Scene window resolution (i.e. not ridiculously massive), or in other words get the Canvas to fit into the Camera size in the Scene, do the following:
Set the Canvas component's Render Mode to Screen Space - Camera.
Make sure you select or drag the relevant Camera from the Hierarchy to the Render Camera field in the Inspector.
You should use the Unity canvas for this along with the canvas scaler component. If I'm not mistaken it will scale all elements relative to the screen they are viewed on.
The canvas scaler allows you to match the scaling based on a preferred viewport size which is a life saver.
However this may not fit you needs perfectly as it would mean that the background element would become fixed. So if you wanted to pan the element you would need to move it's x and y elements within the canvas.
Hope that helps?