• 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
Question by DaiMangouDev · Mar 04, 2015 at 02:11 AM · c#guimathdrag

What is the mathematical formula for GUI dragging ?

Hi , when working with unity's GUI , there is a formula w$$anonymous$$ch is used when creating a drag behavior for GUI.Box, GUI.Button, GUI.DrawTexture etc....

I want to be able to drag any GUI no matter where on the GUI i click and drag . I just wrote t$$anonymous$$s here but t$$anonymous$$s code should do that . The problem is , my drag formula is wrong .

for example

 public class DragSomet$$anonymous$$ng: EditorWindow
 {
     private Rect DrawTextureRect = new Rect(0, 0, 50, 50); // reck of gui we will drag
 private bool Dragging ; // bool to check if we are dragging ...if mouse is down

 private float Xpos, Ypos, Xwidth, Yheight; // so that we can work with each value 

 private Rect GUIposition(Vector2 mousePosition,Rect guiRect)
 {
 
     // our bad math formula w$$anonymous$$ch will drag our gui but actually just throws the gui into a corner ... literally
     Xpos = mousePosition.x - DrawTextureRect.x; // if we had 540 as mousePosition.x  and 500 as DrawTextureRect.x , Xpos would be equal to 40 ... w$$anonymous$$ch is very bad 

 Ypos = mousePosition.y - DrawTextureRect.y;  // same here 

 Xwidth = DrawTextureRect .width;// t$$anonymous$$s is fine

 Yheight = DrawTextureRect .height;// t$$anonymous$$s is fine too

 return(new Rect(Xpos, Ypos, Xwidth, Yheight));
 }
 
 
 void OnGUI()
 {
 
  if (Event.current.type == EventType.MouseUp)
             {
                 Dragging = false;
             }
  if (Event.current.type == EventType.MouseDown && DrawTextureRect.Contains (Event.current.mousePosition))
             {
                 Dragging = true;
             }
 
 if(Dragging )
 {
 GUIposition(Event.current.mousePosition,DrawTextureRect); // give it the mouse position and the rect of the bui we want to drag
 }
 }

 GUI.DrawTexture(DrawTextureRect,sometexture);
 
 }

Comment

People who like this

0 Show 2
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 Glurth · Mar 04, 2015 at 02:18 AM 1
Share

one thing I see is, you keep using the current mouse position, even after a drag is started- I'd think you would want to use an OFFSET from the drag start position(which you would need to record).

avatar image DaiMangouDev · Mar 04, 2015 at 05:36 AM 0
Share

Thank you , that idea worked just fine

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by primemover · Mar 04, 2015 at 04:36 AM

When you capture mouse down in your Rect, find out how far you away from the Rects origin in x and y, then capture the mouse and subtract the x and y you found. Like what Glurth said.

Try t$$anonymous$$s:

Make a Vector2 called cursorOffset.

Place t$$anonymous$$s where you have Dragging = true, before it.

 cursorOffset.x = Event.current.mousePosition.x - DrawTextureRect.x;
 cursorOffset.y = Event.current.mousePosition.y - DrawTextureRect.y;

Then your math would be:

 Xpos = Event.current.mousePosition.x - cursorOffset.x;
 Ypos = Event.current.mousePosition.y - cursorOffset.y;

I t$$anonymous$$nk that might work.

Comment
DaiMangouDev

People who like this

1 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 DaiMangouDev · Mar 04, 2015 at 02:47 PM 0
Share

also works . I guess the formula comes in different forms but it is essentially always the same .

avatar image

Answer by DaiMangouDev · Mar 04, 2015 at 05:27 AM

I was given an example of offset usage w$$anonymous$$ch i had been unaware of

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class TestWindow : EditorWindow {
 
     [MenuItem ("Window/Test Window")]
     static void Init () {
         // Get existing open window or if none, make a new one:
         TestWindow window = (TestWindow)EditorWindow.GetWindow (typeof (TestWindow));
         window.Show();
     }
     
     Texture2D pixel;
     Rect windowRect;
     bool isDragging;
     Vector2 offset;
     
     public TestWindow()
     {
         pixel = new Texture2D(1, 3, TextureFormat.ARGB32, true);
         pixel.SetPixel(0, 0, new Color(1, 1, 1, 1));
         pixel.Apply();
         
         windowRect = new Rect(10,10,100,100);
         isDragging = false;
         offset = Vector2.zero;
     }
     
     void OnGUI()
     {
         //update
         Event evt = Event.current;
         
         switch(evt.type)
         {
             case EventType.MouseDown:
                 if(windowRect.Contains(evt.mousePosition))
                 {
                     isDragging=true; 
                   
                     //get offset from mouse
                 offset = evt.mousePosition - windowRect.position;
                 }
                 break;
             case EventType.MouseUp:
                 isDragging = false;
                 break;
             case EventType.MouseDrag:
                 if(isDragging)
                 {
                     windowRect.position = evt.mousePosition - offset;
                 }
                 break;
         }
         
         //render
         GUI.DrawTexture(windowRect,pixel);
         
         Repaint ();
     }
Comment

People who like this

0 Show 0 · 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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Unlocking & "Equipping" Colors onto Sprites with c# 2 Answers

How can I have a 1st person and 3rd person camera movement option for the player? 1 Answer

Drawing several BeginArea inside a BeginScrollview (GUILayout) produces an unexpected behaviour 1 Answer


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