public override void OnInspectorGUI()
        {
            EditorGUILayout.HelpBox("This object will create new game objects and parent them to this game object", MessageType.Warning);

            splineMesh = target as SplineMesh;
            SplineWindow splineWindow = splineMesh.spline;

            GenerateType type = splineMesh.type;

            splineMesh.spline            = EditorGUILayout.ObjectField(new GUIContent("Spline", "spline from the scene"), splineWindow, typeof(SplineWindow), true) as SplineWindow;
            splineMesh.prefab            = EditorGUILayout.ObjectField(new GUIContent("Prefab", "Game object to spawn"), splineMesh.prefab, typeof(GameObject), false) as GameObject;
            splineMesh.UseLookAtRotation = EditorGUILayout.Toggle(new GUIContent("Use look at rotation", "Rotate the object to face the next point"), splineMesh.UseLookAtRotation);
            splineMesh.update            = EditorGUILayout.Toggle(new GUIContent("Update", "when this is checked the spline mesh will be updated whenever it can (not in game)"), splineMesh.update);

            splineMesh.offsetPosition = EditorGUILayout.Vector3Field(new GUIContent("Offset position", "offset the position of all objects spawned"), splineMesh.offsetPosition);
            splineMesh.offsetRotation = EditorGUILayout.Vector3Field(new GUIContent("Offset rotation", "offset the rotation of all objects spawned"), splineMesh.offsetRotation);
            splineMesh.scale          = EditorGUILayout.Vector3Field(new GUIContent("Scale", "Scale all objects spawned"), splineMesh.scale);

            splineMesh.type = (GenerateType)EditorGUILayout.EnumPopup(new GUIContent("Type", "Generat type"), splineMesh.type);

            if (splineMesh.type == GenerateType.GenerateByDistance)
            {
                splineMesh.distance = EditorGUILayout.FloatField(new GUIContent("Distance", "The distance between objects"), splineMesh.distance);
                EditorGUILayout.HelpBox("Max number of objects depends on the resolution of the spline", MessageType.Info);
            }

            if (splineMesh == null || splineWindow == null || splineWindow.spline == null)
            {
                return;
            }

            if (splineMesh.prefab == null)
            {
                EditorGUILayout.HelpBox("Please assign a prefab", MessageType.Info);
            }

            realTime = EditorGUILayout.Toggle(new GUIContent("Real time", "Update the mesh real time"), realTime);

            if (splineMesh.type != type)
            {
                splineMesh.ClearAll();
            }

            splineMesh.UpdatePosition();
            if (realTime)
            {
                if (GUI.changed)
                {
                    splineMesh.ReGenerate();
                }
            }
            else
            {
                if (GUILayout.Button("Generate"))
                {
                    splineMesh.ReGenerate();
                }
            }

            if (GUILayout.Button("Clear"))
            {
                splineMesh.ClearAll();
            }
        }
        /// <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
        }
示例#3
0
        public override void OnInspectorGUI()
        {
            EditorGUILayout.HelpBox("This object will create new game objects and parent them to this game object", MessageType.Warning);

            splineMesh = target as SplineMesh;
            SplineWindow splineWindow = splineMesh.spline;

            GenerateType type = splineMesh.type;

            splineMesh.spline = EditorGUILayout.ObjectField(new GUIContent("Spline", "spline from the scene"), splineWindow, typeof(SplineWindow), true) as SplineWindow;
            splineMesh.prefab = EditorGUILayout.ObjectField(new GUIContent("Prefab", "Game object to spawn"), splineMesh.prefab, typeof(GameObject), false) as GameObject;
            splineMesh.UseLookAtRotation = EditorGUILayout.Toggle(new GUIContent("Use look at rotation", "Rotate the object to face the next point"), splineMesh.UseLookAtRotation);
            splineMesh.update = EditorGUILayout.Toggle(new GUIContent("Update", "when this is checked the spline mesh will be updated whenever it can (not in game)"), splineMesh.update);

            splineMesh.offsetPosition = EditorGUILayout.Vector3Field(new GUIContent("Offset position", "offset the position of all objects spawned"), splineMesh.offsetPosition);
            splineMesh.offsetRotation = EditorGUILayout.Vector3Field(new GUIContent("Offset rotation", "offset the rotation of all objects spawned"), splineMesh.offsetRotation);
            splineMesh.scale = EditorGUILayout.Vector3Field(new GUIContent("Scale", "Scale all objects spawned"), splineMesh.scale);

            splineMesh.type = (GenerateType)EditorGUILayout.EnumPopup(new GUIContent("Type", "Generat type"), splineMesh.type);

            if(splineMesh.type == GenerateType.GenerateByDistance)
            {
                splineMesh.distance = EditorGUILayout.FloatField(new GUIContent("Distance", "The distance between objects"), splineMesh.distance);
                EditorGUILayout.HelpBox("Max number of objects depends on the resolution of the spline", MessageType.Info);
            }

            if (splineMesh == null || splineWindow == null || splineWindow.spline == null) return;

            if (splineMesh.prefab == null)
            {
                EditorGUILayout.HelpBox("Please assign a prefab", MessageType.Info);
            }

            realTime = EditorGUILayout.Toggle(new GUIContent("Real time", "Update the mesh real time"), realTime);

            if(splineMesh.type != type)
            {
                splineMesh.ClearAll();
            }

            splineMesh.UpdatePosition();
            if (realTime)
            {
                if (GUI.changed)
                {
                    splineMesh.ReGenerate();
                }
            }
            else
            {
                if (GUILayout.Button("Generate"))
                {
                    splineMesh.ReGenerate();
                }
            }

            if(GUILayout.Button("Clear"))
            {
                splineMesh.ClearAll();
            }
        }