脚本有两个:
1.管理脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class SelectManager : MonoBehaviour {
public static SelectManager Instance;
private void Awake()
{
Instance = this;
}
List<Transform> cardList = new List<Transform>();
private float initYValue = -100;
private float outYValue = -70;
private bool isHold=false;
void Start () {
cardList = new List<Transform>();
isHold = false;
cardList.Clear();
}
void Update () {
if (Input.GetKeyDown(KeyCode.Mouse0))
{
isHold = true;
}
if(Input.GetKeyUp(KeyCode.Mouse0))
{
isHold = false;
MouseEnterCard();
cardList.Clear();
}
}
public void SmoothAddCards(Transform cardTrans) //滑入
{
if(isHold)
{
if (!cardList.Contains(cardTrans))
{
cardList.Add(cardTrans);
cardTrans.GetComponent<CardMove>().SetBlack(true);
}
else
{
cardList.Remove(cardTrans);
cardTrans.GetComponent<CardMove>().SetBlack(false);
}
}
}
public void ClickAddCards(Transform cardTrans) //点入(可重载,多态)
{
cardList.Add(cardTrans);
}
private void MouseEnterCard()
{
for (int i = 0; i < cardList.Count; i++)
{
float endValue = cardList[i].localPosition.y == initYValue ? outYValue : initYValue;
cardList[i].DOLocalMoveY(endValue, 0.3f);
cardList[i].GetComponent<CardMove>().SetBlack(false);
}
}
}
2.牌
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CardMove : MonoBehaviour ,IPointerEnterHandler,IPointerDownHandler{
Transform blackGo = null;
private void Start()
{
blackGo = transform.GetChild(1);
}
public void OnPointerDown(PointerEventData eventData)
{
SelectManager.Instance.ClickAddCards(this.transform);
}
public void OnPointerEnter(PointerEventData eventData)
{
SelectManager.Instance.SmoothAddCards(this.transform);
}
public void SetBlack(bool isblack)
{
blackGo.gameObject.SetActive(isblack);
}
}
版权声明:本文为kuilaurence原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。