如何通过鼠标单击/触摸网格来移动对象(例如使用 GUITexture 的游戏对象填充颜色)并检查每个图块是否包含填充颜色?
如果使用 Camera.main.ScreenToWorldPoint 则无法检查包含。当前我要检查的代码包含工作正常但游戏对象不能用鼠标移动:(
链接图片:http: //i.imgur.com/cHoS0YV.png
我的代码如下:
void OnMouseDown()
{
Vector2 mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
transform.position = mousePos;
Debug.Log("OnMouseDown: transform.position: " + transform.position);
}
void OnMouseDrag()
{
Vector2 mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
transform.position = mousePos;
Debug.Log("transform.position: " + transform.position);
foreach (var x in GameBoard.Instance.listTileInGrid)
{
if (x.RectTile.Contains(mousePos))
{
Debug.Log("change color");
x.ColorId = 1;
}
}
}
提前致谢!
更新:
我改变:
transform.position = mousePos;
至
transform.guiTexture.transform.position = Camera.main.ScreenToViewportPoint(Input.mousePosition);