I'm making a 2D project in Unity3D. I want to place an image in the right upper corner. I've got this working, but ofcourse the image is falling of the screen for a part (and completely when I change the screenrotation).

enter image description here

This is the code I've got for it;

posImage = new Vector2(Screen.width - imgGear.transform.localScale.x, Screen.height - imgGear.transform.lossyScale.y );

As you can see in the code I've tried to adapt the position with the size of the object, using localScale 1.0f and lossyscale 0.9...f doesn't really do much as it will be changed with 1 unit. Is their a way to get the real width and height of the objects?


I got same problem for atleast 2 week until i figured myself how to handle object in different resolution which preventing object falling apart or place object based with the screen resolution .

If you want to keep the object right in the position following camera , don't forget to put the object inside camera object .

this is my script to control the resolution :


using UnityEngine;
using System.Collections;

public class calculateresolution : MonoBehaviour {

    // change this to resolution size when you create the game 
	float desiredscreenheights = 768;
	float desiredscreenwidths = 1366;

void CalculateResolution(string theobjectm) {
	GameObject objecttocalculate = GameObject.Find(theobjectm);
	float desiredscreenheight = desiredscreenheights;
	float desiredscreenwidth = desiredscreenwidths;
	float desiredaspectratiowidth = desiredscreenwidth / desiredscreenheight;
	float desiredaspectratioheight = desiredscreenheight / desiredscreenwidth ;
	float screenheight = Screen.height;
	float screenwidth = Screen.width;
	float aspectratioheight = screenheight / screenwidth;
	float aspectratiowidth = screenwidth / screenheight;
	Debug.Log (screenheight + " " + screenwidth + " " + aspectratioheight + " " + aspectratiowidth);
	float objectsizex = objecttocalculate.transform.localScale.x;
	float objectsizey = objecttocalculate.transform.localScale.y;
	float objectsizez = objecttocalculate.transform.localScale.z;
	float objectcoordx = objecttocalculate.transform.localPosition.x;
	float objectcoordy = objecttocalculate.transform.localPosition.y;
	float objectcoordz = objecttocalculate.transform.localPosition.z;
	Debug.Log (objectsizex + " " + objectsizey + " " + objectsizez + " " + objectcoordx + " " + objectcoordy + " " + objectcoordz);
	float newobjectsizex = aspectratiowidth / desiredaspectratiowidth * objectsizex;
	float newobjectsizey = objectsizey;
	float newobjectsizez = objectsizez;
	float newobjectcoordx = aspectratiowidth / desiredaspectratiowidth * objectcoordx;
	float newobjectcoordy = aspectratiowidth / desiredaspectratiowidth * objectcoordy;
	float newobjectcoordz = objectcoordz;
	Debug.Log (newobjectsizex + " " + newobjectsizey + " " + newobjectsizez);
	Debug.Log (newobjectcoordx + " " + newobjectcoordy + " " + newobjectcoordz);
	objecttocalculate.transform.localScale = new Vector3 (newobjectsizex, newobjectsizey, newobjectsizez);
	objecttocalculate.transform.localPosition = new Vector3 (newobjectcoordx, newobjectcoordy, newobjectcoordz);
	Debug.Log ("Calculate Finish For Now");

void Start () {


// Update is called once per frame
void Update () {
// your gameobject name 
CalculateResolution ("yourobjectname ");

To calculate the object , just call CalculateResolution("yourgameobjectname") , change yourgameobjectname with the object name .

Change "float desiredscreenwidths" and "float desiredscreenheights" value to resolution size you use to build the game .

But unfortunally this script does not support screen rotation because i did not know how to :I sorry , or maybe you tell me how ;) .

  • \$\begingroup\$ EDIT : adding new method \$\endgroup\$ – user6668201 Nov 27 '16 at 9:56
  • \$\begingroup\$ This is a rather long code :p For the rotating aspect, you've manually filled in the screen resolution. If you fill in screen.width and screen.height instead of the numbers, wouldn't that automatically adapt when screen is rotated? I will put this code in my test environment and check if it gives me some nice output. \$\endgroup\$ – Budan18 Nov 27 '16 at 10:34
  • \$\begingroup\$ okay ;) oh yes im trying to make it work for screen rotation but i did not yet figured how \$\endgroup\$ – user6668201 Nov 27 '16 at 10:35
  • \$\begingroup\$ for screen rotation it's used screen.orientation docs.unity3d.com/ScriptReference/Screen-orientation.html but i still learning the function \$\endgroup\$ – user6668201 Nov 27 '16 at 10:37
  • 1
    \$\begingroup\$ Moved from start() to update() ! :) thanks for the solution ! \$\endgroup\$ – user6668201 Nov 27 '16 at 11:08

When I putted the positioning script in the update part instead of the start function, it changed position perfectly when rotating screen. For the "real size" of the object I might have to puzzle a bit.

I know that repositioning on every update can be causing some lag in the application but since it is a menu screen, it doesn't cause mayor delays in the application (menu's and popups still open instantly).

this is how i got it now :) enter image description here


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.