2D轮转图选角色换衣服

 //轮转图效果↑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

public class RotationChart2D : MonoBehaviour, IEndDragHandler, IDragHandler
{
string imageName;
public int n = 10;//个数
public Image prefab;//预制体
public float max = 1;//最大值
public float min = 0.5f;//最小值

public float spacing = 100;//UI之间单位是像素
public float cut = 10;//减速度
public ChangeOfOutfit player;
public CastingPanel casting;
float c;//周长
float r;//半径
float ang;//弧度
List<Image> list = new List<Image>();
List<Transform> sortList = new List<Transform>();
public GameObject rotationChart2D;
float moveAng = 0;//拖动的弧度
void Start()
{
rotationChart2D = GameObject.Find("RotationChart2D").gameObject;
casting= GameObject.Find("CastingPanel").GetComponent<CastingPanel>();
imageName = "Image/ima_";
player = GameObject.Find("Player").GetComponent<ChangeOfOutfit>();
Refresh(imageName,12);
}
public void Refresh(string name,int num)
{
n = num;
if (list != null)
{
list.Clear();
}
if (sortList != null)
{
sortList.Clear();
}
if (transform.childCount>0)
{
for (int i = transform.childCount-1; i >= 0; i--)
{
Destroy(transform.GetChild(i).gameObject);
}
}
imageName = name;
//周长=(宽+间距)*个数
c = (prefab.rectTransform.rect.width + spacing) * n;
//半径= 周长/2π
r = c / (2 * Mathf.PI);
ang = 2 * Mathf.PI / n;
Move();
}

public void Move()
{

for (int i = 0; i < n; i++)
{
//判断集合中有就不创建,没有在创建
if (list.Count <= i)
{
list.Add(Instantiate(prefab, transform));
list[i].transform.GetChild(0). GetComponent<Image>().sprite = Resources.Load<Sprite>(imageName + i);
sortList.Add(list[i].transform);
}
float x = Mathf.Sin(i * ang + moveAng) * r;
float z = Mathf.Cos(i * ang + moveAng) * r;
//通过z计算近大远小
//计算z
float p = (z + r) / (r + r);
//线性差值公式
float scale = (max - min) * p + min;
list[i].rectTransform.anchoredPosition = new Vector2(x, 0);
list[i].transform.localScale = Vector3.one * scale;
}
sortList.Sort((a, b) =>//排序
{

if (a.localScale.x < b.localScale.x)
{
return -1;
}
else if (a.localScale.x == b.localScale.x)
{
return 0;
}
else
{
return 1;
}

});
for (int i = 0; i < sortList.Count; i++)
{
sortList[i].SetSiblingIndex(i);
}


}

public void OnDrag(PointerEventData eventData)
{
//每帧拖动的距离
float dis = eventData.delta.x;
//通过计算弧度距离
float dragAng = dis / r;
moveAng += dragAng;
Move();
}
public void OnEndDrag(PointerEventData eventData)
{
float speed = eventData.delta.x;
float time = Mathf.Abs(speed) / cut;
DOTween.To((a) =>
{
//通过距离计算弧度
float dragAng = a / r;
moveAng += dragAng;
Move();
}, speed, 0, time).OnComplete(() =>
{

Align(list.IndexOf(sortList[n - 1].GetComponent<Image>()));//找到正中间的图片ID
});
}
public void Align(int id)
{
//当前下标
int index = list.IndexOf(sortList[n - 1].GetComponent<Image>());
//指定下标与当前下标的问题
int sp = id - index;
//计算另一个旋转方向需要的间隔
int sp0 = n - Mathf.Abs(sp);
//一个方向是顺时针另一个方向是逆时针 所以一个是正另一个一定是负
sp0 = sp > 0 ? -sp0 : sp0;
//选择两个方向中间间隔较小的一方旋转
int end = Mathf.Abs(sp) < Mathf.Abs(sp0) ? sp : sp0;
//计算间隔的弧度
float spAng = end * ang;
//需要旋转的弧度=与中间偏差的弧度+间隔的弧度
float AlignAng = Mathf.Asin(sortList[n - 1].GetComponent<RectTransform>().anchoredPosition.x / r) + spAng;
//通过需要旋转的弧度计算需要旋转的距离
float Aligndis = AlignAng * r;
//通过需要旋转的距离计算旋转的时间
float time = Mathf.Abs(AlignAng) / cut;
DOTween.To((a) =>
{

moveAng = a;
Move();
}, moveAng, moveAng - AlignAng, time).OnComplete(() =>//此处是换装可无视
{
//找到中间对应的图片 ID
int num = list.IndexOf(sortList[sortList.Count - 1].GetComponent<Image>());

if (n==12)
{
player.games[0] = Resources.Load<GameObject>("PlayerPPP/Prefab/Tou_" +num);
casting.ChangeClothes(0,num);
}

if (n==4)
{

player.games[1] = Resources.Load<GameObject>("PlayerPPP/Prefab/YiFu_" + num);
casting.ChangeClothes(1, num);
}
if (n == 5)
{
player.games[2] = Resources.Load<GameObject>("PlayerPPP/Prefab/Tui_" + num);
casting.ChangeClothes(2, num);
}
});

}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值