unity手指触摸放大缩小,旋转,双击,长按

模型放大缩小:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enlarge : MonoBehaviour {

    Vector2 oldPos1;
    Vector2 oldPos2;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if(Input.touchCount == 2)
        {
            if(Input .GetTouch(0).phase==TouchPhase.Moved||Input .GetTouch(1).phase == TouchPhase.Moved)
            {
                Vector2 newPos1 = Input.GetTouch(0).position;
                Vector2 newPos2 = Input.GetTouch(1).position;
                if (IsEnlarge(oldPos1, oldPos2, newPos1, newPos2))
                {
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale * 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                else
                {
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale / 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                oldPos1 = newPos1;
                oldPos2 = newPos2;
            }
        }
		
	}
    //手势判断
    bool IsEnlarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2)
    {
        float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (length1 < length2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

手指滑动模型旋转:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AiXiRotate : MonoBehaviour {

    float Xspeed = 150f;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButton(0))
        {
            if(Input.touchCount == 1)
            {
                if(Input.GetTouch(0).phase == TouchPhase.Moved)//触摸的一根手指滑动
                {
                    transform .Rotate(Vector3.up* Input.GetAxis("Mouse X") * (-Xspeed) * Time.deltaTime);
                }
            }
        }
	}
}

双击和长按手机屏幕:在这里必须得双击屏幕里的模型或长按模型使模型销毁

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

public class TouchTap : MonoBehaviour {

    private float touchTime;
    private bool newTouch=false ;
    public Text text;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0))
        {
            //从相机发出一条射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if(Physics.Raycast(ray,out hitInfo))
            {
                //if (hitInfo.collider.gameObject.name == "AiXi(Clone)")
                //{
                //    if (Input.GetMouseButtonDown(0))
                //    {
                //        newTouch = true;
                //        touchTime = Time.time;
                //        Debug.Log(newTouch);
                //    }
                //     if(Input.GetMouseButtonUp(0))
                //    {
                //        newTouch = false;
                //        Debug.Log(newTouch);
                //    }
                //}

                if (hitInfo.collider.gameObject.name == "AiXi(Clone)")
                {
                    //双击模型使模型销毁
                    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)//几个手指碰到屏幕&&刚点击屏幕
                    {
                        if (Input.GetTouch(0).tapCount == 2)
                        {
                            Destroy(hitInfo.collider.gameObject);
                        }

                    }
                    //长按
                    if (Input.touchCount == 1)
                    {
                        Touch touch = Input.GetTouch(0);
                        if (touch.phase == TouchPhase.Began)
                        {
                            newTouch = true;
                            touchTime = Time.time;
                        }
                        else if (touch.phase == TouchPhase.Stationary)//手指触摸屏幕但没有移动
                        {
                            if (newTouch == true && Time.time - touchTime > 1f)
                            {
                                newTouch = false;
                                Destroy(hitInfo.collider.gameObject);
                            }
                        }
                        else
                        {
                            newTouch = false;
                        }
                    }
                }

            }
        }
	}
}

 

你可能感兴趣的:(unity)