- Home /
GUI Placement Question.
Hey guys I'm currently making a mini map to go on a screen. I need it to go in the top right of the screen. When placing things on the screen such as:
GUI.Label (Rect (0,Screen.height - 390,150,150), imageFile);
obviously the "0" in the first parameter is easy to do because it's in the top left of the screen.
However, how would I code it to always be at the top RIGHT of any screen on any resolution? I'm sure it's easy...just...unknown to me, lol.
Answer by aldonaletto · Jul 13, 2011 at 02:51 AM
You should subtract the rectangle width from the screen width. Supposing W and H are the image width and height:
GUI.Label (Rect(Screen.width - W, 0, W, H), imageFile);
EDITED: Well, I finnally got what's the problem: you've altered the GUI scale using GUI matrix, but nobody told it to Screen.width. In other words, Screen.width is still expressed in scale 1, while all GUI itens are using the scale you've defined. The solution is to convert the screen width to this scale:
// calculate scale
var scale: float = Screen.height / nativeVerticalResolution;
var newScreenW: float = Screen.width / scale; // adjust screen width...
var newScreenH: float = nativeVerticalResolution; // and height
GUI.matrix = Matrix4x4.TRS(Vector3(0,0,0),Quaternion.identity,Vector3(scale,scale,1));
// the rect will be calculated using the new screen width:
GUI.Label (Rect(newScreenW - W, 0, W, H), imageFile);
// the same thing, but at the bottom-left corner:
GUI.Label (Rect(0, newScreenH - H, W, H), imageFile);
You should set the W and H values in the above example to the width and height of the texture, or else the image will appear at the wrong position. The image in GUI.Label is always aligned to the top-left of the rect passed as the first parameter; if the rectangle has a different size, the image will not appear where you expect.
Yeah, you mean like:
GUI.Label (Rect(Screen.width - imageFile.width, 0, imageFile.width, imageFile.height), imageFile);
(even if I defined them as variables, it's the EXACT same thing as doing what I just did, yes?)
?
I currently have the $$anonymous$$atrix:
GUI.matrix = $$anonymous$$atrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
to automatically set the GUI to the current screen resolution. The code you provided works WITHOUT that in it, but ofcourse, there are other problems. I think it is ideal to have the matrix for obvious reasons, but again, the code you provided is not working with it.
Dear God! What are you trying to do? $$anonymous$$essing with GUI.$$anonymous$$atrix can drive you nuts, unless you know exactly what you're doing! But you may try a simple alternative: save the original GUI.$$anonymous$$atrix, do whatever you want to do with your matrix, then restore GUI.$$anonymous$$atrix:
function OnGUI(){
GUI.Label(Rect(....),imageFile);
var old$$anonymous$$atrix = GUI.matrix; // save original matrix
GUI.matrix = $$anonymous$$atrix4x4.TRS(...);
// do whatever you want with the new matrix
GUI.matrix = old$$anonymous$$atrix; // restore it
}
I used this trick once to rotate a texture in a map.