using System; using System.Collections; using UnityEngine; namespace AibisDream.FixSystem { public class ScanLine : MonoBehaviour { #region 组件索引 private OscilloscopeScreen _screen; private LineRenderer _line; #endregion #region 扫描线状态 private bool _isScanning; #endregion #region 部分参数(后面改成从screen获取) private float _screenHeight; private float _screenWidth; private float _screenLeftX; private float _screenRightX; private float _topY; private float _bottomY; private float _spikeX; private float _scanSpeed; private event Action ScanErrorEvent; #endregion private void Awake() { InitComponentRefs(); } private void InitComponentRefs() { _screen = transform.parent.GetComponent(); _line = GetComponent(); } public IEnumerator ScanForError() { // 初始化扫描线 InitStartLine(); // 扫描线右移 var currentX = _screenLeftX; var lastX = currentX; bool isSpikeFound = false; while (currentX <= _screenRightX) { // 横坐标右移 currentX += Time.deltaTime * _scanSpeed; SetLinePos(currentX); // 检查是否扫到尖峰 if (CheckSpikeInRange(lastX, currentX)) { isSpikeFound = true; // 修改线条 MarkLineAsError(); // 触发扫描事件 yield return new WaitForSeconds(1f); ScanErrorEvent?.Invoke(); } } } private void InitStartLine() { // 初始状态是一根靠左的竖线 SetLinePos(_screenLeftX); _line.enabled = true; } private void SetLinePos(float posX) { var topPosition = new Vector3(posX, _topY, 0); var bottomPosition = new Vector3(posX, _bottomY, 0); _line.SetPosition(0, topPosition); _line.SetPosition(1, bottomPosition); } private bool CheckSpikeInRange(float x1, float x2) { var left = Mathf.Min(x1, x2); var right = Mathf.Max(x1, x2); return _spikeX >= left && _spikeX <= right; } private void MarkLineAsError() { SetLinePos(_spikeX); _line.startColor = Color.red; _line.endColor = Color.red; } public void CloseScan() { _line.enabled = false; } } }