【发布时间】:2015-08-25 02:03:07
【问题描述】:
当用户点击按钮时,如果点击了透明像素,我希望忽略点击,而它下方的按钮应该接收点击。
我可以在几分钟内在目标 c 中执行这种行为,但是,尝试在 C# 中的 Unity 中执行此操作已被证明是不可能的,因为无法将屏幕上的点转换为本地点。显然中心或左下角,或者左上角应该是原点(我不知道是哪个,因为Unity会根据月相不断改变原点的位置)
我已尝试查看 ICanvasRaycastFilter 中的 IsRaycastLocationValid 在里面我使用了 RectTransformUtility.ScreenPointToLocalPointInRectangle 和 RectTransformUtility.ScreenPointToWorldPointInRectangle ,结果总是一样的。我给的 RectTransform 是属于按钮的那个。
例如:
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;
}
这是我从其他人的未答复尝试中得到的 Unity 3d 4.6 Raycast ignore alpha on sprite
我找到了这个链接 http://forum.unity3d.com/threads/alpha-area-on-round-gui-button-return-click-event.13608/ 有人试图确定 mousePosition 是否在图像范围内。明显的问题是图像必须在 (100,100) 处有某个角。幻数很糟糕,试图弄清楚如何获得这些坐标几乎是不可能的。
无论您将按钮放在何处,RectTransform 上的以下属性始终相同:
anchoredPosition
anchoredPosition3D
anchorMax
anchorMin
offsetMax
offsetMin
pivot
rect
sizeDelta
如您所见,这就是 RectTransform 具有的所有属性。因此无法判断按钮在哪里,也无法判断点击在按钮的坐标空间中的位置。
有人可以告诉我该怎么做吗?
【问题讨论】:
-
请使用 unity3d 标签。
标签: c# user-interface button unity3d