设置预制体对象、数量、半径、角度、移动的角度、两个用以存放对象的集合
遍历、计算x,y、生成预制体对象添加到集合中、取出单个对象给其位置赋值、预制体挂载脚本用以拖拽传值、分别为拖拽中传值、拖拽结束传值、注意需要坐标转换
空物体中处理代码(根据预制体传过来的值进行旋转移动处理)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class Cell : MonoBehaviour
{
public GameObject m_perfab;
public int m_num;
public float m_r;
float m_angle;
float m_moveangle;
List<GameObject> m_cells = new List<GameObject>();
List<GameObject> m_lists = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
m_angle = 2 * Mathf.PI / m_num;
Move();
}
public void Move()
{
for (int i = 0; i < m_num; i++)
{
float x = Mathf.Sin(i * m_angle + m_moveangle) * m_r;
float z = Mathf.Cos(i * m_angle + m_moveangle) * m_r;
if (m_cells.Count<=i)
{
GameObject go = Instantiate(m_perfab, transform);
m_cells.Add(go);
m_lists.Add(go);
}
m_cells[i].transform.localPosition = new Vector3(x, 0, z);
}
}
public void OnDrag(float dis)
{
m_moveangle += dis / m_r;
Move();
}
public void OnEndDrag(float dis)
{
DOTween.To((a) =>
{
m_moveangle += a / m_r;
Move();
},dis,0,2).OnComplete(()=> {
m_lists.Sort((a,b) =>
{
return (int)(a.transform.localPosition.z - b.transform.localPosition.z);
});
float ang = Mathf.Asin(m_lists[0].transform.localPosition.x / m_r);
DOTween.To((a) =>
{
m_moveangle = a;
Move();
},m_moveangle,m_moveangle+ang,1);
});
}
public void OnClick(int index)//处理按钮传入指定下标从而转到该物体的方法
{
m_lists.Sort((a, b) =>
{
return (int)(a.transform.localPosition.z - b.transform.localPosition.z);
});
int id = m_cells.IndexOf(m_lists[0]);
float endang = (id - index) * m_angle;
float mang = Mathf.Asin(m_lists[0].transform.localPosition.x / m_r) + endang;
DOTween.To((a) =>
{
m_moveangle = a;
Move();
}, m_moveangle, m_moveangle + mang, 1);
}
// Update is called once per frame
void Update()
{
}
}
预制体代码