Exemple #1
0
 private void OnEnable()
 {
     _occupierCache = GetComponent <GridOccupier>();
 }
        private static void OnSceneGUI(SceneView _)
        {
            if (_mapGridInstanceTransform == default)
            {
                return;
            }
            var mapGrid = _mapGridInstanceTransform.GetComponent <MapGridData>();

            if (mapGrid == default)
            {
                return;
            }
            if (mapGrid.GridX == 0 || mapGrid.GridY == 0)
            {
                throw new ApplicationException($"GridX and GridY of MapGrid cannot be 0!");
            }

            if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy; // show a drag-add icon on the mouse cursor

                if (DraggedObject == default)
                {
                    DraggedObject        = (GameObject)DragAndDrop.objectReferences[0];
                    DraggedGridOccupier  = DraggedObject.GetComponent <GridOccupier>();
                    _initialDraggedGridX = DraggedGridOccupier.X;
                    _initialDraggedGridY = DraggedGridOccupier.Y;
                }

                var ray   = Camera.current.ScreenPointToRay(new Vector3(Event.current.mousePosition.x, Camera.current.pixelRect.height - Event.current.mousePosition.y, 0.0f));
                var plane = new Plane(_mapGridInstanceTransform.up, _mapGridInstanceTransform.position);
                if (plane.Raycast(ray, out var intersectionDistance))
                {
                    var pointOfIntersection = ray.origin + ray.direction * intersectionDistance;

                    DraggedGridX = Mathf.FloorToInt(pointOfIntersection.x / mapGrid.GridX);
                    DraggedGridY = Mathf.FloorToInt(pointOfIntersection.z / mapGrid.GridY);

                    var gridOccupier = DraggedObject.GetComponent <GridOccupier>();
                    if (gridOccupier != default)
                    {
                        gridOccupier.X = DraggedGridX;
                        gridOccupier.Y = DraggedGridY;
                    }
                }

                if (Event.current.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();
                    var newObj = (GameObject)PrefabUtility.InstantiatePrefab(DraggedObject);
                    newObj.transform.position = new Vector3(DraggedGridX * mapGrid.GridX, 0f, DraggedGridY * mapGrid.GridY);
                    var gridOccupier = newObj.GetComponent <GridOccupier>();
                    if (gridOccupier != default)
                    {
                        gridOccupier.X = DraggedGridX;
                        gridOccupier.Y = DraggedGridY;
                    }
                    Undo.RegisterCreatedObjectUndo(newObj, "Object dragged to scene");
                    Selection.activeGameObject = newObj;
                    DraggedObject       = default;
                    DraggedGridOccupier = default;
                }

                Event.current.Use();
            }
            else if (Event.current.type == EventType.DragExited)
            {
                if (DraggedGridOccupier != default)
                {
                    DraggedGridOccupier.X = _initialDraggedGridX;
                    DraggedGridOccupier.Y = _initialDraggedGridY;
                    DraggedObject         = default;
                    DraggedGridOccupier   = default;
                }
            }
        }