最近开始跟着老师学做炉石.在手牌这块老师以长方形呈现了手牌列表,不过对于伪完美主义的我来说终归是有些缺陷的.所以抱着自己动手实践的想法做了一个,
实践的过程中也遇到了几个新问题,网上并没有直接答案,所以也算是学有所得.
贴出结果,如果哪里有缺陷还希望大家指教.
以下效果图
只有一张手牌时
增加到六张时
减少到四张
手牌的预设.这里涉及了pivot的设置.x=0.5,y=0.5意味着旋转以图片中心为圆心,x=0,y=0意味着以图片左下角为圆心.以此类推
以下代码
`using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections.Generic;
public class Hero1HandCards : MonoBehaviour {
public GameObject GOHandCardPrefab; //手牌预设
private List ListHandCard = new List();//手牌列表
public GameObject TransBeginHandCard; //生成手牌的最开始的位置
private float _FloRotateAngel; //手牌动画旋转的角度
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.LeftShift))
{
GetCards();
RotateAngel();
AddCardAnimations();
}
else if (Input.GetKeyDown(KeyCode.Backspace))
{
UseCardAnimation();
}
}
public void GetCards()
{
//克隆预设
GameObject GOHandCard = GameObject.Instantiate(GOHandCardPrefab)as GameObject;
GOHandCard.transform.position = TransBeginHandCard.transform.position;
GOHandCard.transform.parent = this.transform;
//将新手牌添加到手牌列表
ListHandCard.Add(GOHandCard);
//计算动画需要旋转的角度
RotateAngel();
}
//为手牌添加动画
private void HandCardAnimation(GameObject GO,float Vec3_Z)
{
GO.transform.DORotate(new Vector3(0, 0, Vec3_Z), 0.3F, RotateMode.Fast);
}
//增加手牌时播放的动画
private void AddCardAnimations()
{
if (ListHandCard.Count == 1)
{
HandCardAnimation(ListHandCard[0], 0);
}
else
{
for (int i = 1; i < ListHandCard.Count; i++)
{
HandCardAnimation(ListHandCard[i - 1], 30 - _FloRotateAngel * (float)i * ListHandCard.Count + 2.5F);
}
HandCardAnimation(ListHandCard[ListHandCard.Count - 1], -27.5F + _FloRotateAngel);
}
}
//使用手牌时播放的动画
private void UseCardAnimation()
{
Destroy(ListHandCard[0]);
ListHandCard.Remove(ListHandCard[0]);
RotateAngel();
if (ListHandCard.Count == 1)
{
HandCardAnimation(ListHandCard[0], 0);
}
else if(ListHandCard.Count>1)
{
for (int i = 1; i < ListHandCard.Count+1; i++)
{
HandCardAnimation(ListHandCard[i-1], 30 - _FloRotateAngel * (float)i * ListHandCard.Count + 2.5F);
HandCardAnimation(ListHandCard[ListHandCard.Count - 1], -27.5F + _FloRotateAngel);
}
}
}
//计算需要旋转的角度
private void RotateAngel()
{
_FloRotateAngel = 55F / (float)ListHandCard.Count / (float)ListHandCard.Count;
}
}
`