Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What to attach my background drag touch script to (android)

Discussion in 'Input System' started by tree_arb, Feb 25, 2021.

  1. tree_arb

    tree_arb

    Joined:
    Dec 30, 2019
    Posts:
    319
    Hello,
    Using android I have a canvas w children like this:

    Code (CSharp):
    1.  
    2. Canvas>
    3.    -Image component with100% alpha (screen size) with touch script added to it.
    4.    -buttons
    5.    -buttons
    6.    -buttons
    If the buttons aren't pressed, then the background blank image picks up the touch and is used to calculate drag movement for a camera

    However the image must be enabled for it to work, and I assume a full size alpha image takes some performance. If not an image, what should I attach a script to that only needs to measure screen touch points?


    script attached to background blank image inside canvas

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class FixedTouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    5. {
    6.     [HideInInspector]
    7.     public Vector2 TouchDist;
    8.     [HideInInspector]
    9.     public Vector2 PointerOld;
    10.     [HideInInspector]
    11.     public int PointerId;
    12.     [HideInInspector]
    13.     public bool Pressed;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.      
    19.     }
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Pressed)
    24.         {
    25.             if (PointerId >= 0 && PointerId < Input.touches.Length)
    26.             {
    27.                 TouchDist = Input.touches[PointerId].position - PointerOld;
    28.                 PointerOld = Input.touches[PointerId].position;
    29.             }
    30.             else
    31.             {
    32.                 TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld;
    33.                 PointerOld = Input.mousePosition;
    34.             }
    35.         }
    36.         else
    37.         {
    38.             TouchDist = new Vector2();
    39.         }
    40.     }
    41.     public void OnPointerDown(PointerEventData eventData)
    42.     {
    43.         Pressed = true;
    44.         PointerId = eventData.pointerId;
    45.         PointerOld = eventData.position;
    46.  
    47.        // print("Pointer Down id= "+ eventData.pointerId);
    48.     }
    49.  
    50.     public void OnPointerUp(PointerEventData eventData)
    51.     {
    52.         if (eventData.pointerId == PointerId)
    53.         {
    54.             Pressed = false;
    55.         }
    56.         else
    57.         {
    58.             //print("Pointer up but NO MATCH");
    59.         }
    60.     }
    61. }
    edit: here is the game object in question, perhaps this is not as bad a performance as I thought? everything seems fine...I just dont need an "image" just a thing to pickup events

     
    Last edited: Feb 25, 2021