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 igotthis · Feb 23, 2014 at 09:34 AM · c#guimouserectcontains

Need help understanding GUI and Rect.Contains interaction

So basically I have been trying to write an inventory system like in Diablo, but during that project I realized that there's some really funky interaction between rect.contains, input.mouseposition and the GUI. To test it out here's a script I was messing with. The code prints true when i hover into the rect one time, but then when i clear the print message by pressing down on left mouse button and hover over the rect again it doesn't print anything. Any help would be much appreciated!!!

 using UnityEngine;
 using System.Collections;
 
 public class guiBOX : MonoBehaviour {
     public int counter;
     // Use this for initialization
     void Start () {
         counter = 0;
     }
 
     void OnGUI() {
         Rect pass = new Rect (0, 0, Screen.width / 2, Screen.height / 2);
         GUI.Box(pass, "This is a title");
         Vector2 mouse = Input.mousePosition;
         mouse.y = Screen.height - mouse.y;
         if (pass.Contains (mouse))
                         print("It's true!");
                 else 
                         print("false");
         //Debug.Log (counter);
         //counter++;
     }
     // Update is called once per frame
     void Update () {
     if (Input.GetMouseButtonDown (0))
                         print ("RANDOM");
     }
 }
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 robertbu · Feb 23, 2014 at 09:46 AM 0
Share

I could not reproduce your problem. Your code worked just fine both before and after I clicked. Any chance you have 'collapse' set in the Console window? Any chance you clicked outside of Unity and Unity lost focus. Anyway, using Input.mousePosition inside OnGUI() is not optimal. It is best to use the information from the current GUI Event. Something like:

 void OnGUI() {
     Event e = Event.current;
     Rect pass = new Rect (0, 0, Screen.width / 2, Screen.height / 2);
     GUI.Box(pass, "This is a title");
     Vector2 mouse = e.mousePosition;
     if (pass.Contains (mouse))
         print("It's true!");
     else 
         print("false");
 }

Event.mousePosition are in GUI coordinates and don't need to be translated.

avatar image vexe · Feb 23, 2014 at 09:46 AM 0
Share

Not that it's related, but here's an advice: if you're going to make an inventory, use anything but the Unity GUI (NGUI, eGUI, etc) - you'll face tons of trouble, headaches, performance bottlenecks etc and you'd end up solving problems in very obscured ways.

About your question, I just took your script and tried it out - I'm not sure I was able to reproduce what you said. I cleared out the console and it still worked as expected.

avatar image igotthis · Feb 23, 2014 at 09:57 AM 0
Share

@vexe in that case would you recommend I just use 2D shapes and simulate a GUI????

p.s. ALso, what would you recommend using GUI for? HUD display? Or something else?

avatar image vexe · Feb 23, 2014 at 10:04 AM 0
Share

I've always used NGUI - awesome GUI - (the upco$$anonymous$$g UnityGUI will be based on it) - awesome developer, awesome support, forums and documentation. I made inventories with both, here's the Unity GUI's, and here's NGUI's. Judge for yourself.

0 Replies

· Add your reply
  • Sort: 

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

How to fire gui button on mouse right release 2 Answers

strange Rect behavior on Y axis 1 Answer

How do I make rect.Contains() accept a rect with negative width and height? 2 Answers

How do I get (return) all the GUI inside(contained in) a GUI rect ? 2 Answers

How expensive is Rect.Contains(Event.current.mousePosition)? 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