Unity achieve zoom and pan position of the camera to achieve the effect of dragging

Zoom function is used in game development capabilities, today speaking about how to achieve zoom in Unity.

1.IDragHandler, IBeginDragHandler, IEndDragHandler three Unity interface is a common interface, corresponding to the drag, start dragging, you can drag the end (It should be noted that these three interfaces can only be used in UGUI (without adding Collider ), Sprite can not be used (the Sprite can OnMouseDown, onMouseDrag, OnMouseUp)), and drag the object needs to be Cavas following sub-elements.

Look at the simple drag and drop and drag the end of the beginning, drag the beginning only a simple judgment is not a single point of click, click of the recording location, drag the marker state is true. Dragging the drag mark inside end state flase.

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (Input.touchCount <= 1)
        {
            isDrag = true;
            lastPoint = Input.mousePosition;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
    }

 

Key to look at drag in this method: This method is always executed in the course of your press, basically each line has a comment, you should have a good understand

    public  void ondrag (PointerEventData eventDataA) 
    { 

        IF (isDrag && Input.touchCount <= . 1 ) 
        { 
            // record the current mouse position 
            currentPoint = Input.mousePosition;
             // record distance moved - number because we want to move the camera because display of moving objects
             // if they can not add moving objects - the principle is the same, a moving object can cause direct reconstruction, especially of a large number of objects (such as maps when it will happen), will be on performance a huge consumption 
            Movedir = - (currentPoint - LASTPOINT);
             // regular z-axis direction 
            Movedir = new new Vector3 (moveDir.x, moveDir.y, 0 ); 
            
            // if the moving distance of 0 then that no drag, return directly 
            IF (Movedir ==Vector3.zero) 
            { 
                return ; 
            } 

            // This section may refer https://www.jianshu.com/p/148725feecfa 
            // is mainly done for the screen and the camera movement distance of a moving distance consistent with the scaling does not change 
            float = Aspect eyeCamera.aspect;
             a float halfFOVTan Mathf.Tan = ((eyeCamera.fieldOfView * 0.5f ) * Mathf.Deg2Rad);
             a float Hight = Mathf.Abs (eyeCamera.transform.position.z) halfFOVTan * * 2 ; // reference image (calculated perspective camera viewport width and height schematic) 
            a float width Hight * = Aspect;
             // calculate the required movement distance of the camera is actually 
            moveDir.x = moveDir.x / screen.width *width; 
            moveDir.y = moveDir.y / screen.height * Hight; 
            
            // camera movement distance: a ride on the back of a moveDir following a scaling factor we are saying, if you do not add words will lead to shrink after moving more slowly that move fast after amplification Comparative 
            eyeCamera.transform.Translate (Movedir * Scale / 6.6f ); 

            IF (! = LASTPOINT currentPoint) 
            { 
                LASTPOINT = currentPoint; 
            } 
        } 
    }

2. Zoom

 
    // here because I used orthographic camera, the camera and orthogonal to the default size equal to 6.6     
   Private a float Scale = 6.6f;
public void ScaleView ()
{
// multi-touch, zoom (new record two touch points )
Touch newTouch1 = Input.GetTouch (0);
Touch newTouch2 = Input.GetTouch (1);
     // judgment is not canceled, or click on a new point or lifted directly out ScaleView method
for (int i = 0 ; I <Input.touchCount; I ++)
{
IF (Input.GetTouch (I) .phase == TouchPhase.Began || Input.GetTouch (I) .phase == TouchPhase.Canceled ||
Input.GetTouch (I) == TouchPhase.Ended .phase)
{
after // cancel the current point becomes a point of recording
lastTouch2 = newTouch2;
lastTouch1 = newTouch1;
return;
}
}

// calculate the distance between two points of the old and the new two-point distance becomes large to zoom model, the model becomes smaller scale to
a float lastDistance = Vector2.Distance (lastTouch1.position, lastTouch2.position);
a float newDistance = Vector2.Distance (newTouch1.position, newTouch2.position);

// distance difference between the two, an enlarged gesture is positive, negative to zoom out gesture
a float offset = lastDistance - newDistance;
// amplification factor, a 0.01-fold of pixels Operators (100 adjustable)
a float by scaleFactor = offset / the zoomFactor;
// current camera size
a float localScale = eyeCamera.orthographicSize;
// calculate an overall magnification
Scale = + localScale by scaleFactor;
IF (Scale <minScale)
{
= minScale Scale;
}
the else IF (Scale> the maxScale)
{
Scale = the maxScale;
}
// adjust the size of the camera
Scale2D.SetCameraOrthographicSize (Scale, eyeCamera);
// remember the latest touch point, the next use
lastTouch1 = newTouch1;
lastTouch2 newTouch2 =;
}

Guess you like

Origin www.cnblogs.com/txfd/p/11323734.html