x


Help with transition from 2d programming with pixel based co-ordinates

I come from a background of coding flash applications for many years and I'm finding it hard to do things that were previously very simple with 2d pixel based space in Unity. I was hoping for some input on how one would go about doing them in 3d.

The lack of width and height relative to the screen is the main thing I am struggling to come to terms with. For example, in flash I could make an object fill the screen by simply doing:

// Make object size of the screen
myObject.width = stage.stageWidth;
myObject.height = stage.stageHeight;

Or I could center an object by doing:-

// Center an object in the middle of the screen
myObject.x = stage.stageWidth / 2 - myObject.width / 2;

I have worked out that localeScale and localePosition can be used for some things. If someone could post how they would commonly perform these tasks in Unity, along with any other examples, it would help me a lot.

Thanks

more ▼

asked Dec 05 '10 at 09:47 PM

boymeetsrobot gravatar image

boymeetsrobot
587 7 10 22

Dec 05 '10 at 10:26 PM yoyo

How does one scale an object to fit the screen dimensions or work out screen.width in relation to the world space? Even make one object the same size of another? Thanks for your help.

Dec 05 '10 at 11:58 PM boymeetsrobot
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I use something like this to achieve what you are trying to do:

public Camera cam;

...

Vector3 camUpperLeft = cam.ViewportToWorldPoint(new Vector3(0,1,-cam.transform.position.z));
Vector3 camUpperRight = cam.ViewportToWorldPoint(new Vector3(1,1,-cam.transform.position.z));
Vector3 camLowerRight = cam.ViewportToWorldPoint(new Vector3(1,0,-cam.transform.position.z));

float viewWidth = Mathf.Abs(camUpperLeft.x - camUpperRight.x);
float viewHeight = Mathf.Abs(camUpperLeft.y - camLowerRight.y);

...

Now, if your object has a scale of (1, 1, 1) by default you can just use viewWidth and viewHeight to adjust it's scale to fill the screen.

obj.transform.localScale = new Vector3(viewWidth, viewHeight, 1);

Hope that helps.

more ▼

answered Jun 23 '12 at 11:52 AM

delstrega gravatar image

delstrega
840 5 8 12

(comments are locked)
10|3000 characters needed characters left

Several possibilities:

  • You can limit yourself to GUIText, GUITexture, or the UnityGUI (=anything that goes in OnGUI(), for example GUI.Button(), GUI.Label, etc.). However, this way you restrict yourself to non-3D objects
  • With perspective cameras, objects need to be larger if they are further away, to have the same pixel size. To avoid that, you can just make your camera orthographic. This way, it will have no perspective distortion, objects are always the same size, no matter how far form the camera they are. The height of the screen in world coordinates is just camera.orthographicSize*2
  • For individual points, you can use the Approach @DelStrega mentioned, using ViewportToWorldPoint(), or better ScreenToWorldPoint(), which uses pixel values
  • The most flexible and powerful approach is to use a (or several) parallel plane to the camera, which is positioned in a way that world coordinates are directly transformed into screen pixels or a normalized viewport space. See my approach here for example on how to accomplish this. The two related main formulas connected to this are:
// required distance object<->camera so that object exatly fills
// (vertical) screen height:
cameraZ=0.5/Mathf.Tan(Camera.main.fieldOfView/2*Mathf.Deg2Rad)*
        heightOfYourObjectInWorldCoordinates

// required object height in world coordinates at given distance
// so that camera sees it at a certain pixel height
heightOfYourObjectInWorldCoordinates=
    2*Mathf.Tan(Camera.main.fieldOfView/2*Mathf.Deg2Rad)*
    objectDistanceFromCamera*desiredPixelHeight/Camera.main.pixelHeight

(NOTE: Camera.main.pixelHeight is the same as Screen.height, if your camera fills the whole screen (as opposed to for example a picture-in-picture camera)

EDIT before even posting: I just realized that @DelStrega revived this ancient question from 2010, which is generally not a good idea. But that was when I finished typing, so I'll post my answer anyway.

more ▼

answered Jun 23 '12 at 12:22 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

Wow, sorry, didn't realize that actually. I just clicked on "Unanswered" and this came up on the first page. Didn't check the date - my bad :(

Jun 23 '12 at 12:38 PM delstrega

It does that sometimes just to keep us on our toes:) @wolfram where do you get the year from? I just see month and day...

Jun 23 '12 at 01:27 PM whydoidoit

@DelStrega - no harm done ;-) I guess the original idea is of course to answer all unanswered questions, but with these old questions it is rather unlikely that it will help the original poster. But of course it might still help people who stumble upon this question, or search for a topic like this!

@whydoidoit - it's rather tricky and inconvenient, it might be a problem of the QATO engine itself. "Just" use mouse-over, to get a tool-tip with the full date ;-)

Jun 23 '12 at 01:31 PM Wolfram
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1032
x268
x41

asked: Dec 05 '10 at 09:47 PM

Seen: 1181 times

Last Updated: Jun 23 '12 at 01:32 PM