您好,欢迎来到Unity之家!   unity.jb51.net 
  • 首 页
  • 你问我答
  • 当前位置:首页 > 学习培训 > Unity编程 >
    Android触摸事件响应
    时间:2015-01-27 09:43 来源:Unity之家 作者:unity.jb51.net 浏览:收藏 挑错 推荐 打印


    我们来看下在手机上面实现多点触屏


    直接贴出代码详情:



    //首先在start()函数里面允许多手指触屏
            void Start() { 
                      //允许多点触屏
                     Input.multiTouchEnabled=true; 
             }
    //判断当前触摸屏幕的手指个数 该函数可自定义 然后在Update()里面调用 也可以直接在Update()里面写
            if(Input.touchCount <= 0) {        return;        }
            if(Input.touchCount == 1)//一个手指触摸屏幕
                    {if(Input.touches[0].phase == TouchPhase.Began)//开始触屏
                            {
                                    m_screenpos=Input.touches[0].position;//自定义的二维坐标向量 记录初始触屏位置
                            }else if(Input.touches[0].phase == TouchPhase.Moved)//手指移动
                            {   
                                    //使物体旋转
                                    this.transform.Rotate(new Vector3(-Input.touches[0].deltaPosition.y*0.5f,Input.touches[0].deltaPosition.x*0.5f,0),Space.World);
                            }
                    }
            //当有多个手指触屏时 
            else if(Input.touchCount > 1)//当有多个手指触屏 
             {
                     //记录两个手指的位置         
                            Vector2 finger1= new Vector2();
                             Vector2 finger2= new Vector2();
                     //记录两个手指的移动距离 
                             Vector2 mov1=new Vector2();
                             Vector2 mov2=new Vector2();
                     for (int i=0;i<2;i++)//用循环来实现记录position
                     { 
                             Touch touch = Input.touches[i]; [/i]//记录第0个、第1个触屏点的状态
                             if(touch.phase == TouchPhase.Ended) break; //如果手指触屏之后离开就break
                             if(touch.phase == TouchPhase.Moved)// 当手指移动时
                             { 
                                     float mov =0; // 用来记录移动增量
                                     if (i == 0) {
                                             finger1=touch.position;
                                             mov1=touch.deltaPosition; 
                                     }else {
                                             finger2=touch.position;
                                             mov2=touch.deltaPosition;
                                     if(finger1.x > finger2.x){
                                                     mov=mov1.x; 
                                             }else { mov=mov2.x; }//比较两个手指在x轴上移动的距离 取其较大者
                                    if(finger1.y>finger2.y)
                                                    { mov+=mov1.y; }
                                            else { mov+=mov2.y; }//加上在y轴上移动的较大的增量
                                            Camera.main.transform.Translate(0,0,mov*Time.deltaTime);//移动相机 在z轴上变化 
                                             }
                                     }
                             }
                     }


    (责任编辑:脚印)
    免责声明:Unity之家部分内容来源于互联网,如有侵权,请联系我们,本站将立即进行处理。