unity手機端ui的跟隨手指移動以及雙手縮放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

//隨意掛載
public class MapController : MonoBehaviour, IDragHandler, IPointerDownHandler
{   
    [SerializeField] private Transform image;//所要控制的UI
    public RectTransform canvas;//所在canvas

    private RectTransform curRecTran;
    private void Start()
    {
        curRecTran = transform.GetComponent<RectTransform>();
    }


    Vector3 offet;
    /// <summary>
    /// 記錄初始手的位置以及地圖中心的向量差
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerDown(PointerEventData eventData)
    {
        if (Input.touchCount == 1)
        {
            Vector3 mouseDown;
            //屏幕座標轉世界座標
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(curRecTran, eventData.position,
    eventData.pressEventCamera, out mouseDown))
            {
                offet = curRecTran.position - mouseDown;
            }

        }
    }
/// <summary>
/// 移動
/// </summary>
/// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData)
    {
        if (Input.touchCount == 1)
        {
            if (RectTransformUtility.RectangleContainsScreenPoint(canvas, eventData.position))
            {
                Vector3 globalMousePos;
                if (RectTransformUtility.ScreenPointToWorldPointInRectangle(curRecTran, eventData.position,
        eventData.pressEventCamera, out globalMousePos))

                {
                    curRecTran.position = globalMousePos + offet;
                }
            }

        }

    }


    //放大縮小
    private bool isTwoTouch;
    private Vector3 firstTouch;
    private Vector3 secondTouch;
    private float twoTouchDistance;
    private float lastDistance;
    void Update()
    {

        //縮放代碼
        if (Input.touchCount > 1)
        {
            //當第二根手指按下的時候
            if (Input.GetTouch(1).phase == TouchPhase.Began)
            {
                isTwoTouch = true;
                //獲取第一根手指的位置
                firstTouch = Input.touches[0].position;
                //獲取第二根手指的位置
                secondTouch = Input.touches[1].position;

                lastDistance = Vector2.Distance(firstTouch, secondTouch);
            }

            //如果有兩根手指按下
            if (isTwoTouch)
            {
                //每一幀都得到兩個手指的座標以及距離
                firstTouch = Input.touches[0].position;
                secondTouch = Input.touches[1].position;
                twoTouchDistance = Vector2.Distance(firstTouch, secondTouch);

                //當前圖片的縮放
                Vector3 curImageScale = new Vector3(image.localScale.x, image.localScale.y, 1);
                //兩根手指上一幀和這幀之間的距離差
                //因爲100個像素代表單位1,把距離差除以100看縮放幾倍
                float changeScaleDistance = (twoTouchDistance - lastDistance) / 100;
                //因爲縮放 Scale 是一個Vector3,所以這個代表縮放的Vector3的值就是縮放的倍數
                Vector3 changeScale = new Vector3(changeScaleDistance, changeScaleDistance, 0);
                //圖片的縮放等於當前的縮放加上 修改的縮放
                image.localScale = curImageScale + changeScale;
                //控制縮放級別
                image.localScale = new Vector3(Mathf.Clamp(image.localScale.x, 0.5f, 10f), Mathf.Clamp(image.localScale.y, 0.5f, 10f), 1);
                //這一幀結束後,當前的距離就會變成上一幀的距離了
                lastDistance = twoTouchDistance;
            }

            //當第二根手指結束時(擡起)
            if (Input.GetTouch(1).phase == TouchPhase.Ended)
            {
                isTwoTouch = false;
                firstTouch = Vector3.zero;
                secondTouch = Vector3.zero;
            }
        }

    }


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章