Author:- Tushar Sonu Lambole
1) This blog includes the sample code for Unity3d project to handle touch event on the mobile devices.
2) Call SetSocialLinks method
public class SocialLinks : MonoBehaviour {
private Vector2 mTouchStartPos;
private bool mTouchMoved = false;
private float mTimeElapsed = 0.0f;
private bool mTapped = false;
private float mTimeElapsedSinceTap = 0.0f;
public GameObject FacebookObj;
public GameObject TwitterObj;
public GameObject YoutubeObj;
public GameObject GooglePlusObj;
public GameObject LinkedINObj;
public GameObject CustomObj;
bool ShowFacebook; string FacebookLink;
bool ShowTwitter; string TwitterLink;
bool ShowGooglePlus; string GooglePlusLink;
bool ShowYoutube; string YoutubeLink;
bool ShowLinkedIn; string LinkedInLink;
bool ShowCustom; string CustomLink;
string CustomIconLink;
public void SetSocialLinks(bool showFacebook, string facebookLink, bool showTwitter, string twitterLink, bool showGooglePlus, string googlePlusLink, bool showYoutube, string youtubeLink, bool showLinkedIn, string linkedInLink, bool showCustom, string customLink, string custonIconLink)
{
ShowFacebook = showFacebook;
FacebookLink = facebookLink;
ShowTwitter = showTwitter;
TwitterLink = twitterLink;
ShowGooglePlus = showGooglePlus;
GooglePlusLink = googlePlusLink;
ShowYoutube = showYoutube;
YoutubeLink = youtubeLink;
ShowLinkedIn = showLinkedIn;
LinkedInLink = linkedInLink;
ShowCustom = showCustom;
CustomLink = customLink;
CustomIconLink = custonIconLink;
Debug.Log("FB:- "+facebookLink+" ,TW:- "+twitterLink+" ,GL:- "+googlePlusLink+" \n In:- "+linkedInLink+" ,YT:-"+youtubeLink+" ,cust:- "+customLink);
}
// Use this for initialization void Start ()
{
}
// Update is called once per frame
void Update () {
FacebookObj.SetActiveRecursively(ShowFacebook);
TwitterObj.SetActiveRecursively(ShowTwitter);
GooglePlusObj.SetActiveRecursively(ShowGooglePlus);
LinkedINObj.SetActiveRecursively(ShowLinkedIn);
YoutubeObj.SetActiveRecursively(ShowYoutube);
CustomObj.SetActiveRecursively(ShowCustom);
// Determine the number of taps
// Note: Input.tapCount doesn't work on Androidif (Input.touchCount > 0) {
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
mTouchStartPos = touch.position;
mTouchMoved = false;
mTimeElapsed = 0.0f;
}
else {
mTimeElapsed += Time.deltaTime;
}
if (touch.phase == TouchPhase.Moved) {
if (Vector2.Distance(mTouchStartPos, touch.position) > 40) {
// Touch moved too far
mTouchMoved = true;
}
} else if (touch.phase == TouchPhase.Ended){
if (!mTouchMoved && mTimeElapsed < 1.0){
if (mTapped) {
// Second tap
mTapped = false;
} else {
// Wait to see if this is a double tap
mTapped = true;
mTimeElapsedSinceTap = 0.0f;
}
}
}
}
if (mTapped){
if (mTimeElapsedSinceTap >= 0.5f) {
// Not a double tap
// getClickedLink(mTouchStartPos);
buttonclicked();
mTapped = false;
}else{
mTimeElapsedSinceTap += Time.deltaTime;
}
}
}
private void buttonclicked() {
RaycastHit hitInfo;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(r, out hitInfo)){
if(hitInfo.collider == FacebookObj.collider)
{
Application.OpenURL(FacebookLink);
}else if(hitInfo.collider == YoutubeObj.collider)
{
Application.OpenURL(YoutubeLink);
}else if(hitInfo.collider == TwitterObj.collider)
{
Application.OpenURL(TwitterLink);
}else if(hitInfo.collider == GooglePlusObj.collider)
{
Application.OpenURL(GooglePlusLink);
}else if(hitInfo.collider == LinkedINObj.collider)
{
Application.OpenURL(LinkedInLink);
}
else if(hitInfo.collider == CustomObj.collider)
{
Application.OpenURL(CustomLink);
}
}
}
GooglePlusObj.SetActiveRecursively(ShowGooglePlus);
LinkedINObj.SetActiveRecursively(ShowLinkedIn);
YoutubeObj.SetActiveRecursively(ShowYoutube);
CustomObj.SetActiveRecursively(ShowCustom);
// Determine the number of taps
// Note: Input.tapCount doesn't work on Androidif (Input.touchCount > 0) {
Touch touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
mTouchStartPos = touch.position;
mTouchMoved = false;
mTimeElapsed = 0.0f;
}
else {
mTimeElapsed += Time.deltaTime;
}
if (touch.phase == TouchPhase.Moved) {
if (Vector2.Distance(mTouchStartPos, touch.position) > 40) {
// Touch moved too far
mTouchMoved = true;
}
} else if (touch.phase == TouchPhase.Ended){
if (!mTouchMoved && mTimeElapsed < 1.0){
if (mTapped) {
// Second tap
mTapped = false;
} else {
// Wait to see if this is a double tap
mTapped = true;
mTimeElapsedSinceTap = 0.0f;
}
}
}
}
if (mTapped){
if (mTimeElapsedSinceTap >= 0.5f) {
// Not a double tap
// getClickedLink(mTouchStartPos);
buttonclicked();
mTapped = false;
}else{
mTimeElapsedSinceTap += Time.deltaTime;
}
}
}
private void buttonclicked() {
RaycastHit hitInfo;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(r, out hitInfo)){
if(hitInfo.collider == FacebookObj.collider)
{
Application.OpenURL(FacebookLink);
}else if(hitInfo.collider == YoutubeObj.collider)
{
Application.OpenURL(YoutubeLink);
}else if(hitInfo.collider == TwitterObj.collider)
{
Application.OpenURL(TwitterLink);
}else if(hitInfo.collider == GooglePlusObj.collider)
{
Application.OpenURL(GooglePlusLink);
}else if(hitInfo.collider == LinkedINObj.collider)
{
Application.OpenURL(LinkedInLink);
}
else if(hitInfo.collider == CustomObj.collider)
{
Application.OpenURL(CustomLink);
}
}
}