• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by benfattino · Nov 24, 2011 at 11:39 PM · dragwindowzoom

drag zoom window

I made a script that draws a drag window allows holding down a button at the bottom right to zoom the window in real time with mouse. It works, but when I move the mouse too quickly falls behind the window and not longer able to resize it, because mouse go out of window and the button is no longer pressed. Anyone suggest me a solution? Thanks!

 var ix = 5;
 var iy = 5;
 var bx = 25;
 var by = 25;
 var dbx = 5;
 var dby = 5;
 var hix = 200;
 var hiy = 200;
 private var windowRect : Rect;
 
 private var hx : int;
 private var hy : int;
 private var hx2 : int;
 private var hy2 : int;
 private var hx3 : int;
 private var hy3 : int;
 
 private var mpx : int;
 private var mpy : int;
 private var mix : int;
 private var miy : int;
 
 function Start () {
 hx = hix;
 hy = hiy;
 windowRect = Rect (ix, iy, hx, hy);
 }
 
 function Update () {
 
 if (Input.GetMouseButtonDown(0)){
 mix = mpx ;
 miy = mpy ;
 hx3 = hx;
 hy3 = hy;}
 
 ix = windowRect.x;
 iy = windowRect.y; 
 hx2 = hx3 + mpx - mix ;
 hy2 = hy3 + mpy - miy ;
 
 windowRect = Rect (ix, iy, hx, hy);
 }
 
 function OnGUI () {
     windowRect = GUI.Window (0, windowRect, DoMyWindow, "");
     var  mp : Event = Event.current;
     mpx = mp.mousePosition.x;
     mpy = mp.mousePosition.y; 
     }
 
 function DoMyWindow (windowID : int){ 
     if ((GUI.RepeatButton (Rect (hx-bx-dbx,hy-by-dby,bx,by), "x")) && (hx2>hix) && (hy2>hiy)){
     hx = hx2;
     hy = hy2;
     }
     
     GUI.DragWindow ();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mox.du · Nov 25, 2011 at 07:01 AM

Here you go.

 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;
 var hix = 200;
 var hiy = 200;
 private var windowRect : Rect;
 
 
 private var hx : int;
 private var hy : int;
 private var hx2 : int;
 private var hy2 : int;
 private var hx3 : int;
 private var hy3 : int;
 
 private var mpx : int;
 private var mpy : int;
 private var mix : int;
 private var miy : int;
 
 function Start () {
 hx = hix;
 hy = hiy;
 windowRect = Rect (ix, iy, hx, hy);
 }
 
 function Update () {
     
    if (Input.GetMouseButtonDown(0)){
 
     pressed = true;
 mix = mpx ;
 miy = mpy ;
 hx3 = hx;
 hy3 = hy;
 }else if(Input.GetMouseButtonUp(0)){
     pressed = false;
     pressed2 = false;
 
     }
 
 
 ix = windowRect.x;
 iy = windowRect.y; 
 hx2 = hx3 + mpx - mix ;
 hy2 = hy3 + mpy - miy ;
 
 windowRect = Rect (ix, iy, hx, hy);
 }
 
 function OnGUI () {
     
     windowRect = GUI.Window (0, windowRect, DoMyWindow, "TRT");
     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));
    
     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, "x")) ){
    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, "x");
         }else{
                 GUI.RepeatButton (rect, "x");
         }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image benfattino · Nov 25, 2011 at 01:58 PM 0
Share

Amazing! Thanks.

avatar image
0

Answer by benfattino · Dec 18, 2011 at 04:08 PM

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;
      }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Popup window size problem 1 Answer

How to open a separate window 1 Answer

Button Drag 2 Answers

drag window 2 Answers

Move one GUI window when another is moved? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges