给 Card 添加 CardDragHandler
代码如下
using UnityEngine;
using UnityEngine.EventSystems;
public class CardDragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private Card currentCard;
private bool canMove;
private bool canExecute;
private void Awake()
{
currentCard = GetComponent<Card>();
}
public void OnBeginDrag(PointerEventData eventData)
{
switch (currentCard.cardData.cardType)
{
case CardType.Attack:
break;
case CardType.Defense:
case CardType.Abilities:
canMove = true;
break;
}
}
public void OnDrag(PointerEventData eventData)
{
if (canMove)
{
currentCard.isAnimating = true;
Vector3 screenPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
currentCard.transform.position = worldPos;
canExecute = worldPos.y > 1f;
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (canExecute)
{
}
else
{
currentCard.ResetCardTransform();
currentCard.isAnimating = false;
}
}
}
目前实现的效果是,当正在拖拽的卡牌是Defense
或者Abilities
时,就允许它进行拖拽
拖拽的时候,会将鼠标当前的位置转换为世界坐标的位置,然后让卡牌来到这个位置
当停止拖拽的时候,会让卡牌来到最初的位置
如果卡牌拖动到 y 轴为 1 及以上位置的时候,就执行这张牌
项目相关代码
代码仓库:https://gitee.com/nbda1121440/DreamOfTheKingdom.git
标签:19,void,卡牌,currentCard,拖拽,private,public From: https://www.cnblogs.com/hellozjf/p/18054197