unity怎么用PointerEventData类获得unity3d手指拖动物体点击的物体世界坐标

当前位置: →
→ Unity3D判断手指左右滑动
Unity3D判断手指左右滑动
& 作者及来源: 陳小贱 - 博客园 &
&收藏到→_→:
摘要: Unity3D判断手指左右滑动
"Unity3D判断手指左右滑动"::
前段时间开发的项目,需要一个功能,手指向左滑动时响应一个事件,手指向右滑动时响应另一个事件,网上看了看,有插件,不过没时间研究,而且插件多了总觉得不舒服,所以自己临时写了一个,下面是c#版代码。
private&vector2 touchfirst =&vector2.//手指开始按下的位置
private&vector2 touchsecond =&vector2.//手指拖动的位置
void ongui(){
  if(event.current.type == eventtype.mousedown){//判断当前手指是按下事件
    touchfirst&= event.current.//记录开始按下的位置
  if(event.current.type == eventtype.mousedown){//判断当前手指是拖动事件
    touchsecond&= event.current.//记录拖动的位置
    if(touchsecond.x &&touchfirst.x){//拖动的位置比按下的位置的x小
      //向左滑动
    if(touchsecond.x & touchfirst.x){//拖动的位置比按下的位置的x大
      //向右滑动
    touchfirst =&touch
注:因为unity3d默认坐标是:
  从左到右0-n。x坐标逐渐变大。
  从下到上0-n。y坐标逐渐变大。
所以根据规律还可以判断出鼠标向上,向下,向左,向右,左上,左下,右上,右下8个方向的手指滑动。
&搜索此文相关文章:此文来自: 马开东博客
网址: 站长QQ
Unity3D判断手指左右滑动_博客园相关文章
博客园_总排行榜
博客园_最新
博客园_月排行榜
博客园_周排行榜
博客园_日排行榜程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
FingerGestures研究院之初探Unity手势操作(一)
FingerGestures研究院之初探Unity手势操作(一)
围观34605次
编辑日期: 字体:
昨天搬家,我被无情的从4楼请上了10楼。原因就是房东们为了争家产打官司,受伤的永远是我们这些打工的租房的码农,呵呵!结果就是我们两家做了一个调换把房子换了一下。东西太多了,真的好累啊,好累啊~~前几天有个朋友问我Unity手势操作,后来我还帮他做了一个例子。我觉得在Unity中用这个手势操作的插件会很方便。以前我只是知道FingerGestures,但是没有深入的用过,这两天学习了一下。真的很好用。
最近研究了一下Unity中的一个手势操作的插件FingerGestures。它能很方便监听到Unity中的各种手势事件:上下左右四方向的滑动事件、按下事件、抬起事件、移动事件、连击事件、长按事件等等。它同时支持触摸屏操作与鼠标操作,总起来说使用起来还是比较方便的,今天写下教程记录这个插件的详细使用步骤。首先下载这个插件,大家可以在圣典上找这个插件的下载地址,当然也可以在本文最后下载该插件。
我看了一下这个插件底层的实现步骤,他是通过C#代理的形式来实现手势操作的。如下图红圈内所示,这五个重要的预设用来监听触摸与鼠标的手势事件。包括:单手触摸事件、双手触摸事件、鼠标事件、触摸事件。这里我们使用一个单手的事件,如图中所示将Finger Gertures Initializer拖拽入左侧层次视图中。
OK,上面我们说了该插件是通过C#代理形式来接收事件消息的,所以我们需要用脚本来注册这些事件从而开始接收消息。接着创建一个立方体对象用以处理手势操作,当然你也可以处理游戏中的任何对象。编写脚本FingerEvent.cs ,把这个脚本挂在这个立方体对象之上。
FingerEvent.cs脚本
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
using UnityEngine;using System.Collections;&public class FingerEvent :&&MonoBehaviour {&&&&&void OnEnable()&&&&{&&&& //启动时调用,这里开始注册手势操作的事件。&&&&& //按下事件: OnFingerDown就是按下事件监听的方法,这个名子可以由你来自定义。方法只能在本类中监听。下面所有的事件都一样!!!&&&&&&&&FingerGestures.OnFingerDown += OnFingerDown;&&&&&&&&//抬起事件
FingerGestures.OnFingerUp += OnFingerUp; &&&&//开始拖动事件 &&&&FingerGestures.OnFingerDragBegin += OnFingerDragBegin;&&&&&&&&//拖动中事件...&&&&&&&&FingerGestures.OnFingerDragMove += OnFingerDragMove;&&&&&&&&//拖动结束事件&&&&&&&&FingerGestures.OnFingerDragEnd += OnFingerDragEnd;
//上、下、左、右、四个方向的手势滑动
FingerGestures.OnFingerSwipe += OnFingerSwipe;
//连击事件 连续点击事件
FingerGestures.OnFingerTap += OnFingerTap;
//手指触摸屏幕中事件调用一下三个方法
FingerGestures.OnFingerStationaryBegin += OnFingerStationaryBegin;
FingerGestures.OnFingerStationary += OnFingerStationary;
FingerGestures.OnFingerStationaryEnd += OnFingerStationaryEnd;
//长按事件
FingerGestures.OnFingerLongPress += OnFingerLongPress;&&&&&}&&&&&void OnDisable()&&&&{&&&& //关闭时调用,这里销毁手势操作的事件&&&& //和上面一样&&&&&&&&FingerGestures.OnFingerDown -= OnFingerDown;
FingerGestures.OnFingerUp -= OnFingerUp;
FingerGestures.OnFingerDragBegin -= OnFingerDragBegin;&&&&&&&&FingerGestures.OnFingerDragMove -= OnFingerDragMove;&&&&&&&&FingerGestures.OnFingerDragEnd -= OnFingerDragEnd;
FingerGestures.OnFingerSwipe -= OnFingerSwipe;
FingerGestures.OnFingerTap -= OnFingerTap;
FingerGestures.OnFingerStationaryBegin -= OnFingerStationaryBegin;
FingerGestures.OnFingerStationary -= OnFingerStationary;
FingerGestures.OnFingerStationaryEnd -= OnFingerStationaryEnd;
FingerGestures.OnFingerLongPress -= OnFingerLongPress;&&&&}&&&&&//按下时调用&&&&void OnFingerDown( int fingerIndex, Vector2 fingerPos )&&&&{
//int fingerIndex 是手指的ID 第一按下的手指就是 0 第二个按下的手指就是1。。。一次类推。
//Vector2 fingerPos 手指按下屏幕中的2D坐标&
//将2D坐标转换成3D坐标&&&&&&&&transform.position = GetWorldPos( fingerPos );
Debug.Log(" OnFingerDown ="&&+fingerPos);&&&&}& //抬起时调用 void OnFingerUp( int fingerIndex, Vector2 fingerPos, float timeHeldDown ) {&
Debug.Log(" OnFingerUp ="&&+fingerPos); }& //开始滑动 void OnFingerDragBegin( int fingerIndex, Vector2 fingerPos, Vector2 startPos )&&&&{&&
Debug.Log("OnFingerDragBegin fingerIndex =" + fingerIndex&&+ " fingerPos ="+fingerPos +"startPos =" +startPos);&&&&} //滑动结束 void OnFingerDragEnd( int fingerIndex, Vector2 fingerPos ) {&
Debug.Log("OnFingerDragEnd fingerIndex =" + fingerIndex&&+ " fingerPos ="+fingerPos); } //滑动中&&&&void OnFingerDragMove( int fingerIndex, Vector2 fingerPos, Vector2 delta )&&&&{&&&&&&&&&& transform.position = GetWorldPos( fingerPos );
Debug.Log(" OnFingerDragMove ="&&+fingerPos); &&&&&} //上下左右四方方向滑动手势操作 void OnFingerSwipe( int fingerIndex, Vector2 startPos, FingerGestures.SwipeDirection direction, float velocity )&&&&{
//结果是 Up Down Left Right 四个方向
Debug.Log("OnFingerSwipe " + direction + " with finger " + fingerIndex);&&&&&}& //连续按下事件, tapCount就是当前连续按下几次 void OnFingerTap( int fingerIndex, Vector2 fingerPos, int tapCount )&&&&{&&&&&&&&&Debug.Log("OnFingerTap " + tapCount + " times with finger " + fingerIndex);&&&&&}& //按下事件开始后调用,包括 开始 结束 持续中状态只到下次事件开始!&&&&&&&& //OnFingerStationary 事件和&&OnFingerDragMove 有一个区别。&&&&&&&& //OnFingerStationary 是手指触摸在屏幕中的事件,而OnFingerDragMove是先触摸一下然后滑动的事件。&&&&&&&& //如果你需要时时捕获手指触摸屏幕中的事件时 用OnFingerStationary 即可 void OnFingerStationaryBegin( int fingerIndex, Vector2 fingerPos ) {&
Debug.Log("OnFingerStationaryBegin " + fingerPos + " times with finger " + fingerIndex); }& void OnFingerStationary( int fingerIndex, Vector2 fingerPos, float elapsedTime ) {&
Debug.Log("OnFingerStationary " + fingerPos + " times with finger " + fingerIndex);& }& void OnFingerStationaryEnd( int fingerIndex, Vector2 fingerPos, float elapsedTime ) {&
Debug.Log("OnFingerStationaryEnd " + fingerPos + " times with finger " + fingerIndex); }& //长按事件 void OnFingerLongPress( int fingerIndex, Vector2 fingerPos ) {&
Debug.Log("OnFingerLongPress " + fingerPos ); }& //把Unity屏幕坐标换算成3D坐标&&&&Vector3 GetWorldPos( Vector2 screenPos )&&&&{&&&&&&&&Camera mainCamera = Camera.main;&&&&&&&&return mainCamera.ScreenToWorldPoint( new Vector3( screenPos.x, screenPos.y, Mathf.Abs( transform.position.z - mainCamera.transform.position.z ) ) );&&&&}}
如下图所示,用鼠标还是IOS Android触摸事件都能很好的在这个Cube上响应,大家把我的代码手动的打一遍就什么都明白了。
上面的脚本,我们是直接绑定在立方体对象上来监听它,如果你想在别的脚本监听这个立方体对象的手势操作。只需调用如下方法即可。这个方法官方封装在了SampleBase中。因为官方的例子程序脚本是继承它的,所以子类就可以直接使用父类的方法。可是SampleBase会自动初始化一个SampleUI的脚本,不想初始化这个脚本的话直接用下面方法就行,原理就是通过射线我就不过多的解释了。传递鼠标或触摸的2D坐标即可得到触摸的3D模型对象。
1234567891011
&&&&// Return the GameObject at the given screen position, or null if no valid object was found&&&&public static GameObject PickObject( Vector2 screenPos )&&&&{&&&&&&&&Ray ray = Camera.main.ScreenPointToRay( screenPos );&&&&&&&&RaycastHit hit;&&&&&&&&&if( Physics.Raycast( ray, out hit ) )&&&&&&&&&&&&return hit.collider.gameObject;&&&&&&&&&return null;&&&&}
最后大家仔细看一下官方的FingerGestures.cs脚本,所有的手势操作的事件都在这里,包括单手操作事件、双手操作事件、鼠标操作事件。
插件以及源码下载地址:
雨松MOMO祝大家学习愉快,啦啦啦。
本文固定链接:
转载请注明:
MOMO与MO嫂提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!5686人阅读
Unity3D的uGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值。如下图所示:
先以PointerClick为例。这个是用于某点点击事件。其他事件都可以根据相同的办法调用。
之所以使用PointerClick为例。是因为在最后笔者会提到一个特殊的实现方式。而相比于其他事件类型,有且仅有Click事件存在特殊实现。
我们要实现事件主要有3种方式:
方式一:继承基础接口实现
步骤一:创建ClickObject脚本。继承MonoBehaviour和IPointerClickHandler。
步骤二:实现public void OnPointerClick(PointerEventData eventData)方法:
步骤三:创建一个名为Panel_IPointer的空对象。并且将ClickObject脚本附加到对象上。
步骤四:启动,并点击Panel_IPointer对象。在Console输出如下:
方式二:Unity3D编辑器操作设置实现
步骤一:创建一个C#脚本。在脚本中添加一个public方法。
步骤二:创建一个命名为Empty的UI对象,用于接收和响应点击事件。创建一个名为Panel的UI对象,用于触发点击事件。
步骤三:Panel对象添加EventTrigger组件,& Add New& -& 选择& PointerClick&。将Empty对象拖拽到触发者位置。然后点击&No Function&选择我们写在Test脚本中的OnTestClick事件。
步骤四:设置好这些之后。我们的事件触发就已经完成了。运行Unity3D。点击窗口中Panel对象。Console输出内容如下:
方式三:程序动态设置实现
我们在日常的开发中。可能需要动态的需要变更绑定的事件。那么我们如何才能使用C#代码控制绑定触发事件呢?
下面我们就介绍代码控制。ScriptControl.cs脚本
1 using System.Collections.G
2 using UnityE
3 using UnityEngine.E
4 using UnityEngine.EventS
6 public class ScriptControl : MonoBehaviour {
void Start ()
var trigger = transform.gameObject.GetComponent&EventTrigger&();
if (trigger == null)
trigger = transform.gameObject.AddComponent&EventTrigger&();
trigger.delegates = new List&EventTrigger.Entry&();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerC
entry.callback = new EventTrigger.TriggerEvent();
UnityAction&BaseEventData& callback = new UnityAction&BaseEventData&(OnScriptControll);
entry.callback.AddListener(callback);
trigger.delegates.Add(entry);
void Update () {
public void OnScriptControll(BaseEventData arg0)
Debug.Log(&Test Click&);
点击事件的特殊实现方式:使用Button控件实现
针对Click事件还存在一种特殊方式:uGUI系统中官方提供了一种Button控件。Button封装了官方提供的一套OnClick事件。操作完全类似于方式二。便不详述了。
使用Button我们可以实现动态的变更鼠标绑定的点击事件。如下代码所示:
1 using UnityE
2 using System.C
3 using UnityEngine.UI;
5 public class BtnControl : MonoBehaviour {
// Use this for initialization
void Start ()
var button = transform.gameObject.GetComponent&Button&();
if (button != null)
button.onClick.RemoveAllListeners();
button.onClick.AddListener(TestClick);
public void TestClick()
Debug.Log(&Test Click. This is Type 4&);
// Update is called once per frame
void Update () {
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:75291次
排名:千里之外
转载:43篇
(1)(1)(1)(1)(1)(3)(1)(2)(2)(2)(1)(1)(1)(1)(1)(1)(2)(4)(2)(1)(3)(4)(5)(5)(3)

我要回帖

更多关于 pointer event none 的文章

 

随机推荐