using System.Collections.Generic; using UnityEngine; public class EreaserManager : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { } // Update is called once per frame void Update() { EraseNearbyLines(); } public float EraseRadius = 0.1f; // شعاع پاک کردن خطوط [System.Obsolete] private void EraseNearbyLines() { // پیدا کردن تمام پاک‌کن‌هایی که isDrawing == true دارند NetworkMarkerEraser[] erasers = FindObjectsOfType(); List activeErasers = new List(); foreach (var eraser in erasers) { if (eraser.isDrawing) { activeErasers.Add(eraser.transform); } } if (activeErasers.Count == 0) return; // اگر هیچ پاک‌کن فعالی نبود، نیازی به پردازش نیست GameObject[] gos = GameObject.FindGameObjectsWithTag("draw"); allLines = new List(); foreach (GameObject go in gos) { LineRenderer lr = go.GetComponent(); if (lr != null) { allLines.Add(lr); } } for (int lineIndex = allLines.Count - 1; lineIndex >= 0; lineIndex--) { LineRenderer line = allLines[lineIndex]; if (line == null) continue; List segment = new List(); List> segments = new List>(); bool isErasing = false; for (int i = 0; i < line.positionCount; i++) { Vector3 point = line.GetPosition(i); bool shouldErase = false; // بررسی می‌کنیم که آیا نقطه در محدوده‌ی حداقل یکی از پاک‌کن‌های فعال قرار دارد foreach (Transform eraser in activeErasers) { if (Vector3.Distance(eraser.position, point) <= EraseRadius) { shouldErase = true; break; } } if (!shouldErase) { if (isErasing) { if (segment.Count > 0) { segments.Add(new List(segment)); segment.Clear(); } isErasing = false; } segment.Add(point); } else { isErasing = true; if (segment.Count > 0) { segments.Add(new List(segment)); segment.Clear(); } } } if (segment.Count > 0) segments.Add(segment); allLines.RemoveAt(lineIndex); Destroy(line.gameObject); foreach (var seg in segments) { if (seg.Count > 1) { CreateNewLine(seg, line); } } } } private void CreateNewLine(List points, LineRenderer line) { GameObject newLineObj = new GameObject("SplitLine"); LineRenderer newLine = newLineObj.AddComponent(); newLine.tag = "draw"; newLine.positionCount = points.Count; newLine.material = line.material; newLine.startColor = line.startColor; newLine.endColor = line.endColor; newLine.SetPositions(points.ToArray()); newLine.startWidth = line.startWidth; newLine.endWidth = line.endWidth; allLines.Add(newLine); } public void RegisterLine(LineRenderer line) { if (!allLines.Contains(line)) { allLines.Add(line); } } public List allLines = new List(); }