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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Lombard · Apr 30, 2014 at 12:35 AM · buttondragdrop

how to active a button after drag?

Hi,

My desired flowchart is the following:

1- Press a GUIRepeatButton and drag its texture over the screen.

2- If you release the mouse when you are over another Button copy the texture on the second button.

My problem is a button only answers true if you press and release above him and not only when you release. Any suggestions? My code is the following:

 public bool dragging = false;
 public Texture2D dragged;

 void onGUI(){
 
 if (GUI.RepeatButton (new Rect (Screen.width/8*5 - Screen.width/32,Screen.height/37*32,Screen.width/8,Screen.height/37*9), "", buttonCircle ))
 {
             dragging = true;
             dragged = circle;
 }
 
 if (dragging) 
 {
     if (Input.GetMouseButtonUp(0))
         {
         dragging = false;
          }else
          {
         GUI.DrawTexture(new Rect(Input.mousePosition.x - Screen.width/8/2,Screen.height - Input.mousePosition.y - Screen.height/37*9/2,Screen.width/8,Screen.height/37*9), dragged, ScaleMode.StretchToFill , true, 0);
 
     }
 }
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
Best Answer

Answer by robertbu · Apr 30, 2014 at 12:48 AM

First 'onGUI' should be 'OnGUI', so this code would not be executed. Second, you don't want to use the Input class here. Use the Event class. Something like:

 void OnGUI() {
     Event e = Event.current;
 
     if (e.type == EventType.MouseUp) {
        Debug.Log("Mouse button lifted");
     }
 }

My problem is a button is only answer true if you press and realesed over him and not only realesed

If this is your specific question, a solution I've used is to test the position of the mouse when the button is released. If it is within the confines of the button, then trigger it. You don't even need to use a button if the only way to trigger it is drag and drop. You can just use a DrawTexture(). So you put the Rect into the variable:

 Rect rectTarget = new Rect(0,0,100,50);

You can then test the rect when the mouse button comes up:

 if (rectTarget.Contains(e.mousePosition)) {
     // Do whatever.
 }
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
avatar image
0

Answer by Lombard · May 01, 2014 at 02:18 PM

It's a cool idea, and it works perfectly. Btw, I still have some questions Why using events? I'm still using input and it works properly... Is there any difference? Another problem i found is the mouse have the origin at the bottom of the screen so my code finally its like that:

if (Input.GetMouseButtonUp(0)){ if (positions[0].Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) { //Do things } dragging = false; }else{ //Keep Dragging

}

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

20 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

Related Questions

UI drag and drop 1 Answer

Drag and drop an external file/folder into a running game window? 2 Answers

Drag item on screen once spawned. 0 Answers

Simplest way to drag and object around in 2D space. 1 Answer

NGUI widget sprite background no longer moves with its button 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