Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help - Add touch control to script

Discussion in 'Scripting' started by IvanNov3D, Dec 9, 2019.

  1. IvanNov3D

    IvanNov3D

    Joined:
    Aug 1, 2019
    Posts:
    5
    Hi,
    Here i found some easy and really good script to make image zoomable movable in scene canvas.
    It works great on PC but on mobile i cant zoom in and out with fingers, only drag the picture.
    Could anyone tell me how to fix it?
    Thank all.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class UIZoomImage : MonoBehaviour, IScrollHandler
    5. {
    6.     private Vector3 initialScale;
    7.  
    8.     [SerializeField]
    9.     private float zoomSpeed = 0.1f;
    10.     [SerializeField]
    11.     private float maxZoom = 10f;
    12.  
    13.     private void Awake()
    14.     {
    15.         initialScale = transform.localScale;
    16.     }
    17.  
    18.     public void OnScroll(PointerEventData eventData)
    19.     {
    20.         var delta = Vector3.one * (eventData.scrollDelta.y * zoomSpeed);
    21.         var desiredScale = transform.localScale + delta;
    22.  
    23.         desiredScale = ClampDesiredScale(desiredScale);
    24.  
    25.         transform.localScale = desiredScale;
    26.     }
    27.  
    28.     private Vector3 ClampDesiredScale(Vector3 desiredScale)
    29.     {
    30.         desiredScale = Vector3.Max(initialScale, desiredScale);
    31.         desiredScale = Vector3.Min(initialScale * maxZoom, desiredScale);
    32.         return desiredScale;
    33.     }
    34. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    In mobile, you need to make sure multitouch is enabled in the input settings and use Input.touches for this. Check threre are more, than one touch, take first two, calculate distance, remember it, and in the next frame do the same again. Compare new value with one you saved initially and use it in place of eventData.scrollDelta.y
     
  3. IvanNov3D

    IvanNov3D

    Joined:
    Aug 1, 2019
    Posts:
    5
    Thank you for answer.
    You mean i need add this in the script i show here?
    I am really sorry, i am new to unity so maybe my question is really fool)
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    First, you need to move the content of OnScroll method to separate method what accepts float delta parameter and call it from OnScroll using eventData.scrollDelta.y
    Second, you need to read touches in the Update method, calculate delta as I said, and then call that new method using calculated delta as it's argument.
    Your question is really simple, probably this will help https://unity3d.com/learning-c-sharp-in-unity-for-beginners
     
  5. IvanNov3D

    IvanNov3D

    Joined:
    Aug 1, 2019
    Posts:
    5
    palex-nx thank you!
    Maybe some one got ready script that adapted or any links to asset that can help me with this mission?