// Update is called once per frame private void Update() { if (!BuilderActionComponentUI.ActiveSlot) { return; } if (MouseBehaviour.Mode == MouseMode.Build) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray); foreach (RaycastHit hit in hits) { if (hit.transform.tag == Tags.Ground) { float heightOffset = 0.03f; float factor = 0.5f; Vector3 position = hit.point; position.y = hit.point.y + heightOffset; position.x = hit.point.x; position.z = hit.point.z; Vector3 snapPoint = new Vector3( (float)Math.Round(position.x / factor, 1, MidpointRounding.AwayFromZero) * factor, hit.point.y + heightOffset, (float)Math.Round(position.z / factor, 1, MidpointRounding.AwayFromZero) * factor ); BuildingSlotUI buildingSlotUI = BuilderActionComponentUI .ActiveSlot .GetComponent <BuildingSlotUI>(); // Set preview game object position GameObject preview = buildingSlotUI.GetPreview(); preview.transform.position = snapPoint; BuildingPreviewBehaviour buildPreviewBehaviour = preview.GetComponent <BuildingPreviewBehaviour>(); // Check for collision if it is ok to place the building here. if (buildPreviewBehaviour.IsConstructable()) { // Left click if (Input.GetMouseButtonDown(0)) { buildingSlotUI.Place(snapPoint); buildingSlotUI.Unselect(); } } } } // Right click if (Input.GetMouseButtonDown(1)) { this.ClearSlotSelection(); } } }
/** * <summary> * On pointer exits * </summary> */ public void OnPointerExit(PointerEventData data) { if (!BuilderActionComponentUI.ActiveSlot) { return; } GameObject preview = BuilderActionComponentUI .ActiveSlot .GetComponent <BuildingSlotUI>() .GetPreview(); BuildingPreviewBehaviour previewBehaviour = preview.GetComponent <BuildingPreviewBehaviour>(); previewBehaviour.SetConstructable(true); BuilderActionComponentUI.PreviewFollow = true; }
/** * <summary> * Make sure mouse action is not functional when it is hovering the UI. * ie: unit selection, zooming and etc... * </summary> */ public void OnPointerEnter(PointerEventData data) { if (!BuilderActionComponentUI.ActiveSlot) { return; } GameObject preview = BuilderActionComponentUI .ActiveSlot .GetComponent <BuildingSlotUI>() .GetPreview(); preview.transform.localPosition = Vector3.zero; BuildingPreviewBehaviour previewBehaviour = preview.GetComponent <BuildingPreviewBehaviour>(); previewBehaviour.SetConstructable(false); BuilderActionComponentUI.PreviewFollow = false; }