Created
February 18, 2014 10:46
-
-
Save zachiPL/9068505 to your computer and use it in GitHub Desktop.
Unity tap button (wth SpriteRenderer and BoxCollider2d) - I'm using this a lot.
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; | |
[RequireComponent(typeof(BoxCollider2D))] | |
public class CustomTapBehaviour : MonoBehaviour | |
{ | |
#region Fields | |
public delegate void HandlingEv(GameObject invoker); | |
public event HandlingEv OnHandlingEv; | |
#endregion | |
void Update() | |
{ | |
if (this.GetComponent<BoxCollider2D>().enabled) | |
{ | |
for (int i = 0; i < Input.touches.Length; i++) | |
{ | |
if (Input.touches[i].phase == TouchPhase.Began) | |
{ | |
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.touches[i].position); | |
Vector2 touchPos = new Vector2(wp.x, wp.y); | |
if (collider2D == Physics2D.OverlapPoint(touchPos)) | |
{ | |
this.TriggerEv(); | |
} | |
} | |
} | |
} | |
} | |
void OnMouseDown() | |
{ | |
#if UNITY_EDITOR | |
if (this.GetComponent<BoxCollider2D>().enabled) | |
{ | |
this.TriggerEv(); | |
} | |
#endif | |
} | |
private void TriggerEv() | |
{ | |
if (this.OnHandlingEv != null) | |
{ | |
this.OnHandlingEv(this.gameObject); | |
} | |
} | |
internal void ClearListeners() | |
{ | |
this.OnHandlingEv = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment