- Home /
Question by
benfattino · Nov 26, 2011 at 05:38 PM ·
guirotatewindow
GUI Rotation
I use script to mouse zooming GUI window pressing button. Is it possible to make another button to rotate GUI window following mouse position? This is the script:
var btnTexture:Texture;
private var rtxt : float;
private var txtiw : float;
private var pressed:boolean = false;
private var pressed2:boolean = false;
private var recordMouseX:boolean = false;
var ix = 5;
var iy = 5;
var bx = 25;
var by = 25;
var dbx =5;
var dby =5;
private var windowRect : Rect;
private var hx : float;
private var hy : float;
private var hx2 : float;
private var hy2 : float;
private var hx3 : float;
private var hy3 : float;
private var mpx : float;
private var mpy : float;
private var mix : float;
private var miy : float;
function Start () {
rtxt = (btnTexture.width/btnTexture.height);
txtiw = Screen.width/2;
hx = txtiw;
hy = txtiw/rtxt;
windowRect = Rect (ix, iy, hx, hy);
}
function Update () {
if (Input.GetMouseButtonDown(0)){
pressed = true;
mix = mpx ;
miy = mpy ;
hx3 = hx;
hy3 = hx/rtxt;
}else if(Input.GetMouseButtonUp(0)){
pressed = false;
pressed2 = false;
}
ix = windowRect.x;
iy = windowRect.y;
hx2 = hx3 + mpx - mix ;
hy2 = hx2/rtxt;
windowRect = Rect (ix, iy, hx, hy);
}
function OnGUI () {
windowRect = GUI.Window (0, windowRect, DoMyWindow, "");
var mp : Event = Event.current;
print( windowRect.x+ " mouseX "+ mpx);
mpx = mp.mousePosition.x;
mpy = mp.mousePosition.y;
}
function DoMyWindow (windowID : int){
GUI.DragWindow (Rect (0,0, windowRect.width - bx -dbx, windowRect.height));
GUI.DragWindow (Rect (0,0, windowRect.width, windowRect.height - by-dby));
GUI.DrawTexture(Rect(0,0,hx,hy), btnTexture, ScaleMode.ScaleToFit, false, 0f);
var rect = Rect (windowRect.width - bx - dbx, windowRect.height-by -dby,bx,by);
if(windowRect.width<=bx +(dbx *2) ){
windowRect.width = bx+(dbx *2);
}
if( windowRect.height<=by+(dby*2) + 15 ){
windowRect.height=by+(dby*2) +15;
}
if (pressed && (GUI.RepeatButton (rect, "+")) ){
recordMouse = true;
pressed2 = true;
hx = hx2;
hy = hy2;
}else if(pressed && pressed2 && !rect.Contains(Input.mousePosition) ){
hx = hx2;
hy = hy2;
} else if(!pressed ){
GUI.RepeatButton (rect, "+");
}else{
GUI.RepeatButton (rect, "+");
}
}
Comment
Best Answer
Answer by perchik · Dec 02, 2011 at 07:07 PM
I think what you'll need to do is use GUIUTility.RotateAroundPivot. To make this work, you have to use an OpenGL type approach. First, store the GUI project matrix:
Matrix4x4 matrixBackup = GUI.matrix;
Then you can rotate the GUI projection matrix, draw whatever you want to draw, and then reset the GUI matrix from the backup:
GUIUtility.RotateAroundPivot(angle, new Vector2(0,0));
GUI.Box(new Rect(0,0, 1, 1));
GUI.matrix = matrixBackup;
That should give you enough to at least figure out how to rotate the GUI. You could place the pivot point and then calculate the angle between the vector from the pivot to the object and the pivot to the mouse.