Search Unity

Scale with Screen Size not working with Anchor Position

Discussion in 'UGUI & TextMesh Pro' started by UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396, Apr 8, 2020.

  1. UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    Joined:
    Jan 9, 2017
    Posts:
    152
    This is a weird case. The canvas scaler is set to scale with screen size and my ui element's anchor position is not moving to where my mouse is clicked. But when I build it onto Android, it works fine. I can't seem to get it working in the editor.

    Code below works on android perfectly but not in editor:
    Code (CSharp):
    1.     // On pointer down. =============================================
    2.     // Move the joypad to the touched position within the zone and run its logic.
    3.     public virtual void OnPointerDown(PointerEventData eventData)
    4.     {
    5.         if (eventData.button != PointerEventData.InputButton.Left) { return; }
    6.  
    7.         // Get the touched position and move the joypad to it.
    8.         var offsetX = joyPadRectT.sizeDelta.x * joyPadRectT.localScale.x / 2;  // Pivot point adjustment.
    9.         var offsetY = joyPadRectT.sizeDelta.y * joyPadRectT.localScale.y / 2;
    10.         joyPadRectT.transform.position = new Vector2(eventData.position.x - offsetX, eventData.position.y - offsetY);
    11.  
    12.         // Set joystick raycast target to true.
    13.         joyStickImage.raycastTarget = true;
    14.  
    15.         // Invoke joystick press event.
    16.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.pointerEnterHandler);
    17.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.pointerDownHandler);
    18.  
    19.         // Invoke joystick dragging event.
    20.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.initializePotentialDrag);
    21.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.beginDragHandler);
    22.     }
    23.     // ==============================================================
    After testing for hours, code below sort of works in editor but not on android..:
    Code (CSharp):
    1.     // On pointer down. =============================================
    2.     // Move the joypad to the touched position within the zone and run its logic.
    3.     public virtual void OnPointerDown(PointerEventData eventData)
    4.     {
    5.         if (eventData.button != PointerEventData.InputButton.Left) { return; }
    6.  
    7.         // Get the touched position and move the joypad to it.
    8.         Vector2 movePos = eventData.position;
    9.         RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, Input.mousePosition, parentCanvas.worldCamera, out movePos);
    10.         var offsetX = joyPadRectT.sizeDelta.x * joyPadRectT.localScale.x / 2;  // Pivot point adjustment.
    11.         var offsetY = joyPadRectT.sizeDelta.y * joyPadRectT.localScale.y / 2;
    12.         joyPadRectT.anchoredPosition = parentCanvas.transform.TransformPoint(new Vector2(movePos.x - offsetX, movePos.y - offsetY));
    13.  
    14.         // Set joystick raycast target to true.
    15.         joyStickImage.raycastTarget = true;
    16.  
    17.         // Invoke joystick press event.
    18.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.pointerEnterHandler);
    19.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.pointerDownHandler);
    20.  
    21.         // Invoke joystick dragging event.
    22.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.initializePotentialDrag);
    23.         ExecuteEvents.Execute(joyStick, eventData, ExecuteEvents.beginDragHandler);
    24.     }
    25.     // ==============================================================