- Home /
Locking Object To Screen
Hello, I want to ask question about screen orientation.
I Want to lock my objects to spesific position and scale in any screen resoulution.
For Example:
1024x600 is my testing tablet.
800x640 is another device
I want to make this. In two orientations should be the same. If ı couldn't understand I can send a picture. Thanks.
Are you talking about game objects in world space, or GUI elements? And what is your reference for 'specific position'...the center of the screen or some other position? A picture might be helpful.
all things in this game is "GameObject" not GUI Element. I want their position is fixed at GUI (Or screen I dunno).
Answer by ________ · Aug 15, 2013 at 10:29 AM
Use the following code on your camera:
public float worldUnitsPerScreenPixel { get { return (camera.orthographicSize * 2f) / camera.pixelHeight; } }
public float cameraWidth { get { return worldUnitsPerScreenPixel * camera.pixelWidth; } }
public float cameraHeight { get { return worldUnitsPerScreenPixel * camera.pixelHeight; } }
You then have the height and width of the camera in the world space, you can make your gui elements children of the camera and offset their local position by half the width and/or half the height to get their transforms aligned with the corners of the "visible area".
Oh FYI, I'm assuming this camera is orthographic.
Edit:
You could also use:
camera.ScreenToWorldPoint(screenPosition);
Although I find the first method to be more useful as more data is exposed.
Domo arigato :D I'll try it, If I couldn't handle I'll post there again :)