两个手指多点触摸 滑动 放大缩小 缩放

using UnityEngine;
using System.Collections;


public class FingersTwo : MonoBehaviour {
  public   GameObject  m1, m2;
  int Z_Far = 0, Z_Near;
  float sulv = 0.001f;
  Transform ziji;
  Vector3 zhongZhuang;
  Vector2 pianyi;
// Use this for initialization
void Start () {
        ///
        Z_Far = 10;
        Z_Near = -10;
        //
        m1.transform.localPosition = new Vector3(0, 0, 0);
        m2.transform.localPosition = new Vector3(0, 0, 0);
        pianyi.x= Screen.width/2;
        pianyi.y = Screen.height/2;
        ziji = this.transform;
        zhongZhuang = ziji.position;
}

// Update is called once per frame
    void Update()
    {
        ziji = this.transform;
        if (Input.touches.Length > 1)
        {
            if (Input.touches[0].fingerId == 0)
            {
                m1.transform.localPosition = Input.touches[0].position - pianyi;
            }
            if (Input.touches[1].fingerId == 1)
            {
                m2.transform.localPosition = Input.touches[1].position - pianyi;
            }
            //m1.transform.localPosition = Input.touches[0].position - pianyi;
            //m2.transform.localPosition = Input.touches[1].position - pianyi;
        }
        else if (Input.touches.Length == 1)
        {
            if (Input.touches[0].fingerId == 0)
            {
                m1.transform.localPosition = Input.touches[0].position - pianyi;
            }
            m2.transform.localPosition = Input.touches[1].position - pianyi;
           // m1.transform.localPosition = Input.touches[0].position - pianyi;
        }
        else
        {
            m1.transform.localPosition = new Vector3(0, 0, 0);
            m2.transform.localPosition = new Vector3(0, 0, 0);
        }
        //if (Input.touches[0].phase == TouchPhase.Began )
        //{


        //}


        if (Input.touches[1].phase == TouchPhase.Began)
        {
            start_dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
        }
        if (Input.touches.Length < 2 || (Input.touches[1].phase == TouchPhase.Stationary && Input.touches[0].phase == TouchPhase.Stationary)) 
        { 
            start_dis = -1; 
            start_dis = Vector2.Distance(Input.touches[0].position, Input.touches[1].position); 
        }
        /// 第一个方法


        //if (start_dis > 0)
        //{


        //    dis_C = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
        //    dis_B = Vector3.Distance(m1.transform.localPosition, m2.transform.localPosition);
        //    //确定为两个手指按下
        //    if (dis_B > start_dis)
        //    {
        //        if (ziji.localPosition.z < Z_Far)
        //        {
        //            this.transform.Translate(new Vector3(0, 0, 0.1f));
        //        }
        //    }
        //    else if (dis_B < start_dis)
        //    {
        //        if (ziji.localPosition.z > Z_Near)
        //            //this.transform.localPosition += new Vector3(0, 0, -0.1f);


        //            this.transform.Translate(new Vector3(0, 0, -0.1f));
        //    }
        //}


        /// 第二个方法
        if (start_dis > 0)
        {


          //  dis_C = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
            dis_B = Vector3.Distance(Input.touches[0].position, Input.touches[1].position) - start_dis;
           // dis_B = Mathf.Clamp(dis_B, Z_Near, Z_Far);
            //确定为两个手指按下
            if (ziji.localPosition.z <= Z_Far && ziji.localPosition.z >= Z_Near)
                zhongZhuang += new Vector3(0, 0, dis_B * sulv);


            if (zhongZhuang.z > Z_Far)
                zhongZhuang = new Vector3(0, 0, Z_Far);
            else if (zhongZhuang.z < Z_Near)
                zhongZhuang = new Vector3(0, 0, Z_Near);


            ziji.localPosition = zhongZhuang;


            //if (dis_B > start_dis)
            //{
            //    if (ziji.localPosition.z < Z_Far)
            //    {
            //        this.transform.Translate(new Vector3(0, 0, 0.1f));
            //    }
            //}
            //else if (dis_B < start_dis)
            //{
            //    if (ziji.localPosition.z > Z_Near)
            //        //this.transform.localPosition += new Vector3(0, 0, -0.1f);


            //        this.transform.Translate(new Vector3(0, 0, -0.1f));
            //}
        }


}
    float start_dis = 0, dis_B, dis_C;
    void OnGUI()
    {
        if (GUI.Button(new Rect(100, 100, 200, 80), "1:" + m1.transform.localPosition)) { }
        if (GUI.Button(new Rect(100, 200, 200, 80), "2:" + m2.transform.localPosition)) { }
        if (GUI.Button(new Rect(100, 300, 200, 80), "3:" + Input.touches[0].position)) {
            //真实位置
        }
        if (GUI.Button(new Rect(100, 400, 200, 80), "4:" + Input.touches[0].deltaPosition)) {
        //运动方向
        }
        if (GUI.Button(new Rect(100, 500, 200, 80), "5:" + Input.touches[0].rawPosition)) {
        //初始点位置
        }
        if (GUI.Button(new Rect(100, 600, 200, 80), "6:" + Input.touches[0].fingerId + " phase: " + Input.touches[1].phase))
        {
             
        }
        if (GUI.Button(new Rect(400, 100, 200, 80), "7:" + Input.touches[1].fingerId + " now:  " + ziji.localPosition.z + " \n start_dis: " + start_dis + " dis_B: " + dis_B))
        {
           
            //if (GUI.Button(new Rect(400, 200, 200, 80), "8:" + dis_B + " dis_C: " + dis_C))
            //{


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值