using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Lun2D : MonoBehaviour, IDragHandler, IEndDragHandler
{
//份数
int count2d=5;
//半径
float r2d = 300;
//周长
float zc2d;
//每份的弧度
float hu2d;
//改变的弧度
float movehu2d;
//要生成的图片对象
public Image Image;
//最大缩放
float max2d = 1;
//最小缩放
float min2d = 0.3f;
//图片间距
public int space2d = 200;
List<GameObject> gameObjects2d = new List<GameObject>();
List<Transform> transforms2d = new List<Transform>();
private void Awake()
{
hu2d = 2 * Mathf.PI / count2d;
zc2d = (Image.rectTransform.sizeDelta.x + space2d) * count2d;//周长
r2d = zc2d / (2 * Mathf.PI);//半径
CreateImage();
}
//指定去某个位置
public void ChangeImage(int n)
{
//获取当前下标
int now = gameObjects2d.IndexOf(transforms2d[count2d - 1].gameObject);
float angel_move = 0;
angel_move = (now - n) * hu2d;
DOTween.To(() => movehu2d, t =>
{
movehu2d = t;
CreateImage();
}, movehu2d + angel_move, 2);
}
public void OnClickUI()
{
int n = UnityEngine.Random.Range(0, count2d);
ChangeImage(n);
}
private void CreateImage()
{
for (int i = 0; i < count2d; i++)
{
if (gameObjects2d.Count <= i)
{
gameObjects2d.Add(Instantiate(Image.gameObject, transform));
transforms2d.Add(gameObjects2d[i].transform);
}
float x = Mathf.Sin(hu2d * i + movehu2d) * r2d;
float y = Mathf.Cos(hu2d * i + movehu2d) * r2d;
float bl = (y + r2d) / (2 * r2d);
float xzbl = max2d * (1 - bl) + min2d * bl;//限制比例
gameObjects2d[i].transform.localPosition = new Vector3(x, 0, 0);
gameObjects2d[i].transform.localScale = Vector3.one * xzbl;
}
transforms2d = transforms2d.OrderBy(x => x.localScale.x).ToList();
for (int i = 0; i < transforms2d.Count; i++)
{
transforms2d[i].SetSiblingIndex(i);//设置对应下标
}
}
void Start()
{
//循环赋图片
for (int i = 0; i < gameObjects2d.Count; i++)
{
gameObjects2d[i].GetComponent<Image>().sprite = Resources.Load<Sprite>("Image/index_" + i);
}
//先回正
transforms2d = transforms2d.OrderBy(x => x.localScale.x).ToList();
float needhu = Mathf.Asin(transforms2d[count2d - 1].localPosition.x / r2d);
DOTween.To((b) =>
{
movehu2d = b;
CreateImage();
int index = gameObjects2d.IndexOf(transforms2d[count2d - 1].gameObject);
}, movehu2d, movehu2d + needhu, 1);
}
public void OnDrag(PointerEventData eventData)
{
var dis = eventData.delta.x / r2d;
movehu2d -= dis;
CreateImage();
}
public void OnEndDrag(PointerEventData eventData)
{
var dis = eventData.delta.x / r2d;
DOTween.To((a) =>
{
movehu2d -= a;
CreateImage();
}, dis, 0, Mathf.Abs(dis)).OnComplete(() =>
{
transforms2d = transforms2d.OrderBy(x => x.localScale.x).ToList();
float needhu = Mathf.Asin(transforms2d[count2d - 1].localPosition.x / r2d);
DOTween.To((b) =>
{
movehu2d = b;
CreateImage();
int index = gameObjects2d.IndexOf(transforms2d[count2d - 1].gameObject);
}, movehu2d, movehu2d + needhu, 1);
});
}
}
04-24
361
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-26
153
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-20
4013
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交