多点触控的方法,两边的触控互不干扰;

主要采用Input.touches的相关属性进行操作;

而采用IPointerDrag接口会造成两个drag的相互干扰;

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class TouchFunc : MonoBehaviour
{
//左边的
Vector3 beginDragPos_Left;
Vector3 dragVector_Left;
Vector3 dragVectorOne_Left;
bool isDraging_Left; //右边的
Vector3 beginDragPos_Right;
Vector3 dragVector_Right;
Vector3 dragVectorOne_Right;
bool isDraging_Right; //控制的对象
Vector3 mousePosition_Current;
Vector3 mousePosition_Mobile; float mouseMoveSpeed;
GameObject moveCenter_Mobile;
GameObject aimCenter_Mobile; GameObject fireButton_Mobile;
public static bool isDragFromFireButton; //某点触控
void TouchPoint( int index)
{
//左边
if (Input.GetTouch(index).position.x < Screen.width / 2)
{
if (Input.GetTouch(index).phase == TouchPhase.Began)
{
beginDragPos_Left = Input.GetTouch(index).position;
}
if (Input.GetTouch(index).phase == TouchPhase.Moved)
{
dragVector_Left += new Vector3(Input.GetTouch(index).deltaPosition.x, Input.GetTouch(index).deltaPosition.y,0);
dragVectorOne_Left = dragVector_Left.normalized; isDraging_Left = true;
}
//右边
if (Input.GetTouch(index).phase == TouchPhase.Ended)
{
dragVector_Left = Vector3.zero;
dragVectorOne_Left = Vector3.zero;
isDraging_Left = false;
}
}
else
{
if (Input.GetTouch(index).phase == TouchPhase.Began)
{
beginDragPos_Right = Input.GetTouch(index).position;
}
if (Input.GetTouch(index).phase == TouchPhase.Moved)
{
dragVector_Right+= new Vector3(Input.GetTouch(index).deltaPosition.x, Input.GetTouch(index).deltaPosition.y, 0);
dragVectorOne_Right = dragVector_Right.normalized;
isDraging_Right = true;
}
if (Input.GetTouch(index).phase == TouchPhase.Ended)
{
dragVector_Right = Vector3.zero;
dragVectorOne_Right = Vector3.zero;
isDraging_Right = false;
}
}
} void TouchMethod()
{
if (Input.touchCount == 1) TouchPoint(0);
if (Input.touchCount == 2) { TouchPoint(0); TouchPoint(1); }
} void DragFrom()
{
float width = fireButton_Mobile.GetComponent<Image>().rectTransform.rect.size.x;
float height = fireButton_Mobile.GetComponent<Image>().rectTransform.rect.size.y;
Vector3 pos = fireButton_Mobile.transform.position;
if (beginDragPos_Right.x > pos.x - width / 2 && beginDragPos_Right.x < pos.x + width / 2 && beginDragPos_Right.y > pos.y - height / 2 && beginDragPos_Right.y < pos.y + height / 2)
{ isDragFromFireButton = true; }
else { isDragFromFireButton = false; } if (isDraging_Right) PlayerBehavior.Instance.fireButtonDown_Mobile = true;
else PlayerBehavior.Instance.fireButtonDown_Mobile = false;
}
//判断是否在拖动、修复判定bug
void StateJudge()
{
if (Input.touchCount == 0)
{
isDraging_Left = false;
isDraging_Right = false;
}
}
void RightDrag()
{
//if (isDragFromFireButton) mouseMoveSpeed = 0.5f;
//else mouseMoveSpeed = 1f; //瞄准按钮中心移动
if (isDraging_Right)
aimCenter_Mobile.transform.localPosition = Vector3.Lerp(aimCenter_Mobile.transform.localPosition, dragVectorOne_Right * Mathf.Clamp(dragVector_Right.magnitude, 0, 90), 0.2f);
if (!isDraging_Right)
aimCenter_Mobile.transform.localPosition = Vector3.Lerp(aimCenter_Mobile.transform.localPosition, Vector3.zero, 0.2f); //光标位置
if (isDraging_Right)
{
Vector3 tempVec = new Vector3(Screen.width / 2, Screen.height / 2, 0) + dragVector_Right * mouseMoveSpeed;
mousePosition_Mobile = new Vector3(Mathf.Clamp(tempVec.x, 0, Screen.width), Mathf.Clamp(tempVec.y, 0, Screen.height), 0);
}
if (!isDraging_Right)
{
mousePosition_Current = new Vector3(Screen.width / 2, Screen.height / 2, 0);
mousePosition_Mobile = Vector3.Lerp(mousePosition_Mobile, mousePosition_Current,0.1f);
} //把触控输入传给Player,以控制玩家转向和瞄准
PlayerBehavior.Instance.mousePosition_Mobile = mousePosition_Mobile;
}
//控制运动、运动按钮的动作
void LeftDrag()
{
//移动按钮中心移动
if (isDraging_Left)
moveCenter_Mobile.transform.localPosition = Vector3.Lerp(moveCenter_Mobile.transform.localPosition, dragVectorOne_Left * Mathf.Clamp(dragVector_Left.magnitude, 0, 90), 0.2f);
if (!isDraging_Left)
moveCenter_Mobile.transform.localPosition = Vector3.Lerp(moveCenter_Mobile.transform.localPosition, Vector3.zero, 0.2f);
//数据传入,以控制玩家移动
PlayerBehavior.Instance.moveVectorOne_Mobile = dragVectorOne_Left;
//是否移动中的判定
if (isDraging_Left && dragVector_Left.magnitude > 10) PlayerBehavior.Instance.isMoving_Mobile = true;
else PlayerBehavior.Instance.isMoving_Mobile = false; } // Start is called before the first frame update
void Start()
{
moveCenter_Mobile = GameObject.Find("MoveCenter_Mobile");
aimCenter_Mobile = GameObject.Find("AimCenter_Mobile");
mousePosition_Mobile = new Vector3(Screen.width / 2, Screen.height / 2, 0);
fireButton_Mobile = GameObject.Find("FireButton_Mobile");
//光标移动速度
mouseMoveSpeed = 1f; } // Update is called once per frame
void Update()
{
if (!GameManager.Instance.isInputMobileShow) return;
if (PlayerBehavior.Instance.leveUp || (!PlayerBehavior.Instance.isAlive)) return;
TouchMethod(); StateJudge();
RightDrag();
LeftDrag(); } }

Unity用Input.touches实现手机端多点触控的更多相关文章

  1. 手机端html5触屏事件(touch事件)

    touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...

  2. (转)手机端html5触屏事件(touch事件)

    本文转载自:http://blog.sina.com.cn/s/blog_51048da70101f0ex.html touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时 ...

  3. 与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触控

    原文:与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触 ...

  4. 转:手机端html5触屏事件(touch事件)

    touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...

  5. 新鲜出炉:appium2.0+ 单点触控和多点触控新的解决方案

    在 appium2.0 之前,在移动端设备上的触屏操作,单手指触屏和多手指触屏分别是由 TouchAction 类,Multiaction 类实现的. 在 appium2.0 之后,这 2 个方法将会 ...

  6. windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放

    http://blog.csdn.net/wangrenzhu2011/article/details/7732907 (转) 实现方法: 对Manipulation进行抽象化 使不同容器可共用多点缩 ...

  7. Android开发实例之多点触控程序

    智能终端设备的多点触控操作为我们带来了种种炫酷体验,这也使得很多Android开发者都对多点触控程序的开发感兴趣.实际上多点触控程序的实现并不是那么遥不可及,而是比较容易.本文就主要通过一个实例具体讲 ...

  8. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件

    原文  MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...

  9. 微信小程序~触摸相关事件(拖拽操作、手势识别、多点触控)

    touchstart     手指触摸动作开始 touchmove    手指触摸后移动 touchcancel  手指触摸动作被打断,如来电提醒,弹窗 touchend      手指触摸动作结束 ...

随机推荐

  1. petite-vue-源码剖析-v-for重新渲染工作原理

    在<petite-vue源码剖析-v-if和v-for的工作原理>我们了解到v-for在静态视图中的工作原理,而这里我们将深入了解在更新渲染时v-for是如何运作的. 逐行解析 // 文件 ...

  2. laravel7 微信小程序获取openid

    l 通过微信公众号获取appid和appsecret l 在小程序页面中编写代码获取code l 在后端编写方法,换取openid l 添加一个按钮,给按钮一个开放能力 open-type=" ...

  3. 万字长文---关于PKM收集与整理系统的思考和实践

    PKM闭环中有一个很重要的环节就是信息输入,包括各种信息来源,例如微信公众号.博客.知乎.RSS等等,因此也就诞生了一大堆稍后读软件,如何真正有效的获取输入而不是做一只仓鼠是需要思考的.最近看了< ...

  4. FreeBSD 利用IPFW实现限制局域网使用QQ

    QQ服务器分为三类: 1.UDP 8000端口类7个:速度最快,服务器最多.QQ上线会向这7个服务器发送UDP数据包,选择回复速度最快的一个作为连接服务器.这7个服务器名字均以sz-sz7开头,域后缀 ...

  5. Docker——概述

    出现原因:开发接替运维的工作,将jar包连同(mysql,jdk)等环境上线 实现:java -> jar(环境) -> 打包项目带上环境(镜像) -> (Docker仓库:商店) ...

  6. git中的文件操作

    现在我们的机器上有了一个 真实项目 的 Git 仓库,并从这个仓库中检出了所有文件的 工作副本. 通常,你会对这些文件做些修改,每当完成了一个阶段的目标,想要将记录下它时,就将它提交到仓库. git中 ...

  7. Anaconda Navigator卡logo打不开闪退问题处理方案-更换阿里云镜像源

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 一.打开软件卡logo,点击图标后闪退 最近有同事使用anaconda时出现了卡logo,显示loading applications,点击图标时发 ...

  8. 阿里云开源镜像站支持IPv6访问

    阿里云开源镜像站在国内企业镜像站中率先支持IPv6访问! 点击立即试用https://developer.aliyun.com/mirror/ 同时基于阿里云OpenSearch的搜索能力,开源镜像站 ...

  9. Linux中ftp服务器的安装与部署

    一.ftp简介FTP(File Transfer Protocol,文件传输协议) 是 TCP/IP 协议组中的协议之一.FTP协议包括两个组成部分,其一为FTP服务器,其二为FTP客户端.其中FTP ...

  10. 74CMS 3.0 任意文件删除漏洞

    一. 启动环境 1.双击运行桌面phpstudy.exe软件 2.点击启动按钮,启动服务器环境 二.代码审计 1.双击启动桌面Seay源代码审计系统软件 2.因为74CMS3.0源代码编辑使用GBK编 ...