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

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.

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.