68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
![]() |
using UnityEngine;
|
|||
|
using UnityEditor;
|
|||
|
using Dreamteck.Splines;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
public class SplineSimpleMerge : EditorWindow
|
|||
|
{
|
|||
|
[MenuItem("Tools/Splines/Simple Merge Selected Splines")]
|
|||
|
static void ShowWindow() => GetWindow<SplineSimpleMerge>("Merge Splines");
|
|||
|
|
|||
|
void OnGUI()
|
|||
|
{
|
|||
|
if (GUILayout.Button("合并选中的 SplineComputer"))
|
|||
|
{
|
|||
|
MergeSplines();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void MergeSplines()
|
|||
|
{
|
|||
|
var list = new List<SplinePoint>();
|
|||
|
|
|||
|
foreach (var transform in Selection.transforms)
|
|||
|
{
|
|||
|
if (!transform.TryGetComponent<SplineComputer>(out var computer)) continue;
|
|||
|
var points = computer.GetPoints();
|
|||
|
|
|||
|
for (var i = 0; i < points.Length; i++)
|
|||
|
{
|
|||
|
var splinePoint = points[i];
|
|||
|
|
|||
|
// 如果不是第一个段的第一个点,需要平滑衔接前一个点
|
|||
|
if (i == 0 && list.Count > 0)
|
|||
|
{
|
|||
|
var lastIndex = list.Count - 1;
|
|||
|
var last = list[lastIndex];
|
|||
|
|
|||
|
// 计算两点之间方向
|
|||
|
Vector3 dir = (splinePoint.position - last.position).normalized;
|
|||
|
float distance = Vector3.Distance(splinePoint.position, last.position) * 0.5f;
|
|||
|
|
|||
|
// 设置上一个点的 tangent 和当前点的 tangent2(对称方向)
|
|||
|
last.tangent2 = last.position + dir * distance;
|
|||
|
splinePoint.tangent = splinePoint.position - dir * distance;
|
|||
|
|
|||
|
list[lastIndex] = last; // 更新修改过的前一个点
|
|||
|
}
|
|||
|
|
|||
|
list.Add(splinePoint);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (list.Count == 0)
|
|||
|
{
|
|||
|
Debug.LogWarning("未找到任何 SplineComputer!");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// 创建新的合并轨道
|
|||
|
var go = new GameObject("Spline_Merged_Simple");
|
|||
|
var newSpline = go.AddComponent<SplineComputer>();
|
|||
|
newSpline.type = Spline.Type.Bezier;
|
|||
|
newSpline.SetPoints(list.ToArray());
|
|||
|
|
|||
|
Debug.Log($"✅ 合并成功,生成 {list.Count} 个 SplinePoint");
|
|||
|
}
|
|||
|
}
|