- Home /
OnMouse functions not working properly on GUITexture when changing viewport rect
When I change Normalized View Port Rect width or height of my main camera, the active area of mouse event functions like OnMouseEnter and OnMouseExit on GUITexture objects is not correctly placed over the image.
For example, if I reduce the width of the Normalized View Port Rect, the active area of my GUITexture is shifted to the left.
I do not want to use GUI scripts even if I know how to use them.
Example
Description
In the example Unity file from the link below, when I point the mouse cursor above the image, it should normally have its alpha color changed. But in this case, with a modified Normalized View Port Rect (width = 0.5), this only happens when I point at the left of the image.
Link
Script
#pragma strict
function OnMouseEnter() {guiTexture.color.a = 0.1;}
function OnMouseExit() {guiTexture.color.a = 1.0;}
Question
Is it a bug ? How can I workaround ?
Update log
[Update] Added on GUITexture objects in the first paragraph.
[Update 2] Added link to an example export.
[Update 3] Added script used + formatting the question.
Answer by aldonaletto · Apr 02, 2012 at 08:33 PM
Yes, this looks like a big fat bug! I tested this, and the "sensible" area moved down and left - even when the viewport was set to the upper right corner.
The 3 bastard sons of Unity are TextMesh, GUIText and GUITexture. Many things with them are weird: TextMesh Z axis points backwards, its shader renders over everything in the 3D world, GUIText and GUITexture use viewport coordinates in the position field, ignore rotation and have an undecipherable interpretation of scale, GUIText doesn't have a color property... there are many weird things, and this bug comes to make things worse.
If the sensible area of your texture may be simplified to a rectangle, you can define some sensible rectangles and use Rect.Contains(Input.mousePosition) to check when the mouse entered them:
var r1:Rect = Rect(10,10,200,100); var r2:Rect = Rect(300,10,200,100);
function Update(){ if (r1.Contains(Input.mousePosition)){ // mouse over r1 } else { // mouse out of r1 } if (r2.Contains(Input.mousePosition)){ // mouse over r2 } else { // mouse out of r2 } } The alternative is the new GUI system, which you seem to hate (I don't like it too...)
Thanks for your reply!
The new GUI system is not that much bad, but despite its advantages, it takes more steps and time to design GUI elements than using the GUITexture / GUIText.
Yes, and you can't see what you're doing without running the project. But the worst thing is when we forget to which script that damned thing was added...
Why your answer was not accepted before is a mystery for me. Now it is fixed. ^^
Your answer
Follow this Question
Related Questions
Detecting a mouse hover using an alpha channel not a rect 0 Answers
Blur script changes Viewport rect ? 0 Answers
Changing the Camera viewport rect results in resizing the objects rendered by the specific camera ? 1 Answer
Having Issues with Camera Viewport Rect 0 Answers
Rect, Texture, GUI, Detection. 1 Answer