https://answers.unity.com/questions/1526663/detect-click-on-canvas.html
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class FakeTouch : MonoBehaviour
{
// Normal raycasts do not work on UI elements, they require a special kind
GraphicRaycaster raycaster;
void Awake()
{
// Get both of the components we need to do this
this.raycaster = GetComponent<GraphicRaycaster>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Debug.Log("Press Q");
//Set up the new Pointer Event
PointerEventData pointerData = new PointerEventData(EventSystem.current);
List<RaycastResult> results = new List<RaycastResult>();
//Raycast using the Graphics Raycaster and mouse click position
pointerData.position = Input.mousePosition;
raycaster.Raycast(pointerData, results);
//For every result returned, output the name of the GameObject on the Canvas hit by the Ray
foreach (RaycastResult result in results)
{
Debug.Log("Hit " + result.gameObject.name);
Button button = result.gameObject.GetComponent<Button>();
if (button != null)
{
button.onClick.Invoke();
}
}
}
}
}
'🌍 Unity > UI & TMP' 카테고리의 다른 글
유니티 TextMeshPro 한글 폰트 생성 설정 정리 (0) | 2020.03.30 |
---|---|
Unity UI Button onClick.AddListener 활용하기 (0) | 2020.02.24 |
InputField AddListener로 이벤트 등록 (0) | 2019.11.26 |
TextMeshProUGUI DoText (0) | 2019.11.08 |
선택창 싱글턴 + 델리게이트 체인 (0) | 2019.10.15 |