- Home /
Making an object resolution-independent
I'm working on a GUI that doesn't use Untiy's GUI system. Instead, it's composed of objects rendered by a second camera. How do I make these objects resolution independent? I gave it a shot and came up with this code:
static public Vector3 GUIPosition(float x, float y) { GameObject GUICamObject = GameObject.FindWithTag("GUICam"); Camera GUICamera = GUICamObject.GetComponent<Camera>();
Vector3 pos = GUICamera.ViewportToWorldPoint(new Vector3((x)/100,-(y-100)/100,9));
return pos;
}
static public void Element(GameObject button, float x, float y) { Vector3 buttonPosition = GUIPosition(x, y); button.transform.position = buttonPosition; }
But it doesn't do the trick; on higher resolutions, object positions tend to distort anyways. I'm obviously going about it wrong; what's the "right" way to make an object resolution independent?
Answer by Jesse Anders · Apr 05, 2011 at 10:47 PM
Assuming the UI camera's projection is orthographic, you can make the orthographic size a fixed value. This gives you the height of the 'UI area'; the width can then be derived from the height and the aspect ratio.
Setting aside aspect ratio for the moment, since you can set the size of the orthographic projection yourself, there shouldn't be any issues with resolution independence as far as that goes. For example, say the orthographic size is 50, meaning that the height of the projection is 100. A UI element that's 25 units tall will then take up a quarter of the screen's heights regardless of resolution. This doesn't give you pixel-for-pixel graphics of course, but it does give you resolution independence.
In practice, aspect ratio complicates things of course, in that you may still have to do some dynamic positioning based on the width of the projection. But, that problem is more or less unavoidable regardless of what approach you take, I think.
Right, I've already managed to set the size of the orthographic projection. $$anonymous$$y problem now is the dynamic positioning you mentioned; that's what the above code does, and it works at lower resolutions. However, at higher resolutions, objects tend to slide off the sides of the screen; could it be something with the way I handle the width/x position ratio?
What's the code intended to do exactly? For example, what are the 100's for? (Also, I don't see any reference to the aspect ratio there, which might have something to do with it.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613120924im_/https://answers.unity.com/themes/thub/images/avi.jpg)