using System.Collections.Generic; using UnityEngine; /// /// 选关场景循环 /// public class LevelSelectLoop : MonoBehaviour { #region 配置参数 /// /// 地块间隔 /// [SerializeField] private float spacing = 12; /// /// 速度 /// [SerializeField] private float speed = 3; /// /// 起始位置 /// [SerializeField] private float startPos = -80f; /// /// 地块列表根节点 /// [SerializeField] private Transform tsPlotRoot; #endregion /// /// 当前地图节点 /// private Transform curTsMap; /// /// 地块列表 /// private readonly List listPlot; private float curPos = 0f; /// /// 上一次地块数量 /// private int lastCount; private void Awake() { curTsMap = GetComponent(); for (int i = 0; i < tsPlotRoot.childCount; ++i) { listPlot.Add(tsPlotRoot.GetChild(i)); } } private void Start() { curPos = startPos; curTsMap.localPosition = new Vector3(curTsMap.localPosition.x, curTsMap.localPosition.y, curPos); lastCount = (int)((startPos - curPos) / spacing) - 1; } private void Update() { curPos -= Time.deltaTime * speed; int count = (int)((startPos - curPos) / spacing) - 1; if (lastCount != count) { int index = count % listPlot.Count; if (index >= 0 && listPlot.Count > index) { Transform tsPlot = listPlot[index]; tsPlot.localPosition = new Vector3((listPlot.Count + count) * spacing, tsPlot.localPosition.y, tsPlot.localPosition.z); } lastCount = count; } curTsMap.localPosition = new Vector3(curTsMap.localPosition.x, curTsMap.localPosition.y, curPos); } }