我正在用unity3d做游戏,我需要将文本组件拖放到其他文本组件上。
我有一个带有画布组的画布。内部有两个带有 Grid Layout Group 的面板。我正在以编程方式加载由Panel和Text组成的组件(预制件),然后将其添加到他的父对象中。我有2个脚本,分别是 ItemDragHandler 和 TextDropHandler ,在将这两个处理程序添加到预制中后,我得到ItemDropHandler从未执行过 OnDrop 功能。我之前使用过这两个脚本,它们已经起作用。我已经以编程方式禁用了画布组中的射线广播。
public class TextDropHandler: MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
Debug.Log("Drop detectado"); //never shown in the Unity Debug
Texto txtDrag = ItemDragHandler.startParent.gameObject.GetComponent<SlotText>().texto;
Texto txtDrop = transform.gameObject.GetComponentInParent<SlotText>().texto;
DropTextTextContainer dttc = GetComponentInParent<DropTextTextContainer>();
if (dttc.verify(txtDrag, txtDrop) && !dttc.showNextText())
{
//Destroy(transform.parent.gameObject);
ItemDragHandler.destroyObject = true;
}
}
}
public class ItemDragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public static GameObject itemBeingDragged;
public static Transform startParent;
public static bool destroyObject;
Vector3 startPosition;
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("Drag ha comenzado");
itemBeingDragged = gameObject;
startPosition = transform.position;
startParent = transform.parent;
GetComponentInParent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Drag finalizado");
if (destroyObject)
{
Destroy(startParent.gameObject);
destroyObject = false;
}
itemBeingDragged = null;
startParent = null;
if(transform.parent != startParent) {
transform.position = startPosition;
}
GetComponentInParent<CanvasGroup>().blocksRaycasts = true;
}
}
我希望在将一个对象拖到另一个对象时执行drop函数。