移动端旋转旋转缩放控制

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

public class rotateAndScale : MonoBehaviour {
    public GameObject car;//被控制对象
    public Vector3 minScale;//最小缩放值
    public Vector3 maxScale;//最大缩放值

    //AR调试用
    private float minValue;
    private float maxValue;
    public Text minText;
    public Text maxText;

    public GameObject[] select;
    public bool isTrue = true;
    public bool isOpen;

	private Touch oldTouch1;  //上次触摸点1(手指1)
	private Touch oldTouch2;  //上次触摸点2(手指2)


    void Update()
    {
        if (select[0].activeInHierarchy == true || select[1].activeInHierarchy == true || select[2].activeInHierarchy == true)
        {
            isOpen = false;
        }

        if (select[0].activeInHierarchy == false && select[1].activeInHierarchy == false && select[2].activeInHierarchy == false)
        {
            isOpen = true;
        }

        minValue = float.Parse(minText.text);
        maxValue = float.Parse(maxText.text);

        if (Input.touchCount <= 0)
        {
            return;
        }
        //水平上下旋转
        if (1 == Input.touchCount)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.position.y > Screen.height/4 || isOpen == true)
            {
                isTrue = true;
            }

            if(touch.position.y < Screen.height/4 && isOpen == false)
            {
                isTrue = false;
            }

            if (isTrue)
            {
                Vector2 deltaPos = touch.deltaPosition;
                car.transform.Rotate(Vector3.down * deltaPos.x/2, Space.World);//绕Y轴进行旋转

            }
        }

        //放大缩小
        Touch newTouch1 = Input.GetTouch(0);
        Touch newTouch2 = Input.GetTouch(1);
        //第2点刚开始接触屏幕, 只记录,不做处理
        if (newTouch2.phase == TouchPhase.Began)
        {
            oldTouch2 = newTouch2;
            oldTouch1 = newTouch1;
            return;
        }
        //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
        float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
        float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
        //两个距离之差,为正表示放大手势, 为负表示缩小手势
        float offset = newDistance - oldDistance;
        //放大因子, 一个像素按 0.01倍来算(100可调整)
        float scaleFactor = offset / 100f;
        Vector3 localScale = car.transform.localScale;
        Vector3 scale = new Vector3(localScale.x + scaleFactor,
            localScale.y + scaleFactor,
            localScale.z + scaleFactor);

        //在什么情况下进行缩放
        if (scale.x >= minValue && scale.y >= minValue && scale.z >= minValue
            && scale.x <= maxValue && scale.y <= maxValue && scale.z <= maxValue)
		{
            car.transform.localScale = scale;
		}
		//记住最新的触摸点,下次使用
		oldTouch1 = newTouch1;
		oldTouch2 = newTouch2;
	}
}

用于AR缩放

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

public class ImageControl : MonoBehaviour
{

    #region MyRegion

    [Header("------ 车比例 ------")]
    public Transform car;
    [Header("缩放速度")]
    public InputField newCarSizeLerpTime;
    float newTempCarSize = 1f;
    [Header("Lerp speed")]
    public float LerpSpeed = 6f;
    float tempRotation = 0;
    float tempDelta = 0;

    [Header("双指缩放速度")]
    float DoubleLerp = 0.5f;

    //限制缩放范围
    float minValue = 0.0006f;
    float maxValue = 10.8f;

    private Touch oldTouch1; 
    private Touch oldTouch2;

    Touch newTouch1;
    Touch newTouch2;
    Vector3 localScale;
    Vector3 scale;

    #endregion

    void Update()
    {
        FindGacPlane();
        newCarSizeChange();
        TouchControl();
    }

    #region Touch control
    /// <summary>
    /// 触摸控制
    /// </summary>
    void TouchControl()
    {
        tempRotation = Mathf.Lerp(tempRotation, tempDelta, Time.deltaTime * LerpSpeed);

        if (car != null)
        {
            car.Rotate(Vector3.down * tempRotation / 2, Space.World);//绕Z轴旋转
        }

        //旋转
        if (1 == Input.touchCount)
        {
            Touch touch = Input.GetTouch(0);
            Vector2 deltaPos = touch.deltaPosition;
            tempDelta = deltaPos.x;
            //car.Rotate(Vector3.down * deltaPos.x / 2, Space.World);//绕Z轴旋转
            //car.transform.Rotate(Vector3.down * deltaPos.x / 2, Space.World);//绕Y轴旋转
            //car.transform.Rotate(Vector3.right * deltaPos.y / 2, Space.World);//绕X轴旋转
        }
        else
        {
            tempDelta = 0;
        }

        //缩放
        if (Input.touchCount == 2)
        {
            newTouch1 = Input.GetTouch(0);
            newTouch2 = Input.GetTouch(1);
        }

        //记录第2点刚开始接触屏幕
        if (newTouch2.phase == TouchPhase.Began)
        {
            oldTouch2 = newTouch2;
            oldTouch1 = newTouch1;
            return;
        }

        //计算新旧两点间距离
        float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
        float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

        //计算距离之差
        float offset = newDistance - oldDistance;

        //放大因子
        float scaleFactor = offset / 100f;
        scale = new Vector3(localScale.x + scaleFactor,
            localScale.y + scaleFactor,
            localScale.z + scaleFactor);

        //缩放区间
        newTempCarSize = Mathf.Clamp(newTempCarSize, minValue, maxValue);

        //缩放操作
        if (newTempCarSize >= minValue && newTempCarSize <= maxValue)
        {
            newTempCarSize += scale.x * DoubleLerp;
        }

        //记录最新的触摸点
        oldTouch1 = newTouch1;
        oldTouch2 = newTouch2;

    }


    /// <summary>
    /// 车比例缩放动画
    /// </summary>
    void newCarSizeChange()
    {
        if (car != null)
        {
            car.localScale = Vector3.Lerp(car.localScale, new Vector3(newTempCarSize, newTempCarSize, newTempCarSize), Time.deltaTime * float.Parse(newCarSizeLerpTime.text));
        }
    }

    #endregion

    /// <summary>
    /// Find gac plane
    /// </summary>
    void FindGacPlane()
    {
        if (car == null)
        {
            GameObject temp = GameObject.FindWithTag("Player");
            if (temp != null)
            {
                car = temp.transform;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33174548/article/details/85251128
今日推荐