- Home /
How to create GUITexture which does not react on over on transparent parts of it.
I have a GUITexture and it have transparent parts. If I use mouse over function it also reacts as over on those transparent parts. My question is, how to make GUITexture not to react on those transparent parts, so mouse over function is not called, when pixel under it have transparent color. Thanks for answers.
@Lukas, if you get a helpful answer, please be sure to upvote it (and checkmark the best answer, if any).
Answer by Peter G · Apr 28, 2010 at 10:29 PM
There are a couple ways. I would do the second personally.
Create multiple rects that encompass the GUITexture's opaque part and then, rather than check if the mouse clicked in the GUITexture's rect, check to see if any of these rects contain the point.
When the user clicks the object, get the alpha value of the pixel they hit and if it is greater than one, treat the click as a hit, otherwise a miss. Here's a basic example.
var guiRect : Rect; //only used if using GUI 2.0. var useOnGUI : boolean = false;
private var texRect : Rect; var tex : Texture2D; //used for 2.0. Auto-assigned otherwise.
private var imageSheerX : float; private var imageSheerY : float;
private var pixelCoord : Vector2; private var guiPos : Vector2;
function Start () {
if(useOnGUI) {
guiPos = Vector2(guiRect.x, guiRect.y - guiRect.height);
imageSheerX = guiRect.width / tex.width; //these two lines are important.
imageSheerY = guiRect.height / tex.height; //they scale the screen position to match the same pixel on
}
else {
texRect = guiTexture.pixelInset; //cache a link to the guiTexture and the coordinates.
guiPos = Vector2(texRect.x, texRect.y);
tex = guiTexture.texture as Texture2D; //cache a link to the texture;
imageSheerX = texRect.width / tex.width; //these two lines are important.
imageSheerY = texRect.height / tex.height; //they scale the screen position to match the same pixel on
// on the texure.
}
}
function OnMouseDown () { //account for shifts in the transform.
var offset : Vector2 = Input.mousePosition - guiPos; //Find the mouse position
//relative to the corner of the GUI.
print (offset);
pixelCoord = Vector2(offset.x / imageSheerX, offset.y / imageSheerY);
//Scale the coordinates to match the Texture.
if(tex.GetPixel(pixelCoord.x, pixelCoord.y).a > 0.1) {
// check the alpha of the pixel. Only run code if alpha > .1
//Place your normal button commands in here.
// anything else will automatically run no matter the transparency.
print("Area is opaque");
}
}
function OnGUI () { //If you are using 2.0 GUI.
if(useOnGUI) {
var offset : Vector2 = Input.mousePosition - guiPos; //Find the mouse position
//relative to the corner of the GUI.
pixelCoord = Vector2(offset.x / imageSheerX, offset.y / imageSheerY);
//Scale the coordinates to match the Texture.
if(GUI.Button(guiRect, tex)) {
if(tex.GetPixel(pixelCoord.x, pixelCoord.y).a > 0) {
print("I was clicked");
}
}
}
}
Thanks that really helps. And could be done sth. like this also with 2.0 GUI? Could I just get texture and access it as 2D field using GetPixel function? And last question, is there somewhere documentation in which I could find also functions like this, because I was searching all over Unity documentation and did not found it. Thanks for answer.
There are two errors in you script.
$$anonymous$$ouse positioning if down left oriented, texture positioning (pixelInset) is top left oriented, so Input.mousePosition - guiPos need to be changed. for example like this:
var offset : Vector2 = Vector2(Input.mousePosition.x - guiPos.x,Input.mousePosition.y - (Screen.height + guiPos.y));Your script does not take into count that texture could be positioned using transform.
Two comments. 1. GUITextures calculate from the bottom left, but 2.0 calculates the position from the top left. Now if you use 2.0 GUI then you will have to correct that, but with a GUItexture, you should be fine. 2. It did not occur to me that the tex. might be moved by the transform. So thank you for pointing that out. I'll correct it soon if I can, but for an easy workaround set the tranform position to (0, 0, 0) then move the object around using only the pixel inset. It's not perfect, but it will work until it is fixed.
These are all classes listed in the script reference. Search Rect, Vector2, Input, and Texture2D and Unity will bring up the class reference, a guide to the class and its functions.
I add 2.0 functionality to the script. Worked on adding position in as a calculation, but got some weird results. I'll try some more, but it's just as easy to use pixelInset and position the gui at (0, 0, 0).
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
How to prevent guitexture from vertically shrinking? 0 Answers
Simple GUI In game 1 Answer
[CLOSED] Pixel Inset ---> transform scale convert 0 Answers
Door Problem 1 Answer