+ 이 글은 작성자가 직접 공부하고 복습하며 작성한 글입니다. 만약 직접 작성하지 않았다면, 꼭 출처를 밝히겠습니다!
+ 이 글은 개인적인 공부를 바탕으로 작성되었기에, 틀린 부분이 있을 수 있으며, 틀린 부분이 있다면 알려주시면 감사하겠습니다!
+ 이 글을 다른 곳으로 가져가신다면, 꼭 출처를 남겨주세요~
+ '참고사이트'는 공부하기 위해 참고한 사이트들을 모아 둔 것입니다.
+ 혹시라도 문제가 된다면 바로 조취를 취할테니, 말해주시면 감사하겠습니다!
+ Unity Editor에서 설정할 수도 있지만, Script를 통해서도 세부적으로 설정할 수 있다.
+ 여기에 2가지 종류가 있는데, UI에서 입력 처리하는 것과 GUI에서 입력처리하는 것이다.
1) UI에서 입력 처리
+ Selectable.OnPointer 시리즈를 사용함.
+ using UnityEngine.EventSystems; 추가
+ 인터페이스 상속 - IPointerEnterHandler, IPointerExitHandler, ISelectHandler 등, 여러가지가 있다.
+ 인터페이스를 상속하면서 선언해줘야 하는 함수들 작성하기.
+ eventData관련 정보를 확인하고 싶으면, https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData.html 이곳을 확인하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.EventSystems; using UnityEngine.UI; public class Button : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler { public void OnPointerEnter(PointerEventData eventData) { _Image.sprite = _cash_image["exit_btn_highlighted_black"]; } public void OnPointerExit(PointerEventData eventData) { _Image.sprite = _cash_image["exit_btn_normal_black"]; } public void OnPointerDown(PointerEventData eventData) { _Image.sprite = _cash_image["exit_btn_clicked_black"]; } } |
2) GUI에서 입력 처리
+ MonoBehaviour.OnMouse 시리즈를 사용함
+ Monobehavior에서 상속하는 함수들이므로 따로 작업을 해주지 않아도 된다.
+ 공식 Docs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //Attach this script to a GameObject to have it output messages when your mouse hovers over it. using UnityEngine; public class OnMouseOverExample : MonoBehaviour { // The mesh goes red when the mouse is over it... void OnMouseEnter() { Debug.Log("Mouse is Enter GameObject."); } void OnMouseOver() { //If your mouse hovers over the GameObject with the script attached, output this message Debug.Log("Mouse is over GameObject."); } void OnMouseExit() { //The mouse is no longer hovering over the GameObject so output this message each frame Debug.Log("Mouse is no longer on GameObject."); } } | cs |
** 참고사이트 **
- https://answers.unity.com/questions/1166426/how-do-i-run-a-script-when-a-button-is-highlighted.html
- https://www.youtube.com/watch?v=1x4LVGRTeB0
- http://www.devkorea.co.kr/bbs/board.php?bo_table=m03_qna&wr_id=81958
- https://answers.unity.com/questions/924683/difference-between-onpointerenter-and-onmouseenter.html - 두가지 경우를 비교한 것
Copyright © -강정이좋아- 무단 전재 및 재배포는 하지 말아주세요.
'요리 레시피 > Unity' 카테고리의 다른 글
Sprite 바꾸기 - 공식문서 (0) | 2018.06.12 |
---|---|
Script에서 Resource가져오기 (0) | 2018.06.12 |
[Unity] rigidbody.velocity (0) | 2018.05.14 |
[UGUI] Progress Bar - Slider (0) | 2018.04.06 |
[UGUI] NGUI -> UGUI (0) | 2018.04.06 |