When you start drag (OnMouseDown), save object position in some variable. Then OnMouseUp you can test that object is in position you needed else restore object position by saved variable.
UPD:
// Define some target to place object in. It can be any Collider
public Collider target;
// Variable for saving initial object position
private Vector3 initialPosition;
void OnMouseDown()
{
drag = true;
// saving initial object position
initialPosition = transform.position;
//Debug.Log("haii ");
}
void OnMouseDrag()
{
if(drag)
{
DragMe();
}
}
void OnMouseUp()
{
// UPD2: missed some test if object are actually dragging now
if(drag) {
// Testing object and target collision
if(TestObjectInPlace(target, transform))
{
// Place object in target position if they are colliding
transform.position = position;
// or you can call DragMe() to put object under mouse cursor
// DragMe();
} else {
// Restore initial object position if object is not collide with target
transform.position = initialPosition;
}
drag = false;
}
}
void DragMe()
{
var pos =Input.mousePosition;
pos.x =Mathf.Clamp(pos.x,0,Screen.width);
pos.y =Mathf.Clamp(pos.y,0,Screen.height);
if(zdist>0)
{
zdist=Vector3.Distance(Camera.main.transform.position,transform.position);
pos.z=zdist;
pos = Camera.main.ScreenToWorldPoint(pos);
transform.position =pos;
}
}
bool TestObjectInPlace(Collider target, Transform obj)
{
// I test some distance between object and target for demo
// (I use distance less than 1 unit, but you can use any parameter of Collider
// to test collision of target and object and if they are colliding return true
// or false if not)
return Vector3.Distance(target.transform.position, obj.position) < 1;
}