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 Lex · Mar 09, 2010 at 01:17 PM · guimousemouseclickgui-buttonmouse-drag

Can I change GUIButton behaviour?

Is there a way to change a GUIButton behaviour? I need it to be activated/pressed if I click outside the button, drag it in its area and then release like any other buttons you find out there.

The main reason for this is because we're using an ELO touchscreen monitor. Changing the mouse emulation mode to click on release works fine with GUIButtons, except that it disables any drag. And dragging stuff is something we need for this game.

Edit: I just realized that it behaves different when running withing Unity and compiled as a Windows standalone. Within Unity the touch with mouse emulation works wonderful, no problems at all.

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

1 Reply

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

Answer by Lex · Mar 11, 2010 at 01:42 PM

Yes I could.

The best way to doing so is to save the rectangles used for drawing the buttons on GUI, and using them on Update checking if mouse is colliding. Note that the mouse coordinates are inverse in Y axes compared with the GUI coordinates so here's the C# sample code:

private Rect myButtonRect;

void Start() { myButtonRect= new Rect(0, 0, 200, 100); }

void Update() { float fMouseX = Input.mousePosition.x; float fMouseY = Screen.height - Input.mousePosition.y; // Inverted Y

 if (ButtonRect.Contains(new Vector2(fMouseX, fMouseY)))
 {
     // Then all you have to do is code your button behaviour for each mouse action

     if (Input.GetMouseButton(0))
     {
         // Your code for mouse left click
     }

     if (Input.GetMouseUp(0))
     {
         // Your code for left button released
     }

     // And so on...
 }

}

void OnGUI() { GUI.Button(myButtonRect, "Whatever"); }

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 NikEy · Oct 28, 2012 at 09:54 PM 0
Share

this is genius, thanks.

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

1 Person is following this question.

avatar image

Related Questions

Disable Mouse Input when OnGUI() 1 Answer

Why is OnDrag triggering but not OnMouseDown? 0 Answers

Buttons that remove themselves when clicked mess up other gui elements 2 Answers

Change button texture when its clicked 1 Answer

Why can't i click a button when in first person? 2 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