1
public static void MoveObject(Transform transform, Vector2 deltaPosition, Camera camera = null)
{
if (transform != null)
{
if (deltaPosition.x != 0.0f || deltaPosition.y != 0.0f)
{
var rectTransform = transform as RectTransform;
// If this is RectTransform then modify the anchoredPosition
if (rectTransform != null)
{
rectTransform.anchoredPosition += deltaPosition;
}
// If this is Transform then modify the position
else
{
transform.position = MoveObject(transform.position, deltaPosition, camera);
}
}
}
}
public static Vector3 MoveObject(Vector3 worldPosition, Vector2 deltaPosition, Camera camera = null)
{
if (camera == null) camera = Camera.main;
if (camera != null)
{
// Find current screen position of world position
var screenPosition = camera.WorldToScreenPoint(worldPosition);
// Modify screen position
screenPosition += (Vector3)deltaPosition;
// Write new world position
worldPosition = camera.ScreenToWorldPoint(screenPosition);
}
return worldPosition;
}