移动端实现手指缩放/单指长按/单指旋转

前言

unity 实现两个手指缩放功能有很多插件,比如easyTouch、FingerGestures、TouchKit等这些均为功能比较多插件,有时候单纯为了一个手指缩放的单一功能又没有必要导入插件,所以以下代码实现相应功能。

1.双指缩放

public float shrinkScale = 0.5f;

    private Vector2 initPostion1;
    private Vector2 initPostion2;

    private Vector2 tempPostion1;
    private Vector2 tempPostion2;

    private bool isInited;

    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 1)
        {
            //if(!isInited)
            //{
            //    initPostion1 = Input.GetTouch(0).position;
            //    initPostion2 = Input.GetTouch(1).position;
            //    isInited = true;
            //}
            if(Input.GetTouch(1).phase==TouchPhase.Began)
            {
                initPostion1 = Input.GetTouch(0).position;
                initPostion2 = Input.GetTouch(1).position;
            }

            if (Input.GetTouch(0).phase == TouchPhase.Moved||
                Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                tempPostion1 = Input.GetTouch(0).position;
                tempPostion2 = Input.GetTouch(1).position;

                Vector3 iniScale = GetComponent<RectTransform>().localScale;

                float initDis = Vector2.Distance(initPostion1, initPostion2);
                float tempDis = Vector2.Distance(tempPostion1, tempPostion2);
                float offset = tempDis - initDis;
                float offsetScale = (offset / initDis) * shrinkScale;//可加Time.delatTime
                Vector3 scale = new Vector3(iniScale.x + offsetScale,iniScale.y + offsetScale,1);

                GetComponent<RectTransform>().localScale = new Vector3(Mathf.Clamp(scale.x, 0.3f, 1),
                    Mathf.Clamp(scale.y, 0.3f, 1), 1);

                initPostion1 = tempPostion1;
                initPostion2 = tempPostion2;
            }
            //else
            //{
            //    isInited = false;
            //}
        }
    }

2.单指长按

private bool newTouch;
    private float newTouchTime;
    private float touchTime = 1f;




 if(Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            if(touch.phase == TouchPhase.Began)
            {
                newTouch = true;
                newTouchTime = Time.time;
            }
            else if(touch.phase == TouchPhase.Stationary)
            {
                if(Time.time-newTouchTime > touchTime && newTouch)
                {
                    //TODO:长按事件
                }
            }
            else
            {
                newTouch = false;
            }
        }

3.单指旋转

public GameObject GoToBeMoved;     
   private float speed = 1;




        if(Input.touchCount>0)
        {
            if(Input.touchCount==1)
            {
                if(Input.GetTouch(0).phase==TouchPhase.Moved)
                {
                    float vel = Input.GetAxis("Mouse X") * Time.deltaTime* speed;
                    GoToBeMoved.transform.Rotate(Vector3.up * vel,Space.World);
                }
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值