Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 arisonu123 · Jul 08, 2017 at 05:11 PM · guieditor-scriptingeditorguidragging

Dragging GUI Item, get mouse to stay at part of box where drag began

So I'm trying to make a custom timeline and I have boxes that the user can place on the timeline and drag around. They can only be dragged from left to right. Now the problem is that when I drag them, instead of my mouse staying over the part where I started dragging, the beginning x of the rect jumps to my mouse. I would like the x of my rect to move the same amount of pixels as the mouse with my mouse staying in the part of the box that it originally starting dragging from. The boxes can be dragged from left to right. Now I have tried using

 if(Event.currrent.type==EventType.MouseDrag&&MyBoxRect.Contains(Event.current.mousePosition){
     MyBoxRect.x+=Event.current.delta.x;
 }

and

  MyBoxRect.x+=(Event.current.mousePosition.x-MyBoxRect.x); 

and

   MyBoxRect.x+=(Event.current.delta.x * 40);

None of these give the result I want. Delta.x results in my mouse moving pretty far from the box. Event.current.mousePosition.x-MyBoxRect.x results in the mouse always being at the start x of MyBoxRect. Now what would be the proper way to get my mouse to stay at the part of the box it started dragging at and to get the x to move only the amount of pixels that my mouse moved?

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
0

Answer by CoryButler · Jul 09, 2017 at 07:26 PM

@arisonu123

You need to get the distance between the mouse and box X. Then subtract the distance from the mouse X.

 var distance = Event.current.mousePosition.x - MyBoxRect.x;
 MyBoxRect.x = Event.current.mousePosition.x - distance;
Comment
Add comment · Show 2 · 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 Jwizard93 · Jul 09, 2017 at 11:21 PM 0
Share

Yes this will be better. deltaX is a filtered value which can be adjusted in the preferences.

Unity doesn't support a lot of $$anonymous$$ouse stuff it seems to me. This is the reason a lot of games hide the real mouse and implement a virtual mouse ins$$anonymous$$d.

avatar image arisonu123 · Jul 10, 2017 at 06:57 PM 0
Share

Hi Cory. I tried this but my box's do not move at all.

avatar image
0

Answer by Cornelis-de-Jager · Jul 10, 2017 at 02:25 AM

First Ensure that the Anchor of the Rect Transform is in the bottom left corner (or Top Left)

 /* Variables */
 Vector2 offset;
 bool moving;
     
 /* Unity Scripting API */
 void OnMouseDown () {
 Vector3 tran = GetComponent<RectTransform> ().position;
     
 offset = new Vector2 (Input.mousePosition.x - tran.x, Input.mousePosition.y - tran.y);
     
 moving = true;
 }
     
 Update () {
 // Button Released
 if (Input.GetKeyUp(KeyCode.Mouse0))
       moving = false;
     
 // Button Down:
 GetComponent<RectTransform> ().position = new Vector3 (Input.mousePosition.x + offset.x, Input.mousePosition.y + offset.y , 0);
 }
 
 
 
Comment
Add comment · Show 3 · 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 arisonu123 · Jul 10, 2017 at 07:01 PM 0
Share

Hi! This is the GUI system and not the UI system. I am making a editor tool so I am using OnGUI and GUI functions to draw everything. Now I adapted this code to work in OnGUI but it still doesn't work properly. The boxes are moving way ahead of my mouse and will only go to the right , they don't move to the left at all.

avatar image CoryButler arisonu123 · Jul 11, 2017 at 12:38 AM 0
Share

Have you tried subtracting offset.x ins$$anonymous$$d of adding it?

avatar image arisonu123 CoryButler · Jul 11, 2017 at 01:20 AM 0
Share

Yes and the boxes don't move at all.

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

119 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using EditorSceneManager to make custom level skip editor GUI doesn't work 0 Answers

Is it possible to store and display EditorGUILayout.Toggles? 0 Answers

Custom Editor GUI Elements 1 Answer

Default toggle style 1 Answer

Lining up GUI 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