Last active
June 27, 2019 15:50
-
-
Save luispedrofonseca/67d325fb79ba2cc3d72f985c8e9d068f to your computer and use it in GitHub Desktop.
PG ESAD - Script to simplify the process of triggering an animation by clicking an object
This file contains hidden or 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; | |
public class OnClickAnimation : MonoBehaviour, IPointerClickHandler | |
{ | |
public Animator MyAnimator; | |
public string TriggerName = "idle"; | |
private void Reset() | |
{ | |
MyAnimator = GetComponentInChildren<Animator>(); | |
var mainCamera = Camera.main; | |
if (mainCamera == null) | |
mainCamera = FindObjectOfType<Camera>(); | |
if(mainCamera == null) return; | |
var physics2DRaycaster = mainCamera.GetComponentInChildren<PhysicsRaycaster>(); | |
if (physics2DRaycaster == null) | |
mainCamera.gameObject.AddComponent<PhysicsRaycaster>(); | |
var eventSystem = FindObjectOfType<EventSystem>(); | |
if (eventSystem == null) | |
{ | |
var evGO = new GameObject("EventSystem"); | |
evGO.AddComponent<EventSystem>(); | |
evGO.AddComponent<StandaloneInputModule>(); | |
} | |
} | |
public void OnPointerClick(PointerEventData eventData) | |
{ | |
if(!string.IsNullOrEmpty(TriggerName)) | |
{ | |
MyAnimator.SetTrigger(TriggerName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment