when a user taps a button I want the tap to be ignored if a transparent pixel is hit and the button beneath it should receive the tap instead.
I can do this sort of behaviour in objective c within a few minutes, however, trying to do it in Unity in C# has proven impossible as there is no way to convert a point from the screen to a local point. Obviously either the center or the bottom left, or the top left corner should be the origin point (I don't know which because Unity continuously changes where the origin is depending on the phase of the moon)
I've tried looking at the IsRaycastLocationValid from the ICanvasRaycastFilter and inside I've used both RectTransformUtility.ScreenPointToLocalPointInRectangle and RectTransformUtility.ScreenPointToWorldPointInRectangle and the result is always the same. The RectTransform I give is the one belonging to the button.
eg:
public bool IsRaycastLocationValid(Vector2 screenPosition, Camera raycastEventCamera) //uGUI callback
{
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPosition, null, out localPoint);
Vector2 pivot = rectTransform.pivot - new Vector2(0.5f, 0.5f);
Vector2 pivotScaled = Vector2.Scale(rectTransform.rect.size, pivot);
Vector2 realPoint = localPoint + pivotScaled;
Debug.Log(screenPosition + " " + localPoint);
return false;
}
Which I got from someone else's unanswered attempt here Unity 3d 4.6 Raycast ignore alpha on sprite
I've found this link http://forum.unity3d.com/threads/alpha-area-on-round-gui-button-return-click-event.13608/ where someone tries to determine if the mousePosition is within the image bounds. The obvious problem being that the image has to have some corner at (100,100). Magic numbers are bad and trying to figure out how to get those coordinates is next to impossible.
The following properties on RectTransform is always the same no matter where you place the button:
anchoredPosition
anchoredPosition3D
anchorMax
anchorMin
offsetMax
offsetMin
pivot
rect
sizeDelta
as you can see, that is all the properties a RectTransform has. Therefore it is impossible to tell where a button is, and is impossible to tell where a click is in the button's coordinate space.
Can someone please tell me how to do what I need to do?
Thats exactly why you dont find the location of the point inside of the button itself... but you compare the location of the point inside of the parent rect of the button and the buttons localPosition itself.
That or you can translate the pixel point to screen position then compare with mouse position