【Unity】【Code】Unity+触摸屏 单点旋转,双点放大缩小

通用代码库
基于Unity5.6.0f

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

public class Touch : MonoBehaviour {

    public Text txt;
    public EventDispatcher evt;
    float distance = 0;//触控缩放的距离
    private float lastDist = 0;//用于计算触控缩放
    private float curDist = 0;//用于计算触控缩放
    int t;//判断缩放触控

    private Vector3 pos1;

    private Quaternion pos2;

    private void Start()
    {
        pos1 = this.gameObject.transform.localScale;
        pos2 = this.gameObject.transform.rotation;
    }


    // Update is called once per frame
    void Update()
    {

        //单点触摸, 水平上下旋转  
        if (1 == Input.touchCount)
        {

            var touch = Input.GetTouch(0);
            ShowTxt("touch: " + touch.position);
            if (touch.position.y > Screen.height * 0.25f && 
                touch.position.y < Screen.height * 0.93f)
            {
                Vector2 deltaPos = touch.deltaPosition;
                transform.Rotate(Vector3.down * deltaPos.x * 0.05f, Space.World);
                transform.Rotate(Vector3.right * deltaPos.y * 0.05f, Space.World);
            }

        }

        //两点以上触控,且触控点发生移动
        if ((Input.touchCount > 1) && (Input.GetTouch(0).phase == TouchPhase.Moved
                                       || Input.GetTouch(1).phase == TouchPhase.Moved))
        {
            var touch1 = Input.GetTouch(0); //第一根手指
            var touch2 = Input.GetTouch(1); //第二根手指
            ShowTxt("touch1: " + touch1.position);
            ShowTxt("touch2: " + touch2.position);
            if (touch1.position.y > Screen.height * 0.25f && 
                touch1.position.y < Screen.height * 0.93f &&
                touch2.position.y > Screen.height * 0.25f && 
                touch2.position.y < Screen.height * 0.93f)
            {
                curDist = Vector2.Distance(touch1.position, touch2.position);//两指间距
                //当手指移动时,重置起始距离为当前距离
                if (t == 0)
                {
                    lastDist = curDist;
                    t = 1;
                }
                distance = curDist - lastDist;
                if (this.gameObject.transform.localScale.x >= 1 && 
                        this.gameObject.transform.localScale.x <     this.gameObject.transform.localScale.x*20)
                {
                    this.gameObject.transform.localScale += Vector3.one * distance * 0.05f * Time.deltaTime;
                }
                else
                {
                    this.gameObject.transform.localScale = new Vector3(1, 1, 1);
                }
            }




            lastDist = curDist;
        }

        //没有触控事件
        if (Input.touchCount == 0)
            t = 0;
    }

    private void ShowTxt(string info)
    {
        txt.text = info + "\r\n" + txt.text;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值