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 Romano · Apr 25, 2014 at 07:51 PM · windowmouse drag

How can I trigger GetMouseButtonUp when the cursor leaves the game window? Or if the window is minimised, etc?

Hiya! I'm working on a game - not web based, will run fullscreen or in a window on the desktop - and I have what is essentially a click and drag functionality when you click and hold on the left mouse button. If the cursor leaves the window while dragging and then the mouse button is released out of the window, when the cursor is returned to the window the mouse still behaves as if the button is down i.e. still dragging while the mouse button is up.

To prevent this, is there a way to trigger the button release automatically when the cursor leaves the window? Or when the game window is minimised etc... Basically when the game loses focus, although I'm reluctant to say "loses focus" because I know that refers to something else to do with the GUI.

I feel like this must have been answered somewhere before, but I just haven't found the right keyword yet. I know how to write the code for the mouse part, if someone could point me in the direction of some code that lets you know when the cursor isn't on the window that would be amazing.

Thanks very much!

Romano

Comment
Add comment · Show 4
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 sixEight · Apr 25, 2014 at 08:31 PM 1
Share

Have you looked into $$anonymous$$athf.Clamp() at all? It may be just what you need to clamp the mouse click inside the screen range.

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html

avatar image Romano · Apr 25, 2014 at 09:03 PM 0
Share

Thanks for that :) I can't figure out how that would solve the problem exactly though? I'll edit the question title - it might be a wee bit misleading.

avatar image Romano · Apr 25, 2014 at 09:11 PM 0
Share

Actually, I'll edit the whole thing, it could be much clearer...

avatar image Romano · Apr 25, 2014 at 09:55 PM 0
Share

Totally get what you mean now sixEight :D

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Apr 25, 2014 at 09:11 PM

I don't have sure, but I think that convert mouse position to viewport point and check if it out of bounds (< 0 or > 1)

 var p = Camera.main.ScreenToViewportPoint(Input.mousePosition);
 if (p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) Debug.Log("Is OUT");
Comment
Add comment · Show 5 · 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 Romano · Apr 25, 2014 at 09:14 PM 0
Share

That's very interesting and might totally have answered my question if I wasn't such a dick and changed my question slightly while you were answering. Thanks very much though that could be the solution to at least part of my problem :)

avatar image Sisso · Apr 25, 2014 at 09:30 PM 1
Share

I have a little knowledge in desktop, but I think that you will need to track what events do you receive and try to solve it by yourself. Add some logs into OnApplication* events and Update to find a pattern that you can use.

If I undestand, you said correctly that game lose focus.

https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnApplicationFocus.html

avatar image Romano · Apr 25, 2014 at 09:39 PM 0
Share

I saw that in the documentation, but it does a pretty terrible job of explaining what its actually for. Do you know how OnApplicationFocus works?

avatar image Sisso · Apr 25, 2014 at 09:48 PM 1
Share

Sorry, no. I work most in mobile.

Add some debug and test many different actions with your app to see. I think that click with mouse outside of the window, alt tab or any other app window that come in front (grab the focus) should invoke this event.

$$anonymous$$ouse outside the window without click, I think that the game will continue to play and Update will called. In this case, could be verify with ScreenToViewportPoint.

avatar image Romano · Apr 25, 2014 at 09:56 PM 0
Share

Cheers for that, I'll have a look at it :)

avatar image
0

Answer by Romano · Apr 25, 2014 at 09:54 PM

Here's my current solution, which is actually working pretty decently for the window being minimised, but doesn't trigger anything when the mouse leaves the window yet.

Basically I have a boolean called

 leftClickAndHoldActive

which is set to true when

 Input.GetMouseButton(0)  // Left mouse button held down.

is true.

Then at the end of my code that detects different kinds of clicks (Single, double, leftclick and hold etc) I have this:

 else if (leftClickAndHoldActive == true && !(Input.GetMouseButton(0)))
     {
               print("Lost Focus");
     }
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 grendayzer77 · May 08, 2014 at 04:06 PM 0
Share

Hi, i have the same problem when i click on a button and release outside it, do you find any other solution ? 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

23 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

Related Questions

Making a glass/window shader with cubemap reflection and transparency options 2 Answers

Some questions about Gui.Window 1 Answer

Windows that do not take priority over other GUI elements 0 Answers

Buttons in window problem 2 Answers

Gui in Gui Not Working ? 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