Pixel To World Unit Conversion

 

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ResolutionCompensation : MonoBehavior
  5. {
  6. public Vector2 WorldUnitsInCamera;
  7. public Vector2 WorldToPixelAmount;
  8.  
  9. public GameObject Camera;
  10.  
  11. void Awake ()
  12. {
  13.         //Finding Pixel To World Unit Conversion Based On Orthographic Size Of Camera
  14.         WorldUnitsInCamera.y = Camera.GetComponent<Camera>().orthographicSize * 2;
  15.         WorldUnitsInCamera.x = WorldUnitsInCamera.y * Screen.width / Screen.height;
  16.  
  17.         WorldToPixelAmount.x = Screen.width / WorldUnitsInCamera.x;
  18.         WorldToPixelAmount.y = Screen.height / WorldUnitsInCamera.y;
  19. }
  20.  
  21.  
  22. //Taking Your Camera Location And Is Off Setting For Position And For Amount Of World Units In Camera
  23. public Vector2 ConvertToWorldUnits(Vector2 TouchLocation)
  24. {
  25.       Vector2 returnVec2;
  26.  
  27. returnVec2.x = ((TouchLocation.x / WorldToPixelAmount.x) - (WorldUnitsInCamera.x / 2)) +
  28. Camera.transform.position.x;
  29. returnVec2.y = ((TouchLocation.y / WorldToPixelAmount.y) - (WorldUnitsInCamera.y / 2)) +
  30. Camera.transform.position.y;
  31.  
  32.       return returnVec2;
  33. }
  34. }

Comentarios

Entradas más populares de este blog

Flutter en iOS: themeMode no cambia a modo oscuro si se usa `ThemeMode.system`

Unity Admob GDPR Compliance, (Easy way)

Admob Unity