using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class EllipticalMovement : MonoBehaviour { public List images; // 存放所有图片的 Transform 列表 public float semiMajorAxis = 200f; // 长轴半径 public float semiMinorAxis = 100f; // 短轴半径 public float rotationSpeed = 200f; // 旋转速度(角度/秒) public Vector3 orbitCenter = Vector3.zero; // 椭圆旋转的中心点 public float min; public bool isRun = false; // 是否运行 private float angle = 0f; // 当前角度 private bool temp = false; private int temp1 = -1; private float timer = 0f; private void Awake() { Resolution deviceResolution = Screen.currentResolution; float xiShu = Screen.width / 1920f; orbitCenter.x *= xiShu; orbitCenter.y *= xiShu; orbitCenter.z *= xiShu; semiMajorAxis *= xiShu; semiMinorAxis *= xiShu; } void OnEnable() { // min*=xiShu; // 启动协程来控制旋转 timer = 0f; StartCoroutine(RotateAndPause()); } // 初始化图片的位置,确保相对布局不发生变化 public IEnumerator RotateAndPause() { while (isRun) // 永久循环,直到手动停止 { timer += Time.deltaTime; // 计算旋转角度 angle += rotationSpeed * Time.deltaTime; if (angle >= 360f) { angle -= 360f; // 保证角度在 0 到 360 之间 } // 计算弧度 float radian = Mathf.Deg2Rad * angle; // 如果是第一次运行,跳过这个检查,避免触发延时 if (temp) { temp = false; // 设置标志位,后续不再跳过 continue; // 跳过本次循环,直接进入下一个图片 } // 计算每个图片的位置 for (int i = 0; i < images.Count; i++) { // 每个图片的角度稍微不同,形成均匀分布 float offsetAngle = i * (360f / images.Count); // 均匀分布的偏移角度 float currentAngle = radian + Mathf.Deg2Rad * offsetAngle; // 加上偏移角度 // 计算每个图片的位置(相对于 orbitCenter 的偏移) float x = semiMajorAxis * Mathf.Cos(currentAngle); float y = semiMinorAxis * Mathf.Sin(currentAngle); // 将图片的位置设置为相对 orbitCenter 的位置 images[i].position = new Vector3(x, y, 0f) + orbitCenter; // print(images[i].position.y+" "+images[i].name); if (images[i].localPosition.y < min && i != temp1) { if (timer < 1f) { continue; } temp1 = i; print(images[i].position.y + " " + images[i].name); ShowHidden showHidden = images[i].gameObject.GetComponent(); if (showHidden != null) { showHidden.OnMouseDown(); } AutoImage autoImage = images[i].gameObject.GetComponent(); if (autoImage != null) { autoImage.CutImage(); } yield return new WaitForSeconds(5f); } } yield return null; } } }