Exemple #1
0
        void OnSceneGUI()
        {
            if (Event.current == null)
            {
                return;
            }
            Event e = Event.current;

            if (Tools.viewTool != ViewTool.Orbit && e.button == 0)
            {
                if (e.type == EventType.mouseMove || e.type == EventType.mouseDown || e.type == EventType.mouseUp || e.type == EventType.mouseDrag)
                {
                    Ray        worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    RaycastHit hitInfo;
                    if (Physics.Raycast(worldRay, out hitInfo, 10000, 1 << LayerMask.NameToLayer(MapBlockVisualizer.MAPBLOCK_LAYER)))
                    {
                        Vector3 position = hitInfo.point - hitInfo.collider.transform.position;

                        Terrain target = hitInfo.collider.GetComponent <Terrain>();

                        int x = (int)(position.x / MapBlockVisualizer.GRID_SIZE);
                        int y = (int)(position.z / MapBlockVisualizer.GRID_SIZE);

                        bool brushed = e.control == false;
                        if (e.type == EventType.mouseDown)
                        {
                            mapBlockVisualizer.OnBrushed(target, x, y, brushed);
                        }
                        else
                        {
                            if (Vector3.Distance(lastDragPosition, position) >= MapBlockVisualizer.GRID_SIZE / 2f)
                            {
                                if (e.type == EventType.mouseDrag)
                                {
                                    mapBlockVisualizer.OnBrushed(target, x, y, brushed);
                                }
                                else if (e.type == EventType.mouseMove)
                                {
                                    mapBlockVisualizer.OnBrushHover(target, x, y);
                                }
                                lastDragPosition = position;
                            }
                        }
                    }
                    else
                    {
                        mapBlockVisualizer.ClearLastHoveredBrush();
                    }
                }
            }


            int controlID = GUIUtility.GetControlID(FocusType.Passive);

            if (e.type == EventType.Layout)
            {
                HandleUtility.AddDefaultControl(controlID);
            }
        }