398839 (2) [Avatar] Offline
#1
Within Listing 3.3, on page 52, we have the OnGUI() method. Within this method, we define a size for our label, then define two variables for the lower left hand corner of the rectangle we are about to draw, posX and posY, i.e.

void OnGUI() {
    int size = 12;
    float posX = _camera.pixelWidth/2 - size/4;
    float posY = _camera.pixelHeight/2 -size/2;
    GUI.Label(new Rect(posX, posY, size, size), "*");
}


Why in posX do we take away size/4 and not size/2? Surely this means that the asterisk in the GUI will be 3 pixels skewed to the right off center?

Thanks.
jhocking (185) [Avatar] Offline
#2
Honestly the size offset there was me just kind of eyeballing what looks right; you might find that a different value is better. The reason it's not as simple as "divide by 2" is because the rectangle size of a gui label isn't the size of the text itself, it's the size of the text area the text is printed in. Thus, saying the gui label has a size of 12 doesn't necessarily mean the character is 12 pixels.
398839 (2) [Avatar] Offline
#3
Thank you for clarifying; cheers!
406056 (1) [Avatar] Offline
#4
I have typed in the code exactly as Listing 3.3, but when I come to run the game I do not see the asterix. I have checked the code a dozen times to makes sure that it is exactly the same as that in the Listing 3.3.

I am missing something?

Kr,

Chee
jhocking (185) [Avatar] Offline
#5
Well first off, is there any error showing up in the console? If not and the asterix isn't appearing, you should check that the code is even running; try putting in Debug.Log() messages and see if they show up.
.Disorder (7) [Avatar] Offline
#6
406056 wrote:I have typed in the code exactly as Listing 3.3, but when I come to run the game I do not see the asterix. I have checked the code a dozen times to makes sure that it is exactly the same as that in the Listing 3.3.

I am missing something?

Kr,

Chee


Check if you typed OnGui() instead of OnGUI()

At least that was my problem.
534292 (1) [Avatar] Offline
#7
.Disorder wrote:
406056 wrote:I have typed in the code exactly as Listing 3.3, but when I come to run the game I do not see the asterix. I have checked the code a dozen times to makes sure that it is exactly the same as that in the Listing 3.3.

I am missing something?

Kr,

Chee


Check if you typed OnGui() instead of OnGUI()

At least that was my problem.


I tried the same thing and I can't get the GUI to show. It's literally identical to the code listing, the function is in my RayShooter script, and the script is attached to my camera. No idea what's going on. I can log from the OnGUI function fine (lot and lots of log messages since it runs every frame), but the GUI just doesn't seem to render.

EDIT: nevermind, I was using _camera.pixelWidth for both posX and posY, so it was trying to render the character offscreen. I need to be using pixelHeight for posY. Duh.