Пример #1
0
	// Update is called once per frame
	void Update () 
	{
		//Get the mouse position on the screen and send a raycast into the game world from that position.
		Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		LayerMask layMask = 1 << 8;
		RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero, Mathf.Infinity, layMask);
		InputObject pointedInputObject = null;
		pointedGameObject = null;
		if(hit.collider != null)
		{
			pointedGameObject = hit.collider.gameObject;
			pointedInputObject = pointedGameObject.GetComponent<InputObject>() as InputObject;
		}
		//If something was hit, the RaycastHit2D.collider will not be null.
		if (pointedInputObject != null)
		{
			pointedInputObject.WhenHangingOver();
		}
		if (Input.GetMouseButton(0))
		{
			if(objectDraggingFrom == null)
			{
				if(pointedInputObject != null)
				{
					objectDraggingFrom = pointedInputObject;
					objectDraggingFrom.WhenDragged();
				}
			}
			else
			{
				objectDraggingFrom.WhenDragging();
			}
			isPressed = true;
		}
		else
		{
			if(isPressed && objectDraggingFrom != null && pointedInputObject != null)
			{
                objectDraggingFrom.WhenMouseReleased();

				if(objectDraggingFrom == pointedInputObject)
				{
					pointedInputObject.WhenReleased();
				}
				else
				{
					objectDraggingFrom.WhenDraggedInto(pointedInputObject);
					
				}
				objectDraggingFrom = null;
			}
			isPressed = false;
		}
	}