/// <summary> /// Updates the tooltip position. /// </summary> public virtual void UpdatePositionAndPivot() { if (this.m_Canvas == null) { return; } // Update the tooltip position to the mosue position // If the tooltip is not anchored to a target // Anchored position should be updated after updating the pivot if (this.m_AnchorToTarget == null) { // Convert the offset based on the pivot Vector2 pivotBasedOffset = new Vector2(((this.m_Rect.pivot.x == 1f) ? (this.m_Offset.x * -1f) : this.m_Offset.x), ((this.m_Rect.pivot.y == 1f) ? (this.m_Offset.y * -1f) : this.m_Offset.y)); Vector2 localPoint; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.m_Canvas.transform as RectTransform, Input.mousePosition, this.uiCamera, out localPoint)) { this.m_Rect.anchoredPosition = pivotBasedOffset + localPoint; } } // Update the tooltip pivot this.UpdatePivot(); // Check if we are anchored to a rect if (this.m_AnchorToTarget != null) { // Set the anchor position to the opposite of the tooltip's pivot Vector3[] targetWorldCorners = new Vector3[4]; this.m_AnchorToTarget.GetWorldCorners(targetWorldCorners); // Convert the tooltip pivot to corner Corner pivotCorner = UITooltip.VectorPivotToCorner(this.m_Rect.pivot); // Get the opposite corner of the pivot corner Corner oppositeCorner = UITooltip.GetOppositeCorner(pivotCorner); // Convert the offset based on the pivot Vector2 pivotBasedOffset = new Vector2(((this.m_Rect.pivot.x == 1f) ? (this.m_AnchoredOffset.x * -1f) : this.m_AnchoredOffset.x), ((this.m_Rect.pivot.y == 1f) ? (this.m_AnchoredOffset.y * -1f) : this.m_AnchoredOffset.y)); Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(this.uiCamera, targetWorldCorners[(int)oppositeCorner]); Vector2 localPoint; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.m_Canvas.transform as RectTransform, screenPoint, this.uiCamera, out localPoint)) { this.m_Rect.anchoredPosition = pivotBasedOffset + localPoint; } } }
/// <summary> /// Updates the pivot. /// </summary> public void UpdatePivot() { // Get the mouse position Vector3 targetPosition = Input.mousePosition; // Determine which corner of the screen is closest to the mouse position Vector2 corner = new Vector2( ((targetPosition.x > (Screen.width / 2f)) ? 1f : 0f), ((targetPosition.y > (Screen.height / 2f)) ? 1f : 0f) ); // Set the pivot this.SetPivot(UITooltip.VectorPivotToCorner(corner)); }
/// <summary> /// Updates the tooltip position. /// </summary> public virtual void UpdatePositionAndPivot() { // Update the tooltip position to the mosue position // If the tooltip is not anchored to a target // Anchored position should be updated after updating the pivot if (this.m_AnchorToTarget == null) { // Convert the offset based on the pivot Vector3 pivotBasedOffset = new Vector3(((this.m_Rect.pivot.x == 1f) ? (this.m_Offset.x * -1f) : this.m_Offset.x), ((this.m_Rect.pivot.y == 1f) ? (this.m_Offset.y * -1f) : this.m_Offset.y), 0f); // Update the position including the offset this.m_Rect.position = pivotBasedOffset + Input.mousePosition; } // Update the tooltip pivot this.UpdatePivot(); // Check if we are anchored to a rect if (this.m_AnchorToTarget != null) { // Set the anchor position to the opposite of the tooltip's pivot Vector3[] targetWorldCorners = new Vector3[4]; this.m_AnchorToTarget.GetWorldCorners(targetWorldCorners); // Convert the tooltip pivot to corner Corner pivotCorner = UITooltip.VectorPivotToCorner(this.m_Rect.pivot); // Get the opposite corner of the pivot corner Corner oppositeCorner = UITooltip.GetOppositeCorner(pivotCorner); // Convert the offset based on the pivot Vector3 pivotBasedOffset = new Vector3(((this.m_Rect.pivot.x == 1f) ? (this.m_AnchoredOffset.x * -1f) : this.m_AnchoredOffset.x), ((this.m_Rect.pivot.y == 1f) ? (this.m_AnchoredOffset.y * -1f) : this.m_AnchoredOffset.y), 0f); // Update the position this.transform.position = pivotBasedOffset + targetWorldCorners[(int)oppositeCorner]; } }