x


Trouble with GUIUtility.RotateAroundPivot

Hi All,

I am having all sorts of problems trying to rotate a GUI label on the screen. The following code will work exactly as expected but only if the label is placed in the center of the screen. If labelPosition (the pivot) is anything other than the center of the screen the label winds up in some strange place. It seems that the pivot actually has something to do with the center of the screen whether you like it or not.

Help please!

var xPosition : float = Screen.width/2;
var yPosition : float = Screen.height/2;

function OnGUI () 
{
    GUI.skin=rulerSkin;
    var labelPosition : Vector2 = Vector2(xPosition,yPosition);    

    GUIUtility.RotateAroundPivot (rotateAngle,labelPosition); 

    GUI.Label(Rect(labelPosition.x,Screen.height-labelPosition.y,100,10),"This is the label",GUIStyle("azimuthLabel"));

    // reset the matrix after rotation
    GUI.matrix = Matrix4x4.identity;
}   
more ▼

asked May 07 '12 at 06:12 PM

tom@whitebox gravatar image

tom@whitebox
46 13 14 16

anyone have any thoughts?

May 14 '12 at 05:12 PM tom@whitebox
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

If you want the label to rotate around its own center, then you mustn't use the center of the screen for labelPosition. Try this (I will call R the label's rect)

labelPosition = Vector2( R.x + R.width * 0.5, R.y + R.height * 0.5 );
more ▼

answered May 14 '12 at 05:39 PM

Berenger gravatar image

Berenger
11k 12 19 53

The problem is that if the label's position is anything other than the center of the screen, that the results are not what you would expect. If you take the following code and play with xPosition and yPosition I think you will see what I mean.

Any and all help is very much appreciated!

var xPosition : float = Screen.width/2; var yPosition : float = Screen.height/2;

function OnGUI () { var labelPosition : Vector2 = Vector2(xPosition,yPosition);

GUIUtility.RotateAroundPivot (rotateAngle,labelPosition);

GUI.Label(Rect(labelPosition.x,Screen.height-labelPosition.y,100,30),"This is the label");

// reset the matrix after rotation GUI.matrix = Matrix4x4.identity;

}

May 15 '12 at 09:45 PM tom@whitebox

Try that one :

#pragma strict

var labelRect : Rect = Rect( 100, 100, 100, 30 );
var pivotOffset : Vector2 = Vector2(0, 0);
var rotateAngle : float = 0.0;
var rotationSpeed : float = 2.0; 

private var currTimeRot : float = 0.0;

function OnGUI () 
{
	currTimeRot = Mathf.Repeat( currTimeRot +  Time.deltaTime * rotationSpeed, 360.0 );
	GUIUtility.RotateAroundPivot (rotateAngle + currTimeRot, 
				Vector2( labelRect.x + pivotOffset.x * labelRect.width,
						 labelRect.y + pivotOffset.y * labelRect.height ) );
	
	GUI.Label(labelRect, "This is the label");
}
May 16 '12 at 03:17 PM Berenger
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3668
x4

asked: May 07 '12 at 06:12 PM

Seen: 670 times

Last Updated: May 16 '12 at 03:17 PM