/// <summary> /// inspector GUI /// </summary> public override void OnInspectorGUI() { // Init if (splineWindow == null) { return; } Spline temp = (Spline)EditorGUILayout.ObjectField("Spline", splineWindow.splineRefernce, typeof(Spline), true); // resetting the spline variable when the refernce changes if (GUI.changed) { if (temp == null) { splineWindow.spline = null; splineWindow.splineRefernce = null; } else { splineWindow.splineRefernce = temp; ScriptableObject.DestroyImmediate(splineWindow.spline); splineWindow.spline = Spline.CreateSpline(temp); EditorUtility.SetDirty(splineWindow.spline); } if (SplineTools.Instance) { SplineTools.Instance.Repaint(); } EditorUtility.SetDirty(target); } if (!spline) { return; } SerializedObject s = new SerializedObject(spline); EditorGUILayout.PropertyField(s.FindProperty("resolution"), new GUIContent("Resolution", "amount of segments between 2 points, increasing this number may affect performance")); s.ApplyModifiedProperties(); if (spline.resolution < 0.1f) { spline.resolution = 0.1f; } if (spline.resolution > 500) { spline.resolution = 500; } if (GUILayout.Button("Override colors")) { overrideColors = !overrideColors; } if (overrideColors) { splineWindow.splineColor = EditorGUILayout.ColorField("Spline Color", splineWindow.splineColor); splineWindow.pointColor = EditorGUILayout.ColorField("Point Color", splineWindow.pointColor); splineWindow.handlesColor = EditorGUILayout.ColorField("Handles Color", splineWindow.handlesColor); splineWindow.selectedColor = EditorGUILayout.ColorField("Selection Color", splineWindow.selectedColor); } GUILayout.Label("Total points : " + spline.Length); if (GUI.changed) { SceneView.RepaintAll(); } //drawing SerializedObject so = new SerializedObject(spline); splineWindow.SetWorldPositions(); foreach (int i in selectedPoints) { if (spline.IsValid(i)) { EditorGUILayout.LabelField("------ Point index : " + i + " ------"); spline[i].method = EditorGUILayout.TextField(new GUIContent("Method", "method will be called when object reaches this point"), spline[i].method); Vector3 pos = spline[i].localPosition; // position field Undo.RecordObject(spline, "move point"); spline[i].localPosition = EditorGUILayout.Vector3Field(new GUIContent("Position", "this point position"), spline[i].localPosition); if (pos != spline[i].localPosition) { splineWindow.SetWorldPositions(); disableHandleMoving = false; List <Vector3> handles = new List <Vector3>(spline[i].handles); MoveHandlesWithPoint(i, spline[i].position, handles); spline[i].handles = handles.ToArray(); splineWindow.SetLocalPositions(); } else { Undo.ClearUndo(spline); } //handles EditorGUILayout.LabelField("------ Handles ------"); for (int h = 0; h < spline[i].handles.Length; h++) { spline[i].localHandles[h] = EditorGUILayout.Vector3Field(new GUIContent("Handle " + h, "this is the handle position"), spline[i].localHandles[h]); } } } if (GUI.changed) { splineWindow.SetWorldPositions(); SceneView.RepaintAll(); } so.ApplyModifiedProperties(); }
/// <summary> /// main method it will be called when the cursor in the scene view /// most of the functionality is in here /// </summary> void OnSceneGUI() { if (spline) { Undo.RecordObject(spline, "Spline edit"); } #region Init //getting spline asset from the SplineWindow object splineWindow = (SplineWindow)target; spline = splineWindow.spline; if (!splineWindow) { return; } if (!spline && splineWindow) { DestroyImmediate(spline); splineWindow.spline = Spline.CreateSpline(splineWindow.splineRefernce); splineWindow.SetWorldPositions(); if (SplineTools.Instance) { SplineTools.Instance.Repaint(); } if (!spline) { return; } } CheckSelection(); //make sure to follow parent when nothing is selected if (selectedPoint == -1) { splineWindow.SetWorldPositions(); //make sure that the main handle shows up when nothing is selected DefaultHandles.Hidden = false; } else { DefaultHandles.Hidden = hideDefaultHandles; } SetCurrPositions(); #endregion #region drawing points if (selectedPoints.Count > 1) { CalculateCenterPos(); } bool setWorldPos = true; bool setLocalPos = false; for (int i = 0; i < spline.Length; i++) { Handles.color = splineWindow.pointColor; if (!hideArrows) { DrawArrow(i); } #region unselected points //draw points ( main ) Vector3 pos = spline.GetPosition(i); if (currPos[i] != pos) { Undo.RecordObject(spline, "Move point"); } List <Vector3> handles = new List <Vector3>(spline[i].handles); Handles.color = splineWindow.pointColor; float size = HandleUtility.GetHandleSize(pos); if (!InSelection(i)) { // point ( main ) pressed if (Handles.Button(pos, Quaternion.identity, size / 5, size / 5, Handles.SphereCap)) { SelectPoint(i); } //draw points ( handles ) if (!hideHandles) { Handles.color = splineWindow.handlesColor; foreach (Vector3 h in handles) { size = HandleUtility.GetHandleSize(h); size *= 0.5f; // point ( handles ) pressed if (Handles.Button(h, Quaternion.identity, size / 5, size / 5, Handles.CubeCap)) { SelectPoint(i); } //drawing the blue lines between main point and handle point Handles.DrawLine(pos, h); } } } #endregion #region selected points // moving selected point if (InSelection(i)) { // hold the position to know later if it got changed Vector3 temp = pos; Vector3 hTemp = Vector3.zero; // drawing points when multiple are selected if (selectedPoints.Count > 1) { Handles.color = splineWindow.selectedColor; if (Handles.Button(pos, Quaternion.identity, size / 4, size / 4, Handles.SphereCap)) { // removes the point from selection or remove other points if (Event.current.shift) { selectedPoints.Remove(i); } else { SelectPoint(i); } } pos += centerDelta; } else { switch (Tools.current) { case Tool.Rotate: rotation = Handles.DoRotationHandle(rotation, pos); Handles.SphereCap(0, pos, Quaternion.identity, size / 5); if (rotation != Quaternion.identity) { for (int h = 0; h < handles.Count; h++) { if (handleStartPositions.Count > h) { handles[h] = rotation * (handleStartPositions[h] - pos); handles[h] += pos; } } } else { handleStartPositions = new List <Vector3>(handles); } scale = 1; break; case Tool.Scale: Vector3 s = new Vector3(scale, 1, 1); s = Handles.DoScaleHandle(s, pos, Quaternion.identity, size); scale = s.x; if (scale != 1) { for (int h = 0; h < handles.Count; h++) { if (handleStartPositions.Count > h) { handles[h] = handleStartPositions[h] + (handleStartPositions[h] - pos).normalized * (scale - 1); } } } else { handleStartPositions = new List <Vector3>(handles); } rotation = Quaternion.identity; break; default: if (isDragMode) { Handles.color = splineWindow.selectedColor; pos = Handles.FreeMoveHandle(pos, Quaternion.identity, size / 4, Vector3.zero, Handles.SphereCap); } else { pos = Handles.PositionHandle(pos, Quaternion.identity); Handles.color = splineWindow.selectedColor; Handles.SphereCap(0, pos, Quaternion.identity, size / 4); } scale = 1; rotation = Quaternion.identity; break; } } //checking if our pos changed to reset the values if (temp != pos) { setLocalPos = true; setWorldPos = false; } if (Tools.current != Tool.Rotate && Tools.current != Tool.Scale) { MoveHandlesWithPoint(i, pos, handles); if (!hideHandles) { for (int a = 0; a < handles.Count; a++) { hTemp = handles[a]; if (isDragMode) { Handles.color = splineWindow.handlesColor; Vector3 tempHandles = Handles.FreeMoveHandle(handles[a], Quaternion.identity, size / 7, Vector3.zero, Handles.CubeCap); handles[a] = tempHandles; } else { Handles.color = splineWindow.handlesColor; handles[a] = Handles.PositionHandle(handles[a], Quaternion.identity); Handles.CubeCap(0, handles[a], Quaternion.identity, size / 7); } if (hTemp != handles[a]) { setLocalPos = true; setWorldPos = false; } Handles.DrawLine(pos, handles[a]); } } } else if (!hideHandles) { foreach (Vector3 handle in handles) { Handles.CubeCap(0, handle, Quaternion.identity, size / 7); Handles.DrawLine(pos, handle); if (hTemp != handle) { setLocalPos = true; setWorldPos = false; } } } } currPos[i] = pos; #endregion //reassign values spline[i].handles = handles.ToArray(); spline[i].position = pos; if (showIndex) { GUIStyle style = new GUIStyle(); style.normal.textColor = Color.grey; style.fontSize = 20; Handles.Label(spline[i].position, i.ToString(), style); } } if (setLocalPos) { splineWindow.SetLocalPositions(); } if (setWorldPos) { splineWindow.SetWorldPositions(); } #endregion #region insert point // insert by clicking on some collider if (Event.current.control == SplinePrefs.addPointMouse.ctrl && Event.current.alt == SplinePrefs.addPointMouse.alt && Event.current.shift == SplinePrefs.addPointMouse.shift) { //making clicking with left click on objects disabled HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); if (spline.Length == 0 || selectedPoint >= 0) { if (Event.current.clickCount == 1) { Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Insert(hit.point); splineWindow.SetLocalPositions(); } } } } if (spline.IsValid(selectedPoint)) { //insert by a shortcut if (Event.current.keyCode == SplinePrefs.addPointKeyboard.keyCode && Event.current.shift && Event.current.type == EventType.KeyDown) { Insert(spline[selectedPoint].position + Vector3.forward); splineWindow.SetLocalPositions(); } } #endregion #region keyboard actions // sticking to ground if (HotkeyPressed(SplinePrefs.stickToGround) && selectedPoint != -1) { Undo.RecordObject(splineWindow, "stick to ground"); foreach (int i in selectedPoints) { StickToGround(i); } } // drawing line if (HotkeyPressed(SplinePrefs.drawLine) && selectedPoints.Count == 2) { spline.DrawLine(selectedPoints[0], selectedPoints[1]); } // remove point if (HotkeyPressed(SplinePrefs.removePoint)) { foreach (int i in selectedPoints) { spline.RemovePoint(i); } splineWindow.SetLocalPositions(); selectedPoints = new List <int>(); } if (HotkeyPressed(SplinePrefs.sharpen)) { Undo.RecordObject(spline, "Sharpen"); foreach (int i in selectedPoints) { spline.Sharpen(i); splineWindow.SetLocalPositions(); SceneView.RepaintAll(); } } #endregion #region others SplineMesh sm = splineWindow.GetComponent <SplineMesh>(); if (sm != null) { sm.UpdatePosition(); } if (Event.current.commandName == "UndoRedoPerformed") { Repaint(); } #endregion }