Version: Unity3D 4.6.2f1
I have a grid of tiles (Cube GameObject
s with a Tile
script attached). They are positioned at their index location [x][y] = (x, y, z=0) World Coordinates
.
Below, I try to get the coordinates of the tile upon which the mouse left click is released.
Before into code:
OnMouseDown()
- OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.OnMouseUp()
- OnMouseUp is called when the user has released the mouse button.
Based on these definitions, I assumed (mistake?) OnMouseUp()
would be called by the object that above it, the mouse left click was released.
On the Tile
script, I have the following :
public class Tile : MonoBehaviour
{
// variables
private Vector2 _gridPosition = Vector2.zero;
public Vector2 GridPosition
{
get { return _gridPosition; }
set { _gridPosition = value; }
}
// functions
string Vec2toText(Vector2 v)
{
return String.Format("(" + v.x + ", " + v.y + ")");
}
void OnMouseDown()
{
Debug.Log("TILE:: ON_MOUSE_DOWN:: " + Vec2toText(_gridPosition));
}
void OnMouseUp()
{
Debug.Log("TILE:: ON_MOUSE_UP:: " + Vec2toText(_gridPosition));
}
void OnMouseEnter()
{
Debug.Log("TILE:: MOUSE_ENTER: " + Vec2toText(_gridPosition));
}
void OnMouseExit()
{
Debug.Log("TILE:: MOUSE_EXIT: " + Vec2toText(_gridPosition));
}
}
And here is the output:
TILE:: MOUSE_ENTER: (15, 15)
TILE:: MOUSE_DOWN: (15, 15)
TILE:: MOUSE_EXIT: (15, 15)
TILE:: MOUSE_ENTER: (14, 15)
TILE:: MOUSE_UP: (15, 15) %% This line
TILE:: MOUSE_EXIT: (14, 15)
I would expect OnMouseDown()
to be called by the object on which the mouse button was pressed (no problem here) and OnMouseUp()
to be called by the object on which the mouse button was released (instead, it is called by the same object as OnMouseDown()
).
Is this -- calling two functions by the same object even though I pressed left click some object and released it on some other object -- intended?