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

            }
        }
	}
}

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值