Created
November 18, 2021 22:15
-
-
Save whoisryosuke/f8c9441ef7cc30e9fe70419dbe9f5f56 to your computer and use it in GitHub Desktop.
Unity - Detect input on canvas elements using EventSystem event interfaces (e.g. IPointerClickHandler) -- via: http://answers.unity.com/answers/1095070/view.html
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 UnityEngine.EventSystems; // 1 | |
public class PointerExample : MonoBehaviour | |
, IPointerClickHandler // 2 | |
, IDragHandler | |
, IPointerEnterHandler | |
, IPointerExitHandler | |
// ... And many more available! | |
{ | |
SpriteRenderer sprite; | |
Color target = Color.red; | |
void Awake() | |
{ | |
sprite = GetComponent<SpriteRenderer>(); | |
} | |
void Update() | |
{ | |
if (sprite) | |
sprite.color = Vector4.MoveTowards(sprite.color, target, Time.deltaTime * 10); | |
} | |
public void OnPointerClick(PointerEventData eventData) // 3 | |
{ | |
print("I was clicked"); | |
target = Color.blue; | |
} | |
public void OnDrag(PointerEventData eventData) | |
{ | |
print("I'm being dragged!"); | |
target = Color.magenta; | |
} | |
public void OnPointerEnter(PointerEventData eventData) | |
{ | |
target = Color.green; | |
} | |
public void OnPointerExit(PointerEventData eventData) | |
{ | |
target = Color.red; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment