Last active
August 29, 2015 14:06
-
-
Save ihsanberahim/a9a3f91576d2e33988f8 to your computer and use it in GitHub Desktop.
Unity TouchClickManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
GUIDES: | |
1. Attach it on "Main Camera" to use. | |
2. Bind your function to the system event OnCasted, OnBegan, OnMove, OnEnd | |
4. Make sure you add "using System.Collections.Generic" on the top of all your script | |
See example below "YouScript.cs" | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class TouchClickManager : MonoBehaviour { | |
public delegate void RayEvent(TouchClickManager comp, RaycastHit hit); | |
public event RayEvent OnCasted; | |
public delegate void TouchClickEvent(List<Vector3> positions); | |
public event TouchClickEvent OnBegan; | |
public event TouchClickEvent OnMove; | |
public event TouchClickEvent OnEnd; | |
[HideInInspector] | |
public bool touchBegan,touchMove,touchEnd,clickBegan,clickMove,clickEnd,cast; | |
[HideInInspector] | |
public List<int> instanceIDs; | |
[HideInInspector] | |
public RaycastHit hit; | |
public List<Vector3> points; | |
public int maxPoints; | |
// Use this for initialization | |
void Start () { | |
instanceIDs = new List<int> (); | |
points = new List<Vector3>(); | |
touchBegan = touchMove = touchEnd = clickBegan = clickMove = clickEnd = cast = false; | |
} | |
// Update is called once per frame | |
void Update () { | |
touchBegan = (Input.touchCount>0&&Input.GetTouch (0).phase == TouchPhase.Began); | |
touchEnd = (Input.touchCount>0&&Input.GetTouch (0).phase == TouchPhase.Ended); | |
clickBegan = Input.GetMouseButtonDown (0); | |
clickEnd = Input.GetMouseButtonUp (0); | |
cast = false; | |
if(clickBegan) | |
{ | |
clickMove = true; | |
} | |
if(touchBegan) | |
{ | |
touchMove = true; | |
} | |
if(clickMove) | |
{ | |
if(!points.Contains(Input.mousePosition) && points.Count<maxPoints) | |
{ | |
points.Add(Input.mousePosition); | |
checkCast(Input.mousePosition); | |
} | |
if(clickBegan) | |
OnBegan(points); | |
} | |
if(touchMove) | |
{ | |
if(!points.Contains(Input.mousePosition) && points.Count<maxPoints) | |
{ | |
points.Add(Input.GetTouch (0).position); | |
checkCast(Input.GetTouch (0).position); | |
} | |
if(touchBegan) | |
OnBegan(points); | |
} | |
if((touchMove||clickMove) && points.Count>0) | |
{ | |
OnMove(points); | |
} | |
if (clickEnd || touchEnd) | |
{ | |
OnEnd (points); | |
points.RemoveRange (0, points.Count); | |
clickMove = false; | |
touchMove = false; | |
} | |
} | |
void OnEnable() | |
{ | |
OnCasted += OnCastedCallback; | |
OnBegan += OnBeganCallback; | |
OnMove += OnMoveCallback; | |
OnEnd += OnEndCallback; | |
} | |
void OnDisable() | |
{ | |
OnCasted -= OnCastedCallback; | |
OnBegan -= OnBeganCallback; | |
OnMove -= OnMoveCallback; | |
OnEnd -= OnEndCallback; | |
} | |
#region Callback | |
void OnCastedCallback(TouchClickManager comp, RaycastHit hit) | |
{ | |
Debug.Log ("TouchClickManager ... OnCasted.. "+hit.collider.name); | |
} | |
void OnBeganCallback(List<Vector3> points) | |
{ | |
string log = ""; | |
log += "TouchClickManager ... OnBegan.. "; | |
if (clickMove) | |
log += "click"; | |
if (touchMove) | |
log += "touch"; | |
// Debug.Log (log); | |
} | |
void OnMoveCallback(List<Vector3> points) | |
{ | |
// Debug.Log ("TouchClickManager ... OnMove.. "+ points.Count); | |
} | |
void OnEndCallback(List<Vector3> points) | |
{ | |
string log = ""; | |
log += "TouchClickManager ... OnEnd.. "; | |
if (clickMove) | |
log += "click"; | |
if (touchMove) | |
log += "touch"; | |
// Debug.Log (log); | |
} | |
#endregion | |
#region Helper | |
void checkCast(Vector3 point) | |
{ | |
cast = Physics.Raycast (Camera.main.ScreenPointToRay (point), out hit); | |
if(cast) | |
{ | |
OnCasted(this, hit); | |
} | |
} | |
public void listSubject() | |
{ | |
string log = ""; | |
log += instanceIDs.Count+" instanceIDs registered:\n"; | |
foreach(int instanceId in instanceIDs) | |
{ | |
log+="# "+instanceId+"\n"; | |
} | |
Debug.Log (log); | |
} | |
public void addSubject(int instanceId) | |
{ | |
if(!instanceIDs.Contains(instanceId)) | |
{ | |
instanceIDs.Add(instanceId); | |
} | |
listSubject (); | |
} | |
public void removeSubject(int instanceId) | |
{ | |
if (instanceIDs.Contains (instanceId)) | |
instanceIDs.Remove (instanceId); | |
} | |
#endregion | |
} | |
/* | |
CREDIT | |
ihsanberahim.com | |
fiction-labs.com | |
pkmedia.com.my | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class GameSystem : MonoBehaviour { | |
public TouchClickManager pointClass; | |
void Start() | |
{ | |
pointClass = GameObject.Find("Main Camera").GetComponent("TouchClickManager"); | |
} | |
void OnEnable() | |
{ | |
pointClass.OnBegan += onceClick; | |
pointClass.OnMove += whenMove; | |
pointClass.OnEnd += afterRelease; | |
pointClass.OnCasted += pointTouchObject; | |
} | |
void OnDisable() | |
{ | |
pointClass.OnBegan -= onceClick; | |
pointClass.OnMove -= whenMove; | |
pointClass.OnEnd -= afterRelease; | |
pointClass.OnCasted -= pointTouchObject; | |
} | |
#region Helper | |
void onceClick(List<Vector3> positions) | |
{ | |
//Do something here | |
} | |
void whenMove(List<Vector3> positions) | |
{ | |
//Do something here | |
} | |
void afterRelease(List<Vector3> positions) | |
{ | |
//Do something here | |
} | |
void pointTouchObject(TouchClickManager comp, RaycastHit hit) | |
{ | |
if(hit.collider.name=="MyGameObjectNameHere") | |
{ | |
//Do something here | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment