Ver.0.3.0.33
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CutLineManager : MonoBehaviour
|
||||
{
|
||||
[Header("切割线参数")]
|
||||
public float cutLineWidth = 0.1f;
|
||||
public float noiseAmount = 0.05f; // 毛刺偏移幅度
|
||||
public float segmentDuration = 4f; // 线段持续时间
|
||||
public float cutLineDelay = 3f; // 延迟几秒才显示轨迹点
|
||||
|
||||
public LineRenderer mainCutLineRenderer;
|
||||
private List<TimedPoint> timedCutPoints = new List<TimedPoint>();
|
||||
private bool isCutting = false;
|
||||
public bool IsCutting => isCutting; // Public property to access isCutting state
|
||||
|
||||
private class TimedPoint
|
||||
{
|
||||
public Vector3 position;
|
||||
public float timeAdded;
|
||||
public TimedPoint(Vector3 pos, float time)
|
||||
{
|
||||
position = pos;
|
||||
timeAdded = time;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitializeMainCutLine();
|
||||
}
|
||||
|
||||
private void InitializeMainCutLine()
|
||||
{
|
||||
GameObject cutLineObj = new GameObject("MainCutLine");
|
||||
cutLineObj.transform.parent = transform;
|
||||
mainCutLineRenderer = cutLineObj.AddComponent<LineRenderer>();
|
||||
mainCutLineRenderer.positionCount = 0;
|
||||
mainCutLineRenderer.widthCurve = AnimationCurve.Constant(0, 1, cutLineWidth);
|
||||
mainCutLineRenderer.material = new Material(Shader.Find("Sprites/Default"));
|
||||
mainCutLineRenderer.numCapVertices = 0;
|
||||
mainCutLineRenderer.sortingOrder = 1;
|
||||
}
|
||||
|
||||
public void StartCutting()
|
||||
{
|
||||
isCutting = true;
|
||||
timedCutPoints.Clear();
|
||||
if (mainCutLineRenderer != null)
|
||||
{
|
||||
mainCutLineRenderer.positionCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddCutPoint(Vector3 point)
|
||||
{
|
||||
if (!isCutting) return;
|
||||
|
||||
if (timedCutPoints.Count == 0 || Vector3.Distance(timedCutPoints[timedCutPoints.Count - 1].position, point) > 0.05f)
|
||||
{
|
||||
timedCutPoints.Add(new TimedPoint(point, Time.time));
|
||||
|
||||
if (timedCutPoints.Count >= 2)
|
||||
{
|
||||
Vector3 prev = timedCutPoints[timedCutPoints.Count - 2].position;
|
||||
Vector3 curr = timedCutPoints[timedCutPoints.Count - 1].position;
|
||||
CreateSegment(prev, curr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void EndCutting()
|
||||
{
|
||||
isCutting = false;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateCutLineRenderer();
|
||||
}
|
||||
|
||||
private void UpdateCutLineRenderer()
|
||||
{
|
||||
if (mainCutLineRenderer == null) return;
|
||||
|
||||
float currentTime = Time.time;
|
||||
List<Vector3> visiblePoints = new List<Vector3>();
|
||||
|
||||
foreach (var timedPoint in timedCutPoints)
|
||||
{
|
||||
if (currentTime - timedPoint.timeAdded >= cutLineDelay)
|
||||
{
|
||||
visiblePoints.Add(timedPoint.position);
|
||||
}
|
||||
}
|
||||
|
||||
mainCutLineRenderer.positionCount = visiblePoints.Count;
|
||||
if (visiblePoints.Count > 0)
|
||||
mainCutLineRenderer.SetPositions(visiblePoints.ToArray());
|
||||
}
|
||||
|
||||
private void CreateSegment(Vector3 from, Vector3 to)
|
||||
{
|
||||
GameObject segObj = new GameObject("HeatLineSegment");
|
||||
segObj.transform.parent = transform;
|
||||
|
||||
LineRenderer segLine = segObj.AddComponent<LineRenderer>();
|
||||
segLine.positionCount = 2;
|
||||
segLine.SetPosition(0, from);
|
||||
segLine.SetPosition(1, to);
|
||||
segLine.sortingOrder = 4;
|
||||
segLine.material = new Material(Shader.Find("Sprites/Default"));
|
||||
segLine.widthCurve = AnimationCurve.Constant(0, 1, cutLineWidth);
|
||||
segLine.numCapVertices = 0;
|
||||
var segmentController = segObj.AddComponent<HeatLineSegmentController>();
|
||||
segmentController.duration = segmentDuration;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user