반응형
유니티의 UI컴포넌트를 많이 사용하는데, ScrollRect도 당연히 많이 사용하게 된다.
ScrollRect로 리스트 구현할 때 간혹 리스트 안에 Button을 붙이는 경우가 생기는데 Button과 ScrollRect간에 이벤트가 공유되지 않아 원치 않은 상황이 생기게 된다. 예를 들면 Button위에서 Swipe을 하게 될 경우 이런경우 스크립으로 버튼오브젝트에 발생되는 터치 이벤트 정보를 스크롤렉트로 동일하게 잘 넘겨주면 되지만 ... 아무튼 귀찮다.
이번꺼는 버튼만 정리한 내용이다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonTouchHandler : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerClickHandler {
bool isEnable;
Button Btn_Target;
void Start () {
Btn_Target = this.gameObject.GetComponent<Button> ();
Btn_Target.interactable = false;
}
public void OnPointerDown (PointerEventData e) {
isEnable = true;
Btn_Target.interactable = false;
}
public void OnDrag (PointerEventData e) {
if (Mathf.Abs (e.delta.x) > 2 || Mathf.Abs (e.delta.y) > 2) {
Btn_Target.interactable = false;
isEnable = false;
}
}
public void OnPointerClick (PointerEventData e) {
if (isEnable) {
Btn_Target.interactable = true;
Btn_Target.OnPointerClick (e);
}
}
}
추가로 상위 ScrollRect로 이벤트를 공유하는 스크립은 이렇게...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MyEvent : UnityEvent<Vector2> { }
public class Synchronizer : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public ScrollRect ParentSR;
public void OnBeginDrag (PointerEventData e) {
ParentSR.OnBeginDrag (e);
}
public void OnDrag (PointerEventData e) {
ParentSR.OnDrag (e);
if (Mathf.Abs (e.delta.x) > 2 || Mathf.Abs (e.delta.y) > 2) {
isDrag = true;
}
}
public void OnEndDrag (PointerEventData e) {
ParentSR.OnEndDrag (e);
}
}반응형
'unity C#' 카테고리의 다른 글
| [Unity] Debug to screen 디버그를 화면에 출력 (0) | 2019.05.29 |
|---|---|
| [Unity] Face Tracking을 이용한 화면 로테이션 (1) | 2019.05.24 |
| [Unity] unity loading bar 구현 (0) | 2019.05.16 |
| [Unity] 스크린 캡쳐(RenderTexture) 해서 여백잘라 Sprite로 가져오기 (0) | 2019.05.03 |
| [Unity] unity callback 함수 호출하기 (0) | 2017.11.10 |