unity移动端操作物体旋转、缩放手势

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

public class SetRoaAndScal : MonoBehaviour
{
    public Transform TargetTransform;//要操作的物体
    public bool flag_Roable = true;//自动旋转标志
    public float xSpeed = 50f;
    public float ySpeed = 50f;
    //public float distance = 30f;
    private float x = 0f;
    private float y = 0f;
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;
    private System.DateTime oldTime;
    private System.DateTime nowTime;
    void Update()
    {
        nowTime = System.DateTime.Now;
        System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks);
        System.TimeSpan ts2 = new System.TimeSpan(nowTime.Ticks);

        System.TimeSpan ts = ts2.Subtract(ts1).Duration();
        if (ts.Seconds > 8 && !Input.anyKey)
        {
            flag_Roable = true;
            oldTime = System.DateTime.Now;
        }
        if (Input.anyKey)
        {
            if (Input.touchCount == 1)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    x = Input.GetAxis("Mouse X") * xSpeed;
                    y = Input.GetAxis("Mouse Y") * ySpeed;
                    TargetTransform.Rotate(Vector3.up * -x * Time.deltaTime, Space.Self);
                    TargetTransform.Rotate(Vector3.right * -y * Time.deltaTime, Space.Self);
                }
            }
            单点触摸, 水平上下移动
            //if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
            //{
            //    //EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)为true表示点到了UI
            //    if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            //    {
            //        var deltaposition = Input.GetTouch(0).deltaPosition;
            //        TargetTransform.Translate(-deltaposition.x * 0.1f, 0f, -deltaposition.y * 0.1f);
            //    }
            //}
            if (Input.touchCount > 1)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
                {
                    Vector2 tempPosition1 = Input.GetTouch(0).position;
                    Vector2 tempPosition2 = Input.GetTouch(1).position;
                    if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                    {
                        float oldScale = TargetTransform.localScale.x;
                        float newScale = oldScale * 1.025f;
                        TargetTransform.localScale = new Vector3(newScale, newScale, newScale);
                    }
                    else
                    {
                        float oldScale = TargetTransform.localScale.x;
                        float newScale = oldScale / 1.025f;
                        TargetTransform.localScale = new Vector3(newScale, newScale, newScale);
                    }
                    //备份上一次触摸点的位置,用于对比   
                    oldPosition1 = tempPosition1;
                    oldPosition2 = tempPosition2;
                }
            }
        }
    }
    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势   
        var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
            //放大手势   
            return true;
        }
        else
        {
            //缩小手势   
            return false;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值