Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity convert pixels to units

    using UnityEngine;
    using System.Collections;
     
    public class ResolutionCompensation : MonoBehavior
    {
    public Vector2 WorldUnitsInCamera;
    public Vector2 WorldToPixelAmount;
     
    public GameObject Camera;
     
    void Awake ()
    {
            //Finding Pixel To World Unit Conversion Based On Orthographic Size Of Camera
            WorldUnitsInCamera.y = Camera.GetComponent<Camera>().orthographicSize * 2;
            WorldUnitsInCamera.x = WorldUnitsInCamera.y * Screen.width / Screen.height;
     
            WorldToPixelAmount.x = Screen.width / WorldUnitsInCamera.x;
            WorldToPixelAmount.y = Screen.height / WorldUnitsInCamera.y;
    }
     
     
    //Taking Your Camera Location And Is Off Setting For Position And For Amount Of World Units In Camera
    public Vector2 ConvertToWorldUnits(Vector2 TouchLocation)
    {
          Vector2 returnVec2;
     
    returnVec2.x = ((TouchLocation.x / WorldToPixelAmount.x) - (WorldUnitsInCamera.x / 2)) +
    Camera.transform.position.x;
    returnVec2.y = ((TouchLocation.y / WorldToPixelAmount.y) - (WorldUnitsInCamera.y / 2)) +
    Camera.transform.position.y;
     
          return returnVec2;
    }
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# sort a list of objects 
Csharp :: basic math functions in c# 
Csharp :: DrawImage resize to target size c# 
Csharp :: process run teamviewer address parametr c# 
Csharp :: c# string is all zeros 
Csharp :: file.deletealltext 
Csharp :: print the top view of the binary tree 
Csharp :: move position smoth unity 
Csharp :: design pattern for so many conditions c# 
Csharp :: python mokeypatch asser called 
Csharp :: EntityFramework: using tables in different scemas 
Csharp :: using == is inefficient unity 
Csharp :: check if app have administrator rights c# 
Csharp :: c# lambda get all records async 
Csharp :: unity set terrain to image 
Csharp :: card caption 
Csharp :: leantween unity when timescale 0 
Csharp :: how to make a respaen script in unity 
Csharp :: transformquestionmarks=OCR 
Csharp :: tee into file 
Csharp :: webtest fullscreen extend window maximize 
Csharp :: c# make two checkbox uncheckable both 
Csharp :: params keycord as var name c# 
Csharp :: C# write to lines without extra line 
Csharp :: Handle all AggregateExceptions when using Task.Whenall() async 
Csharp :: c# how to start an application and detect if started 
Csharp :: C# is folder 
Csharp :: obs mfplat.dll 
Csharp :: linq pick random element 
Csharp :: .net framework cheat sheet 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =