Ghi*_*slo 0 c# unity-game-engine
我一直在尝试使用该功能
RectTransformUtility.RectangleContainsScreenPoint()
Run Code Online (Sandbox Code Playgroud)
与 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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际上滚动的东西就在那里,因为当触摸位于数组的 rectTransform 内部时,我禁用了滚动矩形的滚动,或者至少这就是我想要做的。
该脚本附加到滚动视图的内容中。
我假设您使用的 RectTransform 是屏幕空间 UI 元素?在这种情况下,问题是 UI 元素不与相机关联,因此传递给函数的相机参数应该为 null:
if (RectTransformUtility.RectangleContainsScreenPoint(rectt, Input.GetTouch(0).position, null))
Run Code Online (Sandbox Code Playgroud)
问题是文档中确实不清楚。请参阅此处: http://fogbugz.unity3d.com/default.asp ?660005_vnn982nkbubqr4qp
另外,请记住,为了Input.GetTouch
工作,您必须有某种触摸输入设备,即它只能在真实的手机或带有 Unity Remote 的编辑器中工作。