- Home /
Gui.drawtexture using center instead of corner for positioning
is there a way to use the center to align the Gui texture instead of the top left as is the standard. I am trying to draw Gui textures using GUI.DrawTexture, but using the top right to draw the location is really confusing to me. I have a minimap in the top right corner of my screen, and I am trying to use that width and height to base my Gui textures off of, but the locations are wack. It might be because my camera width and height variables are wrong, but I can't tell. Here is one of my textures that is not drawing in the desired location (this one was supposed to draw in the center of the camera):
GUI.DrawTexture(Rect(Screen.width-(camera3Width*0.5)-(mapMarkerRect*0.5),
(camera3Height*0.5)+(mapMarkerRect*0.5),
mapMarkerRect,mapMarkerRect),mapMarker);
My camera height and width variables were originally using this script:
camera3Width = Screen.width*0.21;
camera3Height = Screen.height*0.4;
I was using this because the numbers I am multiplying the screen size by were the normalized viewport rect height and width numbers of the camera (minimap). Is there a different way to find the camera size in pixels or center the Gui Textures?
Answer by Jesse Anders · Jan 25, 2011 at 06:59 AM
I don't know that the GUI system provides any built-in support for what you describe. However, you could easily write a wrapper function to hide the details, so that you don't have to do all the math manually each time.
You should be able simply to offset the rect origin by half the width and height of the rect; that's what I've always done for 'centered' GUI controls. Unless there's more to the problem, that is (it looks like there might be, based on what you posted, but I'm not clear enough on the nature of the problem to comment further).
Yes, I asked whether there was a way to find the camera ($$anonymous$$imap) width and height in pixels, possibly using it's screen width and height which are .21 and .4, respectively. I tryed to multiply this by the width and height of the screen, but that didn't work, it ended up almost half the width and height of what it appeared to be.
I think I have done it right now, yet they are still appearing in the wrong spots when I maximize the game, but when it is $$anonymous$$imized, they are in the right locations, but the wrong sizes.
haha I figured it out, I had to set the camera height and stuff using the screen hieght in the OnGUI function ins$$anonymous$$d of Start (), which only set them at the beginning ins$$anonymous$$d of after it had maximized, because OnGUI goes every frame.