[英]How can i check if a vector2 (touch input) is inside every rectTransform of a rectTransform[] array?
我一直在嘗試使用該功能
RectTransformUtility.RectangleContainsScreenPoint()
與foreach循環在一起,但是我沒有得到結果。
這是我的代碼:
if (Input.touchCount > 0)
{
foreach (RectTransform rectt in content.GetComponentsInChildren<RectTransform>())
{
if (RectTransformUtility.RectangleContainsScreenPoint(rectt, Input.GetTouch(0).position, cam))
{
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Began:
Debug.Log("BEGAN");
scroll.GetComponent<UnityEngine.UI.ScrollRect>().enabled = false;
break;
case TouchPhase.Ended:
Debug.Log("ENDED");
scroll.GetComponent<UnityEngine.UI.ScrollRect>().enabled = true;
break;
}
}
}
}
實際上存在滾動內容,因為當觸摸位於數組的rectTransform內部時,我將禁用scrollRect的滾動,或者至少這就是我想做的。
該腳本將附加到滾動視圖的內容。
我假設您正在使用的RectTransform具有屏幕空間UI元素? 在這種情況下,問題在於UI元素未與相機關聯,因此傳遞給函數的相機參數應為null:
if (RectTransformUtility.RectangleContainsScreenPoint(rectt, Input.GetTouch(0).position, null))
問題是從文檔中確實不清楚。 看到這里: http : //fogbugz.unity3d.com/default.asp?660005_vnn982nkbubqr4qp
另外,請記住, Input.GetTouch
正常工作,您必須具有某種觸摸輸入設備,即,該設備只能在真實的手機或Unity Remote的編輯器中使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.