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

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

(一)双指缩放

    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().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().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;
            //}
        }
    }

 (二)单指长按

    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;
            }
        }

 (三)单指旋转

    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);
                }
            }
        }

 

转载于:https://www.cnblogs.com/llstart-new0201/p/8821614.html

你可能感兴趣的:(移动端实现手指缩放/单指长按/单指旋转)