반응형

private const float inchToCm = 2.54f; 
[SerializeField] private EventSystem eventSystem = null; 
[SerializeField] private float dragThresholdCM = 0.5f; //For drag Threshold 
private void SetDragThreshold() { 
    if (eventSystem != null) { 
        eventSystem.pixelDragThreshold = (int)(dragThresholdCM * Screen.dpi / inchToCm); 
    } 
} 

void Awake() { 
    SetDragThreshold(); 
}

Unity로 안드로이드 앱을 개발하다보면 스크롤 안에 버튼이 들어갈 경우가 종종 있다.

PC에서 테스트 할 때는 마우스로 잘 눌리던 버튼이 스마트 폰에 넣어서 테스트 해보면 간혹 잘 눌리지 않는 경우가 있다. 이럴때 EventSystem 설정을 바꿔줘야하는데 다음과 같이 세팅 하면 적당하다.

 

반응형
반응형

유니티의 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로 안드로이드 앱을 개발하다보면 스크롤 안에 버튼이 들어갈 경우가 종종 있다.

PC에서 테스트 할 때는 마우스로 잘 눌리던 버튼이 스마트 폰에 넣어서 테스트 해보면 간혹 잘 눌리지 않는 경우가 있다. 이럴때 EventSystem 설정을 바꿔줘야하는데 다음과 같이 세팅 하면 적당하다.

 

private const float inchToCm = 2.54f; 	 
[SerializeField] private EventSystem eventSystem = null; 	 
[SerializeField] private float dragThresholdCM = 0.5f; //For drag Threshold 

private void SetDragThreshold() { 	
    if (eventSystem != null) 	{ 		
        eventSystem.pixelDragThreshold = (int)(dragThresholdCM * Screen.dpi / inchToCm); 	
    } 
}     

void Awake() { 	
    SetDragThreshold(); 
}

 

반응형

+ Recent posts