137 lines
4.2 KiB
C#
137 lines
4.2 KiB
C#
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<NetworkMarkerEraser>();
|
|
List<Transform> activeErasers = new List<Transform>();
|
|
|
|
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<LineRenderer>();
|
|
|
|
foreach (GameObject go in gos)
|
|
{
|
|
LineRenderer lr = go.GetComponent<LineRenderer>();
|
|
if (lr != null)
|
|
{
|
|
allLines.Add(lr);
|
|
}
|
|
}
|
|
for (int lineIndex = allLines.Count - 1; lineIndex >= 0; lineIndex--)
|
|
{
|
|
LineRenderer line = allLines[lineIndex];
|
|
if (line == null) continue;
|
|
|
|
List<Vector3> segment = new List<Vector3>();
|
|
List<List<Vector3>> segments = new List<List<Vector3>>();
|
|
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<Vector3>(segment));
|
|
segment.Clear();
|
|
}
|
|
isErasing = false;
|
|
}
|
|
segment.Add(point);
|
|
}
|
|
else
|
|
{
|
|
isErasing = true;
|
|
if (segment.Count > 0)
|
|
{
|
|
segments.Add(new List<Vector3>(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<Vector3> points, LineRenderer line)
|
|
{
|
|
GameObject newLineObj = new GameObject("SplitLine");
|
|
LineRenderer newLine = newLineObj.AddComponent<LineRenderer>();
|
|
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<LineRenderer> allLines = new List<LineRenderer>();
|
|
|
|
}
|