gameobject需要添加collider,原理是通过射线检测碰撞,返回所有与之接触的碰撞器,然后筛选所要选择的gameobject。
Vector3 pos = GetTouchPosition();
Ray myRay = Camera.main.ScreenPointToRay(pos);
RaycastHit2D[] hits = Physics2D.RaycastAll(new Vector2(myRay.origin.x, myRay.origin.y), Vector2.zero);
for (int index = 0; index < hits.Length; index++)
{
if (hits[index].collider)
{
GemControl gem = hits[index].transform.GetComponent<GemControl>();
if (gem != null)
{
}
}
}
public static Vector3 GetTouchPosition()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
return Input.mousePosition;
#else
return Input.touches[0].position;
#endif
}