Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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 which 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 this here but this code should do that . The problem is , my drag formula is wrong .

for example

 public class DragSomething: 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 which 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 ... which is very bad 

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

 Xwidth = DrawTextureRect .width;// this is fine

 Yheight = DrawTextureRect .height;// this 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
Add comment · 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
1
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 this:

Make a Vector2 called cursorOffset.

Place this 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 think that might work.

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

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

I was given an example of offset usage which 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
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.

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

Trying to get my player to move forward, backward, left and right using WASD in relation to camera direction. My problem cannot be solved using AddRelativeForce, my player is a sphere. 1 Answer

Snapping Connection Points Together Rotation 0 Answers

Using OnGUI to change text in 3D text object. 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