void OnEnable() { creator = (SnakePathCreator)target; lastBezierSegmentInfo = new Types.BezierSegmentInfo(); if (creator.snakePath != null) { creator.snakePath.OffsetPosition = creator.transform.position; EditorUtility.SetDirty(creator.snakePath); } }
void Input(Event guiEvent) { Vector3 mousePos = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).origin; Vector3 direction = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition).direction; //Only allow adding segment on orthographic mode and 100% along one camera axis if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.shift && IsAddSegmentAvailable(direction)) { Vector3 lastPoint = ConvertToWorldPos(creator.snakePath[creator.snakePath.PointCount - 1]); //float magnitude = Vector3.Distance(lastPoint, mousePos); //Vector3 finalPoint = mousePos + (direction * magnitude); Vector3 finalPoint = RestrictPositionBaseOnCameraDir(direction, mousePos, lastPoint); Undo.RecordObject(creator.snakePath, "Add segment"); creator.snakePath.AddSegment(ConvertToLocalPos(finalPoint)); AutoSetControlPointIfEnable(creator.snakePath.PointCount - 1); } //Delete Anchor if (guiEvent.type == EventType.MouseDown && guiEvent.button == 1) { int removeIndex = FindTheMostClosetIndex(mousePos, direction); if (removeIndex >= 0) { Undo.RecordObject(creator.snakePath, "Delete segment"); creator.snakePath.Delete(removeIndex); AutoSetControlPointIfEnable(removeIndex); } } if (guiEvent.type == EventType.MouseMove && guiEvent.control) { lastBezierSegmentInfo = FindClosestSegment(mousePos, direction); if (lastBezierSegmentInfo.SegmentIndex >= 0) { //Debug.Log($"SegmentIndex {lastBezierSegmentInfo.SegmentIndex}, t {lastBezierSegmentInfo.t}, dist {lastBezierSegmentInfo.dist}"); SceneView.RepaintAll(); } } if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && guiEvent.control) { if (lastBezierSegmentInfo.SegmentIndex >= 0) { Undo.RecordObject(creator.snakePath, "Insert segment"); creator.snakePath.SplitSegment(ConvertToLocalPos(lastBezierSegmentInfo.Position), lastBezierSegmentInfo.SegmentIndex); int anchorIndex = lastBezierSegmentInfo.SegmentIndex * 3; creator.SmoothCtrlPoints(anchorIndex - 3, anchorIndex + 3); SceneView.RepaintAll(); } } if (!guiEvent.control) { lastBezierSegmentInfo.SegmentIndex = -1; } }