我有一个巨大的四边形和一个 PNG 作为子对象。
PNG 覆盖整个游戏区域/屏幕。
我知道如何检测点击,但我如何检测用户点击 PNG/quad 左侧还是右侧?
我应该在大四边形上方放置 2 个游戏对象以捕获右侧还是左侧的输入?或者,也许我可以捕获主要大四边形上的点击,但我会进行计算以找出它被点击的位置,如下所示:
if (tappedPosiion > quadWidth / 2)
{
// it's the right side
} else {
// its the left
}
问题是我不知道如何找出当前游戏对象的宽度!
最佳答案
Renderer.bounds.size 应该可以工作(或 collider.bounds.size)
http://answers.unity3d.com/questions/24012/find-size-of-gameobject.html
然后您可以通过获取 x 值来访问宽度:
“size.x 是盒子的宽度,size.y 是高度,size.z 是盒子的深度。”
http://docs.unity3d.com/ScriptReference/Bounds-size.html
现在您已经知道了宽度,因此应该相对容易找到他们点击的位置。
if(tappedLocation == width/2)
//middle
if(tappedLocation > width/2)
//right side
if(tappedLocation < width/2)
//left
非常基本,但回答了您关于如何找到宽度并根据该信息查看他们点击的位置的问题。
关于android - Unity 初学者 - 2d 游戏 : detect position of tap on quad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591979/