Unity UI图片以360度排列,鼠标左键拖拽旋转
UI图片360度旋转排列
using UnityEngine;
public class UIArrangement : MonoBehaviour
{
public int numImages = 6; // 图片数量
public float radius = 100f; // 排列半径
private bool isDragging = false;
private Vector3 previousMousePosition;
private void Start()
{
ArrangeImages();
}
float angleStep;
private void ArrangeImages()
{
angleStep = 360f / numImages;
for (int i = 0; i < numImages; i++)
{
float angle = i * angleStep;
Vector3 position = Quaternion.Euler(0f, angle, 0f) * Vector3.forward * radius;
transform.GetChild(i).localPosition = position;
transform.GetChild(i).localEulerAngles = new Vector3(0,i* angleStep, 0);
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
isDragging = true;
previousMousePosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
isDragging = false;
}
if (isDragging)
{
Vector3 delta = Input.mousePosition - previousMousePosition;
transform.Rotate(-Vector3.up, delta.x, Space.World);
previousMousePosition = Input.mousePosition;
}
}
}