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 Komak57 · Jun 27, 2014 at 02:41 AM · guimousehotcontrol

GUI Mouse lock improvements

I've been researching how to prevent key clicks to world clicks when the user intends to click a GUI. My movement works on mouse move, and mouse release, so usual methods don't seem to work appropriately. Using the GUIUtility.hotControl works by default when you click an interactive element like an input box, or button control. This doesn't seem to work for non-interactive elements that you wish to intercept. I've gotten around this a little bit with

 if (window.Contains(Input.mousePosition)) {
     Debug.Log("<System>: <color=white>Mouse over Debugger</color>");
     GUIUtility.hotControl = 1;
 }


This forces hotControl in scenarios I wish to manage. That being said, I have some complication when window is a handle for a GUI.Window interface. From various debugging, I've determined that window contains the mouse in a virtual rect aligning to the top-left.

Question here, is how do I need to modify my code snippet to adjust for window positions?

[EDIT] window.Contains() does not register x and y rect positions. Modifying the mouse positions via rect X/Y doesn't seem to be accurate enough. Few pixels lower than the window. Still looking for solutions.

Comment
Add comment · Show 1
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 Komak57 · Jun 27, 2014 at 03:32 AM 0
Share
         if (Adjust.FitToScreen(window).Contains(new Vector3(Input.mousePosition.x+Adjust.FitToScreen(window).x, Input.mousePosition.y+Adjust.FitToScreen(window).y, Input.mousePosition.z))) {
             Debug.Log("<System>: <color=white>$$anonymous$$ouse over Debugger</color>");
             //Debug.Log("<System>: <color=yellow>Window: "+window.x+","+window.y+":"+window.x$$anonymous$$in+","+window.y$$anonymous$$in+"</color>");
             GUIUtility.hotControl = windowID;
         } else {
             if (GUIUtility.hotControl == windowID)
                 GUIUtility.hotControl = 0; // release
         }

Is better, but window contains is registerring window.x$$anonymous$$in left, and window.y$$anonymous$$in down from where it should be. If I don't adjust the position, window constains registers at 0, 0.

1 Reply

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

Answer by Komak57 · Jun 28, 2014 at 05:32 AM

First complication, the mouse Y coordinate needs to be inverted by the screen height. Next up, was having trouble with input controls using hotControl, so I wrote a little flag script to maintain multiple GUI's.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MouseControl : MonoBehaviour {
     List<Window> list = new List<Window>();
     public bool Locked {
         get {
             bool locked = false;
             foreach(Window w in list) {
                 if (w.Locked)
                     locked = true;
             }
             return locked;
         }
     }
     public void Lock(int WindowID) {
         Window w = new Window(WindowID, true);
         int i = -1;
         if ((i = Contains(w)) != -1) {
             if (!list[i].Locked)
                 Debug.Log("<System>: <color=white>Window["+WindowID+"] Locked</color>");
             list[i].Locked = true;
         } else {
             list.Add(w);
             Debug.Log("<System>: <color=white>Window["+WindowID+"] Locked</color>");
         }
     }
     public void Unlock(int WindowID) {
         Window w = new Window(WindowID, false);
         int i = -1;
         if ((i = Contains(w)) != -1) {
             if (list[i].Locked)
                 Debug.Log("<System>: <color=white>Window["+WindowID+"] Unlocked</color>");
             list[i].Locked = false;
         } else {
             list.Add(w);
             Debug.Log("<System>: <color=white>Window["+WindowID+"] Unlocked</color>");
         }
         //Debug.Log("<System>: <color=white>Window["+WindowID+"] Unlocked</color>");
     }
     public int Contains(Window a) {
         for(int i = 0; i < list.Count; i++) {
             if (a.ID == list[i].ID)
                 return i;
         }
         return -1;
     }
 }
 
 public class Window {
     public int ID;
     public bool Locked;
     public Window(int ID, bool Locked) {
         this.ID = ID;
         this.Locked = Locked;
     }
 }

Usage:

         if (Adjust.FitToScreen(window).Contains(new Vector3(Input.mousePosition.x, Screen.height-Input.mousePosition.y, 0))) {
             //Debug.Log("<System>: <color=white>Mouse over Debugger</color>");
             //Debug.Log("<System>: <color=yellow>Window: "+window.x+","+window.y+":"+window.xMin+","+window.yMin+"</color>");
             GameObject.Find("System").GetComponent<MouseControl>().Lock(windowID);
         } else {
             GameObject.Find("System").GetComponent<MouseControl>().Unlock(windowID);
         }

My Adjust.FitToScreen is a small formula to scale GUI elements based on a preferred size. That just lets me program everything for one resolution and scale for others.

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

21 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

Related Questions

If mouse is hovering over any GUI element 0 Answers

detect mouseover with grid buttons? 1 Answer

How do i create an object with a GUI Button 1 Answer

Ensuring Correct Call Order 0 Answers

Change GUI on mouse over 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