Improved script (Applicable to an object):
var btnTexture:Texture;
var levels : int = 0;
var op :float = 1;
var myCustomSkin : GUISkin;
var vzoom : float = 10;
var ix =5;
var iy = 5;
var bx = 25;
var by = 25;
var dbx =5;
var dby =5;
private var txw : float;
private var txh : float;
private var rtxt : float;
private var txtiw : float;
private var pressed:boolean = false;
private var pressed2:boolean = false;
private var pressed3:boolean = false;
private var starting : boolean = false;
private var ddx : float;
private var ddy : float;
private var windowOpen : boolean;
private var showbutton : boolean = false;
private var mpv : Vector2;
private var dangl : float;
private var iangl : float;
private var rotAngle : float ;
private var rotAngleF : float ;
private var oldrotAngle : float ;
private var anglrotg : float ;
private var pivotPoint : Vector2;
private var ixin : float;
private var iyin : float;
private var windowRect : Rect ;
private var rect : Rect;
private var ix3 : float;
private var iy3 : float;
private var ix2 : float;
private var iy2 : float;
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 () {
txw = btnTexture.width;
txh = btnTexture.height;
rtxt = (txw/txh);
txtiw = Screen.width/4;
hx = bx;
hy = hx/rtxt;
ix = Screen.width/2 - hx/2;
iy = Screen.height/2 - hy/2;
rotAngle = 0;
Hide();
}
function Update () {
//Zoom animation start
//---------------------------------------------------------------
if ( (starting) && (hx<txtiw)) {
hx = hx + vzoom;
hy = hx/rtxt;
ixin = ix- hx/2 - windowRect.x - 5;
iyin = iy - hy/2- windowRect.y -5;
ix = ixin + Screen.width/2;
iy = iyin + Screen.height/2;
}else if (starting){
hx =txtiw;
hy = hx/rtxt;
starting= false;
}
//-------------------------------------------------------------
//Variables
//---------------------------------------------------------------
if (!pressed && Input.GetMouseButtonDown(0) ){
mix = mpx ;
miy = mpy ;
hx3 = hx;
hy3 = hx/rtxt;
ix3 = windowRect.x;
iy3 = windowRect.y;
iangl = anglrotg;
pressed = true;
}else if(Input.GetMouseButtonUp(0) ){
pressed = false;
pressed2 = false;
pressed3= false;
}
ix2 = ix3 - (mpx-mix) ;
iy2 = iy3 - ((mpx-mix)/rtxt) ;
hx2 = hx3 + 2*(mpx - mix) ;
hy2 = hx2/rtxt;
windowRect = Rect (ix, iy, hx, hy);
}
//---------------------------------------------------------------
//Open window
//---------------------------------------------------------------
function OnMouseDown() {
windowOpen = true;
starting = true;
}
//---------------------------------------------------------------
function OnGUI () {
GUI.skin = myCustomSkin;
//Rotation GUI
//---------------------------------------------------------------
GUIUtility.RotateAroundPivot (rotAngle, pivotPoint);
//---------------------------------------------------------------
if(windowOpen) {
windowRect = GUI.Window (levels, windowRect, DoMyWindow, "");
windowRect = GUI.Window (levels+1, windowRect, DoMyWindowT, "");
}
if (windowRect.Contains(Event.current.mousePosition)) {
showbutton = true;
}
else {
showbutton = false;
}
var mp : Event = Event.current;
mpx = mp.mousePosition.x;
mpy = mp.mousePosition.y;
//Rotation angle
//---------------------------------------------------------------
anglrot = Mathf.Atan2(mpx-(windowRect.x+windowRect.width/2),mpy-(windowRect.y+windowRect.height/2));
anglrotg = anglrot*Mathf.Rad2Deg + 360;
rotAngleF = iangl - anglrotg;
//---------------------------------------------------------------
}
function DoMyWindow (windowID : int){
if(!starting && windowRect.width<= txtiw/2 ){
windowRect.width = txtiw/2;
ix2 = windowRect.x;
iy2 = windowRect.y;
}
if( !starting && windowRect.height<=txtiw/rtxt/2){
windowRect.height=txtiw/rtxt/2;
ix2 = windowRect.x;
iy2 = windowRect.y;
}
ix = windowRect.x ;
iy = windowRect.y ;
rect = Rect (windowRect.width - bx - dbx, windowRect.height-by -dby,bx,by);
if (showbutton || pressed2 || pressed3 ){
//Opacity slider
//---------------------------------------------------------------
op = GUI.HorizontalSlider (Rect (2*dbx, windowRect.height - dby - by/2 , windowRect.width - 2*bx -6*dbx , by ), op, 1, 0.1);
//---------------------------------------------------------------
//Zoom button
//---------------------------------------------------------------
if (GUI.RepeatButton (rect, "+")){
pressed2 = true;
ix=ix2;
iy=iy2;
hx = hx2;
hy = hy2;
}else if(pressed2){
ix=ix2;
iy=iy2;
hx = hx2;
hy = hy2;
}else{
GUI.RepeatButton (rect, "+");
}
//---------------------------------------------------------------
// Rotation button
//---------------------------------------------------------------
if (GUI.RepeatButton (Rect(windowRect.width - 2*bx - 2*dbx, windowRect.height-by -dby,bx,by), "o")){
pressed3 = true;
rotAngle = rotAngle + rotAngleF;
pivotPoint = Vector2( windowRect.x + windowRect.width/2 , windowRect.y+ windowRect.height/2 );
}else if(pressed3 ){
rotAngle = rotAngle + rotAngleF;
pivotPoint = Vector2( windowRect.x + windowRect.width/2 , windowRect.y+ windowRect.height/2 );
}else{
GUI.RepeatButton (Rect(windowRect.width - 2*bx - 2*dbx, windowRect.height-by -dby,bx,by), "o");
}
//---------------------------------------------------------------
//Close button
//---------------------------------------------------------------
if(GUI.Button(Rect(windowRect.width-bx-dbx, dby, bx, by),"X")){
Hide();
hx=0;
hy=0;
rotAngle = 0;
}
//---------------------------------------------------------------
GUI.DragWindow (Rect (0,0, windowRect.width , windowRect.height ));
}
}
//Texture window
//---------------------------------------------------------------
function DoMyWindowT (windowID : int){
GUI.color.a = op;
GUI.DrawTexture(Rect(0,0,windowRect.width,windowRect.height), btnTexture, ScaleMode.ScaleToFit, true, 0f);
}
//---------------------------------------------------------------
function Hide() {
windowOpen = false;
}