前言

我在做项目时遇到了一个要聚拢手牌,像三国杀里的手牌聚拢的效果
大概效果图:大概效果图

代码

public Dictionary<int, int> level_index ;//全部牌数据,卡牌id-卡牌阶级
//objects是需要洗的牌数
public void Xipai(GameObject[] objects)
{ float readonly position1=-2.28f;
float readonly position2=-3.5f;
float readonly allLength=4.9f;
//float readonly rotation=1f; //旋转间隔系数
int temp;
int counta = objects.Length;
foreach(GameObject t in kapai)
{
if(!objects.Contains(t)){
t.SetActive(false);
}
}
for(int i=0;i<counta;i++)
{
temp = i + 1;
if (!objects[i].activeSelf) objects[i].SetActive(true);
//距离聚拢
//4.9 -0.19
objects[i].transform.position = new Vector3(position2 + allLength / (counta + 1f) * (temp - 1f) + (temp - 1f) * 1f, position1, 0f);
//旋转成扇形
float t = (counta % 2f == 1f ? 1f : 0f);//奇偶判断器
float t1 = counta - t;//奇变偶
float t2 = t1 / 2;//取中间值
if (t == 1f && temp == t2 + 1)
{
objects[i].transform.localEulerAngles = new Vector3(0f, 0f, 0f);
}
else
{
objects[i].transform.localEulerAngles = new Vector3(0f, 0f, (temp <= t2 ? 90f : 0f) - 180f / (counta + 1f) * (temp - (temp > t2 ? t2 + t : 0f)) + (temp <= t2 ? -1f : 1f) * (level_index.Count - counta) * temp * rotation);
}
}
}