如何在 unity 2D 和 3D 中缩放以及点击屏幕

发表于2016-06-16
评论0 4.1k浏览
目标
  下面的示例代码的主要目的是为了解释如何在 Unity 中实现缩放即以点击屏幕的功能。

一、介绍
  在示例代码中,实现了放大或缩小和点击功能。在手机的图库中,缩放和平移/拖动图像时,它具有相同的行为。此示例代码对 unity2d 和 unity3d 对象都起作用。它独立于摄像机;无论主摄像机是 tk2d 摄像机或简单的摄像机以及它的投影类型是正交或是透视。

二、示例
  下面的 c# 代码实现的是,你可以通过两个手指来放大和缩小屏幕以及在图片上点击移动,注意:在你想要实现缩放功能的游戏物体上添加下面的脚本。
1、 PinchZoomScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using UnityEngine;
using System.Collections;
   
public class PinchZoom : MonoBehaviour
{
    private float scale_factor= 0.07f; 
    private float MAXSCALE = 6.0f, MIN_SCALE = 0.6f; // zoom-in and zoom-out limits
    private bool isMousePressed;
    private Vector2 prevDist = new Vector2(0,0);
    private Vector2 curDist = new Vector2(0,0);
    private Vector2 midPoint = new Vector2(0,0);
    private Vector2 ScreenSize;
    private Vector3 originalPos;
    private GameObject parentObject;
      
    void Start ()
    {
        // Game Object will be created and make current object as its child (only because we can set virtual anchor point of gameobject and can zoom in and zoom out from particular position)
        parentObject = new GameObject("ParentObject");
        parentObject.transform.parent = transform.parent;
        parentObject.transform.position = new Vector3(transform.position.x*-1, transform.position.y*-1, transform.position.z);
        transform.parent = parentObject.transform;
   
        ScreenSize = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height));
        originalPos = transform.position;
        isMousePressed = false;
    }
    void Update ()
    {
        if(Input.GetMouseButtonDown(0))
            isMousePressed = true;
        else if(Input.GetMouseButtonUp(0))
            isMousePressed = false;
        // These lines of code will pan/drag the object around untill the edge of the image
        if(isMousePressed && Input.touchCount==1 && Input.GetTouch(0).phase == TouchPhase.Moved && (parentObject.transform.localScale.x > MIN_SCALE || parentObject.transform.localScale.y > MIN_SCALE))
        {
            Touch touch = Input.GetTouch(0);   
            Vector3 diff = touch.deltaPosition*0.1f;   
            Vector3 pos = transform.position + diff;
            if(pos.x > ScreenSize.x * (parentObject.transform.localScale.x-1))
                pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1);
            if(pos.x < ScreenSize.x * (parentObject.transform.localScale.x-1)*-1)
                pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1)*-1;
            if(pos.y > ScreenSize.y * (parentObject.transform.localScale.y-1))
                pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1);
            if(pos.y < ScreenSize.y * (parentObject.transform.localScale.y-1)*-1)
                pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1)*-1;
            transform.position = pos;
        }
        // On double tap image will be set at original position and scale
        else if(Input.touchCount==1 && Input.GetTouch(0).phase == TouchPhase.Began && Input.GetTouch(0).tapCount==2)
        {
            parentObject.transform.localScale = Vector3.one;
            parentObject.transform.position = new Vector3(originalPos.x*-1, originalPos.y*-1, originalPos.z);
            transform.position = originalPos;
        }   
        checkForMultiTouch();
    }
    // Following method check multi touch
    private void checkForMultiTouch()
    {
        // These lines of code will take the distance between two touches and zoom in - zoom out at middle point between them
        if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
        {
            midPoint = new Vector2(((Input.GetTouch(0).position.x + Input.GetTouch(1).position.x)/2), ((Input.GetTouch(0).position.y + Input.GetTouch(1).position.y)/2));
            midPoint = Camera.main.ScreenToWorldPoint(midPoint);
              
            curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
            prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
            float touchDelta = curDist.magnitude - prevDist.magnitude;
            // Zoom out
            if(touchDelta>0)
            {
                if(parentObject.transform.localScale.x < MAXSCALE && parentObject.transform.localScale.y < MAXSCALE)
                {
                    Vector3 scale = new Vector3(parentObject.transform.localScale.x + scale_factor, parentObject.transform.localScale.y + scale_factor, 1);
                    scale.x = (scale.x > MAXSCALE) ? MAXSCALE : scale.x;
                    scale.y = (scale.y > MAXSCALE) ? MAXSCALE : scale.y;
                    scaleFromPosition(scale,midPoint);
                }
            }
            //Zoom in
            else if(touchDelta<0)
            {
                if(parentObject.transform.localScale.x > MIN_SCALE && parentObject.transform.localScale.y > MIN_SCALE)
                {
                    Vector3 scale = new Vector3(parentObject.transform.localScale.x + scale_factor*-1, parentObject.transform.localScale.y + scale_factor*-1, 1);
                    scale.x = (scale.x < MIN_SCALE) ? MIN_SCALE : scale.x;
                    scale.y = (scale.y < MIN_SCALE) ? MIN_SCALE : scale.y;
                    scaleFromPosition(scale,midPoint);
                }
            }
        }
        // On touch end just check whether image is within screen or not
        else if (Input.touchCount == 2 && (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(1).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Canceled))
        {
            if(parentObject.transform.localScale.x < 1 || parentObject.transform.localScale.y < 1)
            {
                parentObject.transform.localScale = Vector3.one;
                parentObject.transform.position = new Vector3(originalPos.x*-1, originalPos.y*-1, originalPos.z);
                transform.position = originalPos;
            }
            else
            {
                Vector3 pos = transform.position;
                if(pos.x > ScreenSize.x * (parentObject.transform.localScale.x-1))
                    pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1);
                if(pos.x < ScreenSize.x * (parentObject.transform.localScale.x-1)*-1)
                    pos.x = ScreenSize.x * (parentObject.transform.localScale.x-1)*-1;
                if(pos.y > ScreenSize.y * (parentObject.transform.localScale.y-1))
                    pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1);
                if(pos.y < ScreenSize.y * (parentObject.transform.localScale.y-1)*-1)
                    pos.y = ScreenSize.y * (parentObject.transform.localScale.y-1)*-1;
                transform.position = pos;
            }
        }
    }
    //Following method scales the gameobject from particular position
    static Vector3 prevPos = Vector3.zero;
    private void scaleFromPosition(Vector3 scale, Vector3 fromPos)
    {
        if(!fromPos.Equals(prevPos))
        {
            Vector3 prevParentPos = parentObject.transform.position;
            parentObject.transform.position = fromPos;   
            Vector3 diff = parentObject.transform.position - prevParentPos;
            Vector3 pos = new Vector3(diff.x/parentObject.transform.localScale.x*-1, diff.y/parentObject.transform.localScale.y*-1, transform.position.z);
            transform.localPosition = new Vector3(transform.localPosition.x + pos.x, transform.localPosition.y+pos.y, pos.z);
        }
        parentObject.transform.localScale = scale;
        prevPos = fromPos;
    }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引