Skip to content

Instantly share code, notes, and snippets.

@cabbibo
Created August 15, 2017 00:19
Show Gist options
  • Save cabbibo/841712f2b4d98d915737c0c45becf850 to your computer and use it in GitHub Desktop.
Save cabbibo/841712f2b4d98d915737c0c45becf850 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
public class TouchEvents : MonoBehaviour {
public delegate void TouchDown(Ray ray,Vector3 screenPos);
public static event TouchDown OnTouchDown;
public delegate void TouchUpdate(Ray ray,Vector3 screenPos , Vector3 deltaPos , Vector3 touchStartPos );
public static event TouchUpdate OnTouchUpdate;
public delegate void TouchUp(Ray ray,Vector3 screenPos, Vector3 deltaPos , Vector3 touchStartPos);
public static event TouchUp OnTouchUp;
public delegate void ObjectDown(Ray ray,GameObject t, Vector3 screenPos, Vector3 eyeVec);
public static event ObjectDown OnObjectDown;
public delegate void ObjectUpdate(Ray ray,GameObject t, Vector3 screenPos, Vector3 eyeVec, Vector3 deltaPos , Vector3 touchStartPos);
public static event ObjectUpdate OnObjectUpdate;
public delegate void ObjectUp(Ray ray,GameObject t, Vector3 screenPos, Vector3 eyeVec, Vector3 deltaPos , Vector3 touchStartPos);
public static event ObjectUp OnObjectUp;
private Vector3 oPos;
private Vector3 touchStartPos;
private Vector3 dPos;
private bool touching = false;
private bool dragging = false;
private GameObject collidedObject;
private Vector3 eyeVec;
private Ray ray;
void OnDown( Vector3 screenPosition ){
touchStartPos = screenPosition;
oPos = screenPosition;
//Debug.Log( "DOWN");
//GameObject ball = newBall;
touching = true;
// Seeing if we should make something new or drag something old;
ray = Camera.main.ScreenPointToRay(screenPosition);
RaycastHit info;
if( Physics.Raycast(ray, out info, 100) ){
collidedObject = info.collider.gameObject;
eyeVec = Camera.main.transform.position - collidedObject.transform.position;
dragging = true;
}
if( dragging == false ){
if(OnTouchDown != null) OnTouchDown(ray, screenPosition);
}else{
if(OnObjectDown != null) OnObjectDown( ray, collidedObject , screenPosition , eyeVec );
}
}
void OnUp( Vector3 screenPosition ){
//Debug.Log("UP");
dPos = screenPosition - oPos;
if( dragging == false ){
if(OnTouchUp != null) OnTouchUp(ray,screenPosition,dPos,touchStartPos);
}else{
if(OnObjectUp != null) OnObjectUp(ray,collidedObject,screenPosition,eyeVec,dPos,touchStartPos);
}
dragging = false;
touching = false;
collidedObject = null;
oPos = screenPosition;
}
void OnDrag( Vector3 screenPosition ){
//Debug.Log( "DRAG");
dPos = screenPosition - oPos;
Ray ray = Camera.main.ScreenPointToRay(screenPosition);
if( dragging == false ){
if(OnTouchUpdate != null) OnTouchUpdate(ray ,screenPosition,dPos,touchStartPos);
}else{
if(OnObjectUpdate != null) OnObjectUpdate(ray,collidedObject,screenPosition,eyeVec,dPos,touchStartPos);
}
oPos = screenPosition;
}
public static Vector3 GetWorldPos( Vector3 screenPosition , float val ){
Ray ray = Camera.main.ScreenPointToRay(screenPosition);
Vector3 p = ray.origin + ray.direction * val;
return p;
}
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
if( Input.GetMouseButtonDown(0) == true ){
OnDown( Input.mousePosition);
}
if( Input.GetMouseButtonUp(0) == true ){
OnUp( Input.mousePosition);
}
if( Input.GetMouseButton(0) == true ){
OnDrag( Input.mousePosition);
}
#else
if (Input.touchCount > 0 ){
var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began){
OnDown( touch.position );
}else if (touch.phase == TouchPhase.Moved){
OnDrag( touch.position );
}else if (touch.phase == TouchPhase.Ended){
OnUp( touch.position );
}
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment