Unity3D之扑克牌选牌操作
作者:互联网
一 , 前景
玩家点击或者拖拽都可以对牌进行选择和取消选择. 和很多"斗地主"游戏操作一样
二, scene
a, Cards上注册点击按钮,点击抬起,拖拽事件
b, 在Card_1 到Card_6上处理牌被选中和取消选中的表现
三 . Cards 上的代码 Cards.cs
using System.Globalization;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 卡牌测试
/// </summary>
public class Cards : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
/**当前位置的卡牌*/
private GameObject curCard;
[SerializeField]
private RectTransform rectTransform;
private Dictionary<GameObject, CardAni> mapCards;
/// <summary>
/// 选中的卡牌
/// </summary>
private List<GameObject> selectedCards;
#region 关于动画参数
[SerializeField]
private float offY;
[SerializeField]
private AnimationCurve animationCurve;
[SerializeField]
private float duration;
#endregion
private void Awake()
{
this.mapCards = new Dictionary<GameObject, CardAni>();
this.selectedCards = new List<GameObject>();
}
private void Start()
{
Transform cell;
CardAni js;
for (int i = 0; i < this.transform.childCount; i++)
{
cell = this.transform.GetChild(i);
js = cell.GetComponent<CardAni>();
js.Init(offY, animationCurve, duration);//初始化参数
this.mapCards.Add(cell.gameObject, js);
}
}
public void OnDrag(PointerEventData eventData)
{
GameObject target = eventData.pointerCurrentRaycast.gameObject;
if (target.transform == this.transform)
{
this.curCard = null;
return;
}
if (this.curCard == target)
{
return;
}
CardAni js = target.GetComponent<CardAni>();
if (js == null)
{
return;
}
this.curCard = target;
int index = this.selectedCards.IndexOf(target);
if (index < 0)
{
this.selectedCards.Add(target);
js.PlayState(true);
}
else
{
this.selectedCards.RemoveAt(index);
js.PlayState(false);
}
}
public void OnPointerDown(PointerEventData eventData)
{
RaycastResult result = eventData.pointerPre***aycast;
if (result.gameObject == this.transform.gameObject)
{
return;
}
CardAni js = result.gameObject.GetComponent<CardAni>();
if (js != null)
{
this.curCard = result.gameObject;
int index = this.selectedCards.IndexOf(this.curCard);
if (index >= 0)
{
this.selectedCards.RemoveAt(index);
js.PlayState(false);//播放取消选中动画
}
else
{
this.selectedCards.Add(this.curCard);
js.PlayState(true);//播放选中动画
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
this.curCard = null;
}
private void OnDestroy()
{
this.mapCards.Clear();
this.mapCards = null;
this.selectedCards.Clear();
this.selectedCards = null;
}
}
四, CardAni.cs 代码 :
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardAni : MonoBehaviour
{
private float offY;
private AnimationCurve animationCurve;
private float duration;
private float originalY;
private bool isAniPlay;
private float targetY;
private float selectedY;
private float curDuration;
private float curveX;
private Vector3 startVe3;
private Vector3 endVe3;
public void Init(float offY, AnimationCurve animationCurve, float duration)
{
this.offY = offY;
this.animationCurve = animationCurve;
this.duration = duration;
this.originalY = (this.transform as RectTransform).anchoredPosition.y;
this.selectedY = this.originalY + this.offY;
}
/// <summary>
/// 播放选中/放弃Card动画
/// </summary>
/// <param name="selected">是否选中</param>
public void PlayState(bool selected)
{
this.isAniPlay = true;
this.curveX = 0;
this.SetTargetY(selected);
this.curDuration = Mathf.Abs((this.transform as RectTransform).anchoredPosition.y - targetY) / Mathf.Abs(this.offY) * this.duration;//处理移动时间
this.startVe3 = (this.transform as RectTransform).anchoredPosition;
this.endVe3 = new Vector3(this.startVe3.x, targetY, this.startVe3.z);
}
/// <summary>
/// 设置目标Y
/// </summary>
/// <param name="selected"></param>
private void SetTargetY(bool selected)
{
if (selected)
{
targetY = this.selectedY;
}
else
{
targetY = this.originalY;
}
}
/// <summary>
/// 定位状态
/// </summary>
/// <param name="selected">是否被选中</param>
public void LocationState(bool selected)
{
this.isAniPlay = false;
this.SetTargetY(selected);
this.startVe3 = (this.transform as RectTransform).anchoredPosition;
this.endVe3 = new Vector3(this.startVe3.x, targetY, this.startVe3.z);
(this.transform as RectTransform).anchoredPosition = this.endVe3;//直接赋值坐标
}
private void Update()
{
if (!this.isAniPlay) return;
this.curveX += Time.deltaTime / this.curDuration;
(this.transform as RectTransform).anchoredPosition = Vector3.Lerp(this.startVe3, this.endVe3, this.animationCurve.Evaluate(this.curveX));
if (this.curveX >= 1)
{
this.isAniPlay = false;
}
}
private void OnDestroy()
{
this.animationCurve = null;
}
}
标签:Unity3D,扑克牌,void,float,transform,private,js,选牌,using 来源: https://blog.51cto.com/aonaufly/3036675