Exemple #1
0
 public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
 {
     localPoint = Vector2.zero;
     if (ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out Vector3 worldPoint))
     {
         localPoint = rect.InverseTransformPoint(worldPoint);
         return(true);
     }
     return(false);
 }
		public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
		{
			localPoint = Vector2.zero;
			Vector3 position;
			if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out position))
			{
				localPoint = rect.InverseTransformPoint(position);
				return true;
			}
			return false;
		}
Exemple #3
0
        public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
        {
            localPoint = Vector2.zero;
            Vector3 position;

            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out position))
            {
                localPoint = rect.InverseTransformPoint(position);
                return(true);
            }
            return(false);
        }
 public static Vector2 GetCanvasInputPosition(RectTransform canvas)
 {
     float camDistance = canvas.gameObject.transform.position.z - Camera.main.transform.position.z;
     if (INPUT_DEBUG) { //return mouse position in canvas
         Vector3 screenPoint = new Vector3(Input.mousePosition.x,Input.mousePosition.y,camDistance);
         Vector3 worldPoint = Camera.main.ScreenToWorldPoint (screenPoint);
         //Debug.Log ("worldpoint: " + worldPoint.x + ", " +worldPoint.y + ", " + worldPoint.z);
         Vector3 canvasPoint = canvas.InverseTransformPoint(worldPoint);
         //Debug.Log ("canvaspoint: " + canvasPoint.x + ", " +canvasPoint.y + ", " + canvasPoint.z);
         return new Vector2(canvasPoint.x,canvasPoint.y);
     } else { //return first finger position in canvas, or 0,0 if no touches exist
         if(Input.touchCount < 1){
             return lastCanvasPos;
         }
         Touch t = Input.GetTouch(0);
         Vector3 screenPoint = new Vector3(t.position.x,t.position.y,camDistance);
         Vector3 worldPoint = Camera.main.ScreenToWorldPoint (screenPoint);
         Vector3 canvasPoint = canvas.InverseTransformPoint(worldPoint);
         lastCanvasPos = new Vector2(canvasPoint.x,canvasPoint.y);
         return lastCanvasPos;
     }
 }
Exemple #5
0
        public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
        {
            localPoint = Vector2.zero;
            Vector3 position;
            bool    flag = RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out position);
            bool    result;

            if (flag)
            {
                localPoint = rect.InverseTransformPoint(position);
                result     = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
 public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
 {
     Vector3 vector;
     localPoint = Vector2.zero;
     if (ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out vector))
     {
         localPoint = rect.InverseTransformPoint(vector);
         return true;
     }
     return false;
 }
Exemple #7
0
		/// <summary>
		/// Calculates the drag position.
		/// </summary>
		/// <returns>The drag position.</returns>
		/// <param name="screenPosition">Screen position.</param>
		/// <param name="canvas">Canvas.</param>
		/// <param name="canvasRect">Canvas rect.</param>
		static public Vector3 CalculateDragPosition(Vector3 screenPosition, Canvas canvas, RectTransform canvasRect)
		{
			Vector3 result;
			var canvasSize = canvasRect.sizeDelta;
			Vector2 min = Vector2.zero;
			Vector2 max = canvasSize;
			
			var isOverlay = canvas.renderMode == RenderMode.ScreenSpaceOverlay;
			var noCamera = canvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.worldCamera == null;
			if (isOverlay || noCamera)
			{
				result = screenPosition;
			}
			else
			{
				var ray = canvas.worldCamera.ScreenPointToRay(screenPosition);
				var plane = new Plane(canvasRect.forward, canvasRect.position);
				
				float distance;
				plane.Raycast(ray, out distance);
				
				result = canvasRect.InverseTransformPoint(ray.origin + (ray.direction * distance));
				
				min = - Vector2.Scale(max, canvasRect.pivot);
				max = canvasSize - min;
			}
			
			result.x = Mathf.Clamp(result.x, min.x, max.y);
			result.y = Mathf.Clamp(result.y, min.x, max.y);
			
			return result;
		}
 Vector3 GetWorldPointInWidget(RectTransform target, Vector3 worldPoint)
 {
     return target.InverseTransformPoint(worldPoint);
 }
 public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
 {
   localPoint = Vector2.zero;
   Vector3 worldPoint;
   if (!RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out worldPoint))
     return false;
   localPoint = (Vector2) rect.InverseTransformPoint(worldPoint);
   return true;
 }