private void InsertNewPointAt(int index) { Vector3 position; if (spline.Count >= 2) { if (index > 0 && index < spline.Count) { position = (spline[index - 1].localPosition + spline[index].localPosition) * 0.5f; } else if (index == 0) { if (spline.loop) { position = (spline[0].localPosition + spline[spline.Count - 1].localPosition) * 0.5f; } else { position = spline[0].localPosition - (spline[1].localPosition - spline[0].localPosition) * 0.5f; } } else { if (spline.loop) { position = (spline[0].localPosition + spline[spline.Count - 1].localPosition) * 0.5f; } else { position = spline[index - 1].localPosition + (spline[index - 1].localPosition - spline[index - 2].localPosition) * 0.5f; } } } else if (spline.Count == 1) { position = index == 0 ? spline[0].localPosition - Vector3.forward : spline[0].localPosition + Vector3.forward; } else { position = Vector3.zero; } BezierPoint point = spline.InsertNewPointAt(index); point.localPosition = position; Undo.IncrementCurrentGroup(); Undo.RegisterCreatedObjectUndo(point.gameObject, "Insert Point"); Undo.RegisterCompleteObjectUndo(point.transform.parent, "Insert Point"); Selection.activeTransform = point.transform; SceneView.RepaintAll(); }
static void ImportBezierSpline(MenuCommand command) { string loadPath = EditorUtility.OpenFilePanel("Import spline", null, "json"); if (string.IsNullOrEmpty(loadPath)) { return; } SavedSpline savedSpline = JsonUtility.FromJson <SavedSpline>(File.ReadAllText(loadPath)); if (savedSpline == null || savedSpline.points == null || savedSpline.points.Length < 2) { Debug.LogError("Invalid saved data!"); return; } BezierSpline spline = (BezierSpline)command.context; spline.loop = savedSpline.loop; if (spline.Count > savedSpline.points.Length) { for (int i = spline.Count - 1; i >= savedSpline.points.Length; i--) { spline.RemovePointAt(i); } } else { for (int i = spline.Count; i < savedSpline.points.Length; i++) { spline.InsertNewPointAt(i); } } for (int i = 0; i < savedSpline.points.Length; i++) { BezierPoint point = spline[i]; SavedSpline.SavedPoint savedPoint = savedSpline.points[i]; point.localPosition = savedPoint.position; point.localRotation = savedPoint.rotation; point.localScale = savedPoint.scale; point.handleMode = savedPoint.handleMode; point.precedingControlPointLocalPosition = savedPoint.precedingPosition; point.followingControlPointLocalPosition = savedPoint.followingPosition; } }