Right so im having a slight issue with the gui commands, basically just with the positioning of the elements.
After trying to run it the engine, getting the button alligned up with a plane im using for the background texture, and using the screen width and height for calculating size and position - im still getting incorrect positions.
function OnGUI () {
var a = Screen.width;
var b = Screen.height;
if( GUI.Button(Rect((a/2.98),(b/2.25),(a/2.88),(b/17)), "New Mission") ){
Application.LoadLevel(1);
}
}
This is the code from my latest attempt.
It pretty much places the button in the center of the screen (slightly upwards tho).
The thing is, when i try out the different aspect ratios in the engine it looks decent in pretty much any format.
However the second i make a build and run it, no matter what aspect ratio/resolution i run it in - the button moves so its halfway between the topleft corner and the middle.
Is my code horribly wrong or is there some speciel trick to positioning these elements that i havent figured out yet?
I wouldn’t do divisions like that. Kind of hard to really place things exactly.
The easiest way is to offset from the sides or the center:
panelRect = Rect(5, 5, 100, 100); // start from top left of screen with padding of 5
// or
panelRect = Rect(Screen.width/2 + offsetHorizontal, Screen.height/2 + offsetVertical, ...); // offset from center of screen exact pixels
Try one of those and see if you have an easier time aligning things.
Thanks for the tip, still havent gotten it to work though - but im beginning to get the feeling that its not the code thats at fault here.
Basically just to try it out fast i tried setting the initial position of the box to the center of the screen (height and width / 2) just to see what happend.
The result was that it ended up in the excact same position as before(about halfway between middle and top left corner).
Its as if it just ignores the GUI settings once its building.
edit: added some example pictures:
testimage in engine: engineimage
testimage in player: playerimage
edit2: Thought i should mention, the result is the same no matter what i build it as - e.g. if i go in the editor and tell it to show “standalone” - and then actually build a standalone the result is the same. Fine in the editor, crap in the build.
You should make the sizes relative to the resolution you crated them to be sure you will have them with the same proportion and position in other resolutions.
I did something like this:
First of all you need to calculate the screen factor:
float factor = 768/ Screen.height;
(768 would be the screen height wich I used to create my GUI elements)
Then you need to get the final rect size:
float rect_w = 100 * factor;
float rect_h = 100 * factor;
float xcenter = Screen.Width * factor * 0.5f - (rect_w * 0.5)
float ycenter = Screen.Height * factor * 0.5f - (rect_h * 0.5)
panelRect = Rect(xcenter, ycenter, rect_w, rect_h)
This should be enough to have your position and size relative to the screen size.