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.

There are a couple ways. I would do the second personally.

  1. 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.

  2. 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");
            }

        }
    }
}