问题描述
我正在制作简单的合并游戏。我将图像设置为“块”,将面板设置为“ DropZone”。它的工作原理是:当指针向下时,在拖动过程中“阻止”从Canvas中退出;当指针向上时,它成为面板的子级。当从一个面板到另一个面板时,它运作良好。但是,当我只单击图像时,图像从Canvas中移出,但没有进入原始面板。
我尝试使用IPointerClickHandler,但我不知道要使用它。我该怎么办?
这是Block的代码:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class Block : MonoBehavIoUr,IPointerDownHandler,IPointerUpHandler,IDragHandler
{
private RectTransform rectTrans;
private RectTransform parentRectTrans;
private Canvas rootCanvas;
private Vector2 offset;
void Awake()
{
rectTrans = this.GetComponent<RectTransform>();
parentRectTrans = this.rectTrans.parent as RectTransform;
this.rootCanvas = this.GetComponentInParent<Canvas>();
}
public void OnPointerDown(PointerEventData eventData)
{
this.transform.SetParent(rootCanvas.transform);
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
this.parentRectTrans,eventData.position,(this.rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay) ? null : this.rootCanvas.worldCamera,out offset))
{
this.offset.x = this.offset.x - this.transform.localPosition.x;
this.offset.y = this.offset.y - this.transform.localPosition.y;
}
}
public void OnPointerUp(PointerEventData eventData)
{
this.GetComponent<CanvasGroup>().blocksRaycasts = true;
}
public void OnDrag(PointerEventData eventData)
{
this.GetComponent<CanvasGroup>().blocksRaycasts = false;
Vector2 outLocalPos = Vector2.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
this.parentRectTrans,out outLocalPos))
{
this.transform.localPosition = outLocalPos - offset;
}
}
public void returnToParent()
{
//go back to its original parent
this.transform.SetParent(this.parentRectTrans);
}
}
这是DragZone的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class DropZone : MonoBehavIoUr,IDropHandler{
public void OnDrop(PointerEventData eventData)
{
Block d;
if (this.transform.childCount == 0)
{
eventData.pointerDrag.transform.SetParent(this.transform);
}
else
{
d = eventData.pointerDrag.gameObject.GetComponent<Block>();
d.returnToParent();
}
}
}
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)