tech/metaverse

21/08/18 Unity 2D Tutorial About How To Zoom In And Zoom Out Game View With Finger Pinch On Android Device.

tech-lover 2021. 8. 18. 17:10

https://youtu.be/R5yoorghduo

- zoomInOut

 

```

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class zoomInOut : MonoBehaviour {

 

    Camera mainCamera;

 

    float touchesPrevPosDifferencetouchesCurPosDifferencezoomModifier;

 

    Vector2 firstTouchPrevPossecondTouchPrevPos;

 

    [SerializeField]

    float zoomModifierSpeed = 0.1f;

 

    // Use this for initialization

    void Start () {

        mainCamera = GetComponent<Camera> ();

    }

    

    // Update is called once per frame

    void Update () {

        

        if (Input.touchCount == 2) {

            Touch firstTouch = Input.GetTouch (0);

            Touch secondTouch = Input.GetTouch (1);

 

            firstTouchPrevPos = firstTouch.position - firstTouch.deltaPosition;

            secondTouchPrevPos = secondTouch.position - secondTouch.deltaPosition;

 

            touchesPrevPosDifference = (firstTouchPrevPos - secondTouchPrevPos).magnitude;

            touchesCurPosDifference = (firstTouch.position - secondTouch.position).magnitude;

 

            zoomModifier = (firstTouch.deltaPosition - secondTouch.deltaPosition).magnitude * zoomModifierSpeed;

 

            if (touchesPrevPosDifference > touchesCurPosDifference)

                mainCamera.orthographicSize += zoomModifier;

            if (touchesPrevPosDifference < touchesCurPosDifference)

                mainCamera.orthographicSize -= zoomModifier;

            

        }

 

        mainCamera.orthographicSize = Mathf.Clamp (mainCamera.orthographicSize2f10f);

 

    }

}

```