大概效果是滑动滚轮,UI按钮按照一个轮盘的轨迹进行滑动的效果。
可以分为两步:
1.把UI按钮摆放成一个圆盘的轨迹
大概操作就是,圆盘按钮的数量/360得到按钮之间的旋转角度。
然后使用官方提供的Quaternion*poistion得到旋转后的位置,具体看代码。
2.父级旋转同时子集保持y轴一直朝上。
父级旋转的多少角度,子集也应该旋转相应的角度,只不过角度相反用于保持y轴一直朝上。
public class circleButton : MonoBehaviour
{
public List<GameObject> Buttons = new List<GameObject>();
public int ButtonsNum = 4;
private GameObject tempPos;
public Transform centerImage;
private float angle;
public float distance = 50;
private float wheel = 0;
public float rotateSpped = 20;
void Start()
{
InitCircleButton();
}
void Update()
{
SetDistance();
ControlRatate();
}
private void InitCircleButton()
{
GameObject btn = Resources.Load("Button") as GameObject;
angle = 360 / ButtonsNum; //几等分
float hudu = (angle / 180) * Mathf.PI;
for (int i = 0; i < ButtonsNum; i++)
{
GameObject m = Instantiate(btn);
m.transform.parent = centerImage.transform;
m.name = "i_" + i;
m.transform.GetChild(0).GetComponent<Text>().text = i + "";
m.transform.localPosition = Quaternion.Euler(0, 0, angle * i) * Vector2.up * distance;
m.SetActive(true);
Buttons.Add(m);
}
}
private void ControlRatate()
{
wheel = Input.GetAxis("Mouse ScrollWheel") * rotateSpped;
if (wheel != 0)
{
transform.Rotate(Vector3.forward * wheel);
for (int i = 0; i < ButtonsNum; i++)
{
Buttons[i].transform.Rotate(Vector3.back * wheel);
}
}
}
private void SetDistance()
{
for (int i = 0; i < ButtonsNum; i++)
{
Buttons[i].transform.localPosition = Quaternion.Euler(0, 0, angle * i) * Vector3.up * distance;
}
}
}
Image.gif
网友评论