- Home /
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");
}
}
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.
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.
@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?
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.
Your answer
Follow this Question
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