/** * Finalize spawning of objects at the end of a drag and drop. * * @param locationValid bool True if the cursor is over a valid location to place the object * @param hitInfo RaycastHit Result of the raycast done to test location validity */ private void FinalizeDrag(bool locationValid, RaycastHit primaryHitInfo, List <RaycastHit> secondaryHitInfo) { // hide any visualizations that may have existed for the object PlacementVisualizationManager.Instance.DisplayVisualization(spawnedObjectDragAndDrop.GetType(), false); // if we are in a valid place, spawn the object if (locationValid) { // place the spawned object spawnedObjectDragAndDrop.Place(primaryHitInfo, secondaryHitInfo); // make it visible and turn it on spawnedObject.SetActive(true); // set the layer back to whatever it was before Layers.MoveAllToLayer(spawnedObject, spawnedObjectLayer); } // if not in a valid place, destroy the spawned object else { // destroy object Destroy(spawnedObject); // clear old references, reset values to default spawnedObject = null; spawnedObjectDragAndDrop = null; spawnedObjectOffsetTopLeft = Vector3.zero; spawnedObjectOffsetTopRight = Vector3.zero; spawnedObjectOffsetBottomLeft = Vector3.zero; spawnedObjectOffsetBottomRight = Vector3.zero; } }
/** * Handle beginning of a drag action on this object * * @param eventData The PointerEventData for the drag which allows us to reference the pointer position for UpdateDrag */ public void OnBeginDrag(PointerEventData eventData) { if (!gameManager.PlaceState) { return; } if (towerUI.CanAfford) { // Spawn the object // Keep it invisible and don't set position yet because we haven't actually figured out where it's going to go yet, // but we need to instantiate it so we can figure out whether spawnedObject = Instantiate(objectToSpawn); // Set the spawned object to be on the unplaced object layer, keeping its old layer for future use spawnedObjectLayer = spawnedObject.layer; Layers.MoveAllToLayer(spawnedObject, LayerMask.NameToLayer(Layers.IGNORE_RAYCAST_LAYER_NAME)); // Get the IDragAndDrop interface for the spawned object spawnedObjectDragAndDrop = spawnedObject.GetComponentInChildren <IDragAndDropObject>(); if (spawnedObjectDragAndDrop == null) { Debug.LogError("Trying to drag and drop an object with no IDragAndDrop interface implemented!"); return; } // Show any visualizations that may exist for the object PlacementVisualizationManager.Instance.DisplayVisualization(spawnedObjectDragAndDrop.GetType(), true); // Calculate the offsets needed to get the points at the corners of the spawned object's renderer's bounding box // NOTE: These are * 0.25f to allow for Anglers and Rangers to be placed closer to the shore of the river if (spawnedObjectDragAndDrop is MonoBehaviour mb && mb.GetComponent <MeshRenderer>().bounds is Bounds bounds) { spawnedObjectOffsetTopLeft = new Vector3(-1 * bounds.extents.x * 0.25f, 0, bounds.extents.z * 0.25f); spawnedObjectOffsetTopRight = new Vector3(bounds.extents.x * 0.25f, 0, bounds.extents.z * 0.25f); spawnedObjectOffsetBottomLeft = new Vector3(-1 * bounds.extents.x * 0.25f, 0, -1 * bounds.extents.z * 0.25f); spawnedObjectOffsetBottomRight = new Vector3(bounds.extents.x * 0.25f, 0, -1 * bounds.extents.z * 0.25f); } else { Debug.LogError("Attempting to spawn object from drag and drop that has no MeshRenderer!"); } // Start moving the icon UpdateDrag(eventData); }
/** * Handle beginning of a drag action on this object */ public void OnBeginDrag(PointerEventData eventData) { // spawn the object // keep it invisible and don't set position yet because we haven't actually figured out where it's going to go yet // but, we need to instantiate it so we can figure out whether spawnedObject = Instantiate(objectToSpawn); // set the spawned object to be on the unplaced object layer, keeping its old layer for future use spawnedObjectLayer = spawnedObject.layer; Layers.MoveAllToLayer(spawnedObject, LayerMask.NameToLayer(Layers.IGNORE_RAYCAST_LAYER_NAME)); // get the IDragAndDrop interface for the spawned object spawnedObjectDragAndDrop = spawnedObject.GetComponentInChildren <IDragAndDropObject>(); if (spawnedObjectDragAndDrop == null) { Debug.LogError("Trying to drag and drop an object with no IDragAndDrop interface implemented!"); } else { // show any visualizations that may exist for the object PlacementVisualizationManager.Instance.DisplayVisualization(spawnedObjectDragAndDrop.GetType(), true); } // calculate the offsets needed to get the points at the corners of the spawned object's renderer's bounding box MeshRenderer mr = (spawnedObjectDragAndDrop as MonoBehaviour).GetComponent <MeshRenderer>(); if (!mr) { Debug.LogError("Attempting to spawn object from drag and drop that has no MeshRenderer!"); } else { spawnedObjectOffsetTopLeft = new Vector3(-1 * mr.bounds.extents.x, mr.bounds.extents.y); spawnedObjectOffsetTopRight = new Vector3(mr.bounds.extents.x, mr.bounds.extents.y); spawnedObjectOffsetBottomLeft = new Vector3(-1 * mr.bounds.extents.x, -1 * mr.bounds.extents.y); spawnedObjectOffsetBottomRight = new Vector3(mr.bounds.extents.x, -1 * mr.bounds.extents.y); } // start moving the icon UpdateDrag(eventData); }