用过Unity的都知道自带的Input.touches并不支持鼠标输入,给咱们的调试带来很大的不方便。那么咱们会发现其实有不少触控方面的插件,如inputtouches,easy touch,fingerGesture等。this
下面我主要讲解FingerGesture的使用,这个插件不是免费的,能够自行购买spa
1.导入插件插件
导入后的插件会在Assets/Plugins下面调试
2.拖动Assets/Plugins/FingerGestures/Prefabs/FingerGestures到你的Hierarchy中,以下图code
3.建立空节点GestureObj,而后绑定脚本TestGesture.csorm
using UnityEngine; using System.Collections; public class testGestures : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } // 单击 void OnTap(TapGesture gesture) { if (gesture.Selection == tapObject) { Debug.Log("Tab!!!!!!!!!!!"); } } // 双击 void OnDoubleTap(TapGesture gesture) { if (gesture.Selection == doubleTapObject) { Debug.Log("Double Tab!!!!!!!!!!!"); } } void OnSwipe(SwipeGesture gesture) { // make sure we started the swipe gesture on our swipe object // we use the object the swipe started on, instead of the current one GameObject selection = gesture.StartSelection; if (selection == swipeObject) { Debug.Log("Swipe!!!!!!!"); } } int dragFingerIndex = -1; void OnDrag(DragGesture gesture) { // 获取起始点 FingerGestures.Finger finger = gesture.Fingers[0]; if (gesture.Phase == ContinuousGesturePhase.Started) { // dismiss this event if we're not interacting with our drag object if (gesture.Selection != dragObject) return; // remember which finger is dragging dragObject dragFingerIndex = finger.Index; } else if (finger.Index == dragFingerIndex) // gesture in progress, make sure that this event comes from the finger that is dragging our dragObject { if (gesture.Phase == ContinuousGesturePhase.Updated) { // update the position by converting the current screen position of the finger to a world position on the Z = 0 plane dragObject.transform.position = GetWorldPos(gesture.Position); } else { // reset our drag finger index dragFingerIndex = -1; } } } // 长按 void OnLongPress(LongPressGesture gesture) { if (gesture.Selection == longPressObject) { Debug.Log("Long press!!!!!!"); } } // 定义变量,用来操做 public GameObject longPressObject; public GameObject tapObject; public GameObject doubleTapObject; public GameObject swipeObject; public GameObject dragObject; // 公共方法 public static Vector3 GetWorldPos(Vector2 screenPos) { Ray ray = Camera.main.ScreenPointToRay(screenPos); // we solve for intersection with z = 0 plane float t = -ray.origin.z / ray.direction.z; return ray.GetPoint(t); } }
4.添加指定的触控对象对象
5.添加Componentblog
6.添加Screen Raycasterip
7.要为对象添加2D物理碰撞区域。否则的话点击没有效果。rem