0
我想使用Unity UI对象创建一个简单的拖放游戏。在UI上拖放
我的对象现在是带有脚本的图像。我的脚本使用OnBeginDrag
,OnDrag
和OnEndDrag
事件,当我想要移动图像对象时它可以正常工作。
但是图像对象总是正方形的,我只想在鼠标位于特定区域时才检测拖动。所以我在我的图像上创建了一个Polygon Collider 2D,并将其调整为我想要的形状。不幸的是,我无法让它工作。
public void OnBeginDrag(PointerEventData eventData)
{
var canvas = FindInParents<Canvas>(gameObject);
itemBeingDragged = gameObject;
m_DraggingPlane = canvas.transform as RectTransform;
}
public void OnDrag(PointerEventData eventData)
{
SetDraggedPosition(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
itemBeingDragged = null;
int i = 0;
foreach (Vector3 hex in otherHexes)
{
Vector3 rayBeginning = hex + transform.position;
rays = Physics2D.RaycastAll(rayBeginning, Vector2.zero);
if (rays.Length > 1) //if there are other elements in RayCast
{
foreach (RaycastHit2D ray in rays)
{
if (ray.transform.gameObject != gameObject && ray.transform.tag == "Hex") //if element is not self and is a HexGrid
{
SuccessfulDrag(ray, i);
}
else if (ray.transform.gameObject != gameObject && ray.transform.tag == "GetOut") //if element is not self and is another puzzle
{
FailedDrag();
break;
}
}
}
else if (rays.Length == 1 && rays[0].transform.gameObject == gameObject) //if there is only one element and it's self
{
FailedDrag();
break;
}
i++;
}
}
http://answers.unity3d.com/questions/423378/how-do-you-create-complex-collision-geometry-to-ma.html - 这可能有助于 – Clint
@Clint谢谢回答,但它不是我所需要的。我知道如何创建复杂的对撞机,因为我使用了Polygon Collider 2D来创建我想要的形状。我不知道的是如何应用我的拖放脚本,以便它使用那些碰撞器而不是精灵。当我尝试将我的脚本应用到没有图像的对象时,它根本不起作用。它不检测任何拖动事件。 –