canvas can be fixed for all screen sizes but I want to fit gameobjects which is not in canvas, for all sizes of screen.

This is the transform of my game object

Transform of a gameobject The game object's size and position variation

UI is like this in 720pxX1280px UI is like this in 720pxX1080px My question is how to render the gameobject with same size and same position for all screens

  • \$\begingroup\$ The same position relative to what reference point? Since the two resolutions you've shown have different aspect ratios, you'll end up with more space to fill somewhere, and you'll need a policy on how to fill it (eg. expand the view to both left & right equally, as is the default, or lock content relative to the top-left corner or some other anchor, etc...) \$\endgroup\$ – DMGregory Jul 17 '18 at 17:34
  • \$\begingroup\$ Im a beginner in game development. I can't understand the points u said in the braces "()" @DMGregory sorry :( \$\endgroup\$ – c0d3er Jul 17 '18 at 17:40

If you want to do this with UI objects, then it's pretty straight forward using anchors.

Since your case seems to be with 3d objects, it all depends where you want to place the object. Here's some math:

// This is the value on the Z axis, where the object can be placed
// You can populate it with something like `posZ = myObj.position.z;`
float posZ;

// This is your scene's camera, based on its field of view and aspect, it will calculate positions
Camera cam;

// This is the width and height of the "plane" that is visible on the screen on position `posZ`
float height = (Mathf.Tan(cam.fov *Mathf.Deg2Rad*0.5f) *posZ *2f) /10f; 
float width = height *cam.aspect;

Credits for this code go to this answer on stackoverflow

Based on this, you can use the width and height to calculate the screen's edges and place objects there. For example placing a cube on position (width/2, 0, 10) (assuming posZ = 10;) then the center of the cube would be placed on the right part of the screen, making it partially visible.

If you want to place the cube in such a way that it is touching the right part of the screen, and is almost fully visible, then you can assign its position to something like (width -cubeWidth/2, 0, 10).

You can get a Mesh's size with myMesh.GetComponent<MeshRenderer>().mesh.bounds.size which is a Vector3 with the object's dimensions. Note that this shows the bounding box of the mesh, keep that in mind when handling complex meshes.


and the answer is unity will automatically do this 🤦‍♂️we don't need to care of screen resolution for gameobjects.In game window of unity the position and size will be different but in real time case of testing in different resolution mobiles,the unity will resize and position it correctly.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.