Пример #1
0
        public void OnSceneGUI()
        {
            bool    mousePosValid = false;
            Vector3 mousePos      = Vector3.zero;

            float radius = gizmo.brushSettings.brushSize / 2f;

            int controlId = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);

            Ray        ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
            RaycastHit hit;

            // TODO: raycast hit against layer
            //       see https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
            if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity))
            {
                mousePos      = hit.point;
                mousePosValid = true;

                ///
                /// process mouse events
                ///

                // control key pressed
                if (Event.current.control)
                {
                    // mouse wheel up/down changes the radius
                    if (Event.current.type == EventType.ScrollWheel)
                    {
                        // ctrl + shift + scroll = brush rotation
                        if (Event.current.shift)
                        {
                            int rotationStepSize = 10;
                            int rotationMin      = 0;   // TODO: find out of to get that from Range
                            int rotationMax      = 360; // TODO: find out of to get that from Range

                            // scroll up
                            if (Event.current.delta.y > 0)
                            {
                                gizmo.brushSettings.brushRotation += rotationStepSize;
                                if (gizmo.brushSettings.brushRotation > rotationMax)
                                {
                                    gizmo.brushSettings.brushRotation = rotationMin + rotationStepSize;
                                }
                                Event.current.Use();
                            }
                            // scroll down
                            else if (Event.current.delta.y < 0)
                            {
                                gizmo.brushSettings.brushRotation -= rotationStepSize;
                                if (gizmo.brushSettings.brushRotation < rotationMin)
                                {
                                    gizmo.brushSettings.brushRotation = rotationMax - rotationStepSize;
                                }
                                Event.current.Use();
                            }
                        }
                        // ctrl + scroll = brush size
                        else
                        {
                            // scroll up
                            if (Event.current.delta.y > 0)
                            {
                                gizmo.brushSettings.brushSize++;
                                Event.current.Use();
                            }
                            // scroll down
                            else if (Event.current.delta.y < 0)
                            {
                                gizmo.brushSettings.brushSize--;

                                // TODO: slider
                                if (gizmo.brushSettings.brushSize < 1)
                                {
                                    gizmo.brushSettings.brushSize = 1;
                                }

                                Event.current.Use();
                            }
                        }
                    }
                }

                BrushMode brushMode = BrushMode.None;
                if (Event.current.shift)
                {
                    brushMode = BrushMode.Add;

                    if (Event.current.control)
                    {
                        brushMode = BrushMode.Remove;
                    }
                }

                // draw brush gizmo
                DrawBrush(mousePos, hit.normal, radius, brushMode);

                // paint prefabs on mouse drag
                if (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseDown)
                {
                    // left button = 0; right = 1; middle = 2
                    if (Event.current.button == 0)
                    {
                        switch (brushMode)
                        {
                        case BrushMode.None:
                            break;

                        case BrushMode.Add:
                            AddPrefabs(hit);
                            break;

                        case BrushMode.Remove:
                            RemovePrefabs(hit.point);
                            break;
                        }

                        Event.current.Use();
                    }
                }
            }
            else
            {
                mousePosValid = false;
            }

            if (Event.current.type == EventType.Layout)
            {
                HandleUtility.AddDefaultControl(controlId);
            }


            // examples about how to show ui info
            // note: Handles.BeginGUI and EndGUI are important, otherwise the default gizmos aren't drawn
            Handles.BeginGUI();


            if (mousePosValid)
            {
                ShowHandleInfo(mousePos);
            }

            string[] info = new string[] { "Add prefabs: shift + drag mouse\nRemove prefabs: shift + ctrl + drag mouse\nBrush size: ctrl + mousewheel, Brush rotation: ctrl + shift + mousewheel", "Children: " + editor.getContainerChildren().Length };
            PrefabPainterEditor.ShowGuiInfo(info);

            Handles.EndGUI();
        }