Unity中vuforia的地面识别(ground plane)的物体操作包括双指旋转,双指扩大缩小,单指拖动(美的项目)

using UnityEngine;
using Vuforia;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour
{
    private bool isSingleFinger = false;
    private Vector2 preSingleTouchPosition = Vector2.zero;
    private float beginTouchDistance = 0f;
    private Vector3 currentScale = Vector3.one;
    private Vector3 currentPosition = Vector3.zero;
    private Vector2 oldPosition1 = Vector2.zero;
    private Vector2 oldPosition2 = Vector2.zero;
    private Touch curSingleTouch;

    public GameObject PlaneFinder;
    public GameObject CubeOldParent;//挪出cube之前的父物体
    public GameObject CubeParent;//挪出cube之后的父物体

    void Awake()
    {
        //testText = GameObject.Find("Text").GetComponent();
        //PlaneFinder.GetComponent().enabled = true;//恢复光标

    }

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

    /// 
    /// 重置按钮
    /// 
    public void ReStart()
    {
        //SceneManager.LoadScene(0);

        PlaneFinder.SetActive(true);
        //PlaneFinder.GetComponent().enabled = true;//恢复光标

        this.GetComponent().enabled = false;
        this.transform.parent.SetParent(CubeOldParent.transform);   // 重置
        this.transform.parent.transform.localPosition = Vector3.zero;

    }
    // Update is called once per frame

    void Update()
    {
        if (this.GetComponent().enabled)
        {
            PlaneFinder.SetActive(false);  //禁用
            this.transform.parent.SetParent(CubeParent.transform);   // 不再生成cube

            //PlaneFinder.GetComponent().enabled = false;//隐藏光标

        }
        if (Input.touchCount == 1)
        {
            //PlaneFinder.GetComponent().enabled = false;//隐藏光标
            SingleTouch();
        }
        else if (Input.touchCount == 2)
        {
            DoubleTouch();
        }
        if (Input.GetMouseButtonUp(0))
        {
            isdo = true;
        }
    }

    public void SingleTouch()
    {

        Debug.Log("SingleTouch");
        //isSingleFinger = true;
        //Debug.Log("this.transform.position**************"+this.transform.position);
        //Debug.Log("this.transform.localPosition===========" + this.transform.localPosition);

        curSingleTouch = Input.GetTouch(0);
        if (Input.GetTouch(0).phase == TouchPhase.Began || !isSingleFinger)
        {
            //在开始触摸或者从两字手指放开回来的时候记录一下触摸的位置  
            preSingleTouchPosition = Input.GetTouch(0).position;
        }
        //if (Input.GetTouch(0).phase == TouchPhase.Moved)
        //{
        //    //var deltaposition = Input.GetTouch(0).deltaPosition;
        //    //transform.Translate(-deltaposition.x * 0.001f, 0f, -deltaposition.y * 0.001f);

        //}

        isSingleFinger = true;

    }
    public void DoubleTouch()
    {
        Debug.Log("DoubleTouch:" + Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position));
        if (isSingleFinger)
        {
            oldPosition1 = Input.GetTouch(0).position;
            oldPosition2 = Input.GetTouch(1).position;
            beginTouchDistance = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
            currentScale = transform.parent.transform.localScale;
            // currentPosition = transform.localPosition;
        }

        if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)//双指移动
        {
            Vector2 xiangliang1 = Input.GetTouch(0).position - oldPosition1;
            Vector2 xiangliang2 = Input.GetTouch(1).position - oldPosition2;
            //float ang = Vector2.Angle(xiangliang1, xiangliang2);
            float dir = Vector2.Dot(xiangliang1.normalized, xiangliang2.normalized);
            if (dir <= 1 && dir >= 0)//旋转
            {
                Debug.Log("dirdirdir11111111111:" + dir);
                transform.Rotate(Vector3.up, -Input.GetTouch(0).deltaPosition.x * 0.25f);
                preSingleTouchPosition = Input.GetTouch(0).position;
                isdo = false;
            }


            if (dir >= -1 && dir < 0f)//大小
            {
                Debug.Log("dirdirdir2222222222222:" + dir);
                float currentTouchDistance = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
                if (currentTouchDistance!=0)
                {
                    float tmpScale = (currentTouchDistance / beginTouchDistance - 1f) * 2f;//倍数
                    tmpScale += currentScale.x;
                    tmpScale = Mathf.Clamp(tmpScale, 0.2f, 3.5f);//xianzhi
                    this.transform.parent.transform.localScale = Vector3.one * tmpScale;
                    isdo = false;
                }
            }
           
        }
        isSingleFinger = false;
    }
    public static bool isdo;


}

主要问题就是两个双指的操作总是有冲突,因为还有一个单指拖动,所以总会有功能的混乱 


using UnityEngine;

public class Move : MonoBehaviour {
    public GameObject stage;
    public GameObject plane;
    public static  bool isone;
    public Camera camera;
    private  Vector3 pointA;
    public GameObject local;
    private Vector3 localpoint;
	void Start () {
        isone = true;
    }
	
	void Update () {
        if (!UIManager.isdo)
        {
            return;
        }
        if (Input.touchCount == 1)
        {
            Ray ray = camera.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 100, ~plane.layer))
            {

                //  Debug.Log(LayerMask.LayerToName);
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    pointA = hit.point;
                    localpoint = local.transform.position;
                }
                else if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    Vector3 dis = hit.point - pointA;
                    local.transform.position = localpoint + dis;
                }
            }

        }
    }
    public void A()  //鼠标点击调用
    {
        if (isone)
        {
            isone = false;
            plane.transform.position = stage.transform.position;
        }
    }
}

 

 

你可能感兴趣的:(Unity,unityAR,unityVuforia,unity功能操作记录,unity代码记录)