Unity多點觸控 - 屏幕攝像機放大縮小

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class MobileInput : MonoBehaviour
{

    //距離最值  
    public float MaxDistance = 5F;
    public float MinDistance = 0.5F;
    //縮放速率  
    public float ZoomSpeedS = 0.1F;
    public float ZoomSpeedM = 0.05F;
    public float ZoomSpeedL = 0.03F;

    public float ZoomSpeed = 0.1F;
    //觀察目標  
    private Transform Target;

    [HideInInspector]
    public string str = "";

    //記錄上一次手機觸摸位置判斷用戶是在左放大仍是縮小手勢
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;

    private Vector3 _vec3TargetScreenSpace;// 目標物體的屏幕空間座標  
    private Vector3 _vec3TargetWorldSpace;// 目標物體的世界空間座標  
    private Vector3 _vec3MouseScreenSpace;// 鼠標的屏幕空間座標  
    private Vector3 _vec3Offset;// 偏移 

    private int downIndex = 0;
    private int dragIndex = 0;

    private Camera camera;
    private float Distance;

    public static MobileInput Instance;
    void Awake()
    {
        Target = GameObject.Find("Cube").transform;
        camera = this.gameObject.GetComponent<Camera>();
        Distance = camera.orthographicSize;
        Instance = this;

        ZoomSpeed = ZoomSpeedS;
    }

    void Start()
    {
        //容許多點觸控  
        Input.multiTouchEnabled = true;
    }
    public bool isLock = false;
    private int lastTouchNum = 0;
    private bool canMove = true;
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_WEBGL
    void Update()
    {

#if UNITY_WEBGL
        if (Input.touchCount > 0)
        {
            Debug.LogError("Input.touchCountL:"+ Input.touchCount);
            if(Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                {
                    Debug.LogError("MobileInput 點擊UI");
                    isLock = true;
                    return;
                }
            }
        }
        else
        {
            isLock = false;
        }

#endif
        if (isLock) return;

        if(Input.touchCount == 1 && Input.touches[0].phase == TouchPhase.Began)
        {
            canMove = true;
        }

        if (Input.touchCount > 1)
        {
            canMove = false;
        }

        if (canMove && Input.touchCount == 1 && Input.touches[0].phase == TouchPhase.Moved)
        {
            if (downIndex++ == 0)
            {
                // 把目標物體的世界空間座標轉換到它自身的屏幕空間座標 
                _vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(Target.position);

                // 存儲鼠標的屏幕空間座標(Z值使用目標物體的屏幕空間座標) 
                _vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);

                // 計算目標物體與鼠標物體在世界空間中的偏移量 
                _vec3Offset = Target.position - Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace);
            }

            // 存儲鼠標的屏幕空間座標(Z值使用目標物體的屏幕空間座標)
            _vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);

            // 把鼠標的屏幕空間座標轉換到世界空間座標(Z值使用目標物體的屏幕空間座標),加上偏移量,以此做爲目標物體的世界空間座標
            _vec3TargetWorldSpace = Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace) + _vec3Offset;

            // 更新目標物體的世界空間座標 
            Target.position = _vec3TargetWorldSpace;
        }
        else 
        {
            downIndex = 0;
        }
        if (Input.touchCount > 1)
        {
            //兩隻手指都處於移動狀態  
            if (Input.touches[0].phase == TouchPhase.Moved || Input.touches[1].phase == TouchPhase.Moved)
            {
                //計算移動方向  
                var tempPosition1 = Input.GetTouch(0).position;
                var tempPosition2 = Input.GetTouch(1).position;

                //根據向量的大小判斷當前手勢是放大仍是縮小  
                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2) == 3)
                {
                    Distance -= ZoomSpeed;
                }
                else if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2) == 1)
                {
                    Distance += ZoomSpeed;
                }

                if (dragIndex++ != 0)
                {
                    //限制距離  
                    Distance = Mathf.Clamp(Distance, MinDistance, MaxDistance);
                    Camera.main.orthographicSize = Distance;
                }

                //備份上一次觸摸點的位置,用於對比
                oldPosition1 = tempPosition1;
                oldPosition2 = tempPosition2;
            }
            else
            {
                dragIndex = 0;
            }
        }
    }

#endif

    //函數返回真爲放大,返回假爲縮小
    int isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        //函數傳入上一次觸摸兩點的位置與本次觸摸兩點的位置計算出用戶的手勢
        var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
            //放大手勢
            return 3;
        }
        if (leng1 == leng2)
        {
            return 2;
        }
        else
        {
            //縮小手勢
            return 1;
        }
    }

}
相關文章
相關標籤/搜索